Spring Boot Integration Elasticsearch for function score query weighting

Source: Internet
Author: User

Operating environment: JDK 7 or 8,maven 3.0+

Technology stack: Springboot 1.5+,elasticsearch 2.3.2

Outline of this article

First, ES of the use of the scene

Second, the operation of Springboot-elasticsearch project

Three, Springboot-elasticsearch engineering code detailed


"springboot-learning-example" Open Source project, Fork a bit, pull a lot request~

The Spring boot Practice learning case is the best practice for Spring boot beginners and core technology consolidation.

First, ES of the use of the scene

Simply put, ElasticSearch (ES) is a search engine and a distributed search engine for structured data.

In the "Elasticsearch and plug-in Elasticsearch-head installation Details" and "Elasticsearch default configuration IK and Java analyzerequestbuilder use" I described in detail how to install, the initial use of I K word breaker.

Here, I mainly talk about how to use ElasticSearch in the Springboot project.

The use of ES is broadly divided into two pieces

1. Full-Text search. Plus participle (IK is one of them), pinyin plug-ins and so on can become a powerful full-text search engine.

2. Log statistics analysis. You can dynamically analyze massive amounts of log data in real time.

Second, the operation of Springboot-elasticsearch project

Note that the ElasticSearch 2.3.2 is used here. is because of the version correspondence relationship:

Spring Boot Version (x) Spring Data Elasticsearch Version (y) Elasticsearch Version (z)x <= 1.3.5 y <= 1.3.4 z <= 1.7.2* x >= 1.4.x 2.0.0 <=y < 5.0.0** 2.0.0 <= z < 5.0.0*** - 只需要你修改下对应的 pom 文件版本号** - 下一个 ES 的版本会有重大的更新

git clone download Project Springboot-elasticsearch, project address see GitHub- https://github.com/jeffli1993/s Pringboot-learning-example.

1. Back up daemon thread start Elasticsearch

cd elasticsearch-2.3.2/./bin/elasticsearch -d

The following starts the engineering step (Quick start):

2. Introduction to the project structure

org.spring.springboot.controller - Controller 层org.spring.springboot.repository - ES 数据操作层org.spring.springboot.domain - 实体类org.spring.springboot.service - ES 业务逻辑层Application - 应用启动类application.properties - 应用配置文件,应用启动会自动读取配置

Locally launched ES, you do not need to change the configuration file. If you are testing the ES service address, you need to modify the appropriate configuration

3. Compiling the project

In the project root directory Springboot-elasticsearch, run the MAVEN directive:

mvn clean install

4. Running the project

Right-click to run the Application app startup class (location:/springboot-learning-example/springboot-elasticsearch/src/main/java/org/spring/ Springboot/application.java) is the main function, which successfully launches the Springboot-elasticsearch case.

Two new cities with Postman tool

New City Information


You can open the ES visualizer head plugin: http://localhost:9200/_plugin/head/: (If you don't know how to install it, check out the Elasticsearch and plug-in Elasticsearch-head installation details.) )

In the data browsing "tab, you can find out if the data in ES is inserted, and the data format is as follows:

{"_index": "cityindex","_type": "city","_id": "1","_version": 1,"_score": 1,"_source": {"id": 1,"provinceid": 1,"cityname": "温岭","description": "温岭是个好城市"}}

The following verify the implementation of the weighted sub-query Search interface: GET http://localhost:8080/api/city/search?pageNumber=0&pageSize=10&searchContent= wenling

The data will appear

[{"id": 1,"provinceid": 1,"cityname": "温岭","description": "温岭是个好城市"},{"id": 2,"provinceid": 2,"cityname": "温州","description": "温州是个热城市"}]

From the background Console can be seen, print out the corresponding DSL statement:

{"function_score" : {"functions" : [ {"filter" : {"bool" : {"should" : {"match" : {"cityname" : {"query" : "温岭","type" : "boolean"}}}}},"weight" : 1000.0}, {"filter" : {"bool" : {"should" : {"match" : {"description" : {"query" : "温岭","type" : "boolean"}}}}},"weight" : 100.0} ]}}

Why is there a Wenzhou city? Because function score query weights, no related data default score is 1. If you want to remove, set a Setminscore score.

Three, Springboot-elasticsearch engineering code detailed

Specific code See GITHUB- https://github.com/jeffli1993/springboot-learning-example

1.pom.xml Dependency

<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 Http://maven.apache . org/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupid>springboot</ Groupid> <artifactId>springboot-elasticsearch</artifactId> <version>0.0.1-snapshot</    Version> <name>springboot-elasticsearch:: Integrated elasticsearch </name> <!--Spring Boot boot parent Dependency-- <parent> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-star ter-parent</artifactid> <version>1.5.1.RELEASE</version> </parent> &LT;DEPENDENCIES&G        T <!--Spring Boot Elasticsearch Dependency--<dependency> &LT;GROUPID&GT;ORG.SPRINGFRAMEWORK.BOOT&L T;/groupid> &LT;ARTIFACTID&GT;spring-boot-starter-data-elasticsearch</artifactid> </dependency> <!--Spring boot Web dependency- <dependency> <groupId>org.springframework.boot</groupId> <artifact Id>spring-boot-starter-web</artifactid> </dependency> <!--Junit-to-<depende Ncy> <groupId>junit</groupId> <artifactId>junit</artifactId> & Lt;version>4.12</version> </dependency> </dependencies></project>

The Spring-boot-starter-data-elasticsearch version relied on here is 1.5.1.RELEASE, and the corresponding Spring-data-elasticsearch version is 2.1.0.RELEASE.

The subsequent data operations layer is implemented through the interface provided by the Spring-data-elasticsearch. Official documentation for Operation:/http/docs.spring.io/spring-data/elasticsearch/docs/2.1.0.release/reference/html/ .

2. Application.properties Configure ES Address

# ESspring.data.elasticsearch.repositories.enabled = truespring.data.elasticsearch.cluster-nodes =

The default 9300 is the port of the Java client. 9200 is the interface that supports Restful HTTP.

More configurations:

spring.data.elasticsearch.cluster-name Elasticsearch 集群名。(默认值: elasticsearch)spring.data.elasticsearch.cluster-nodes 集群节点地址列表,用逗号分隔。如果没有指定,就启动一个客户端节点。spring.data.elasticsearch.propertie 用来配置客户端的额外属性。spring.data.elasticsearch.repositories.enabled 开启 Elasticsearch 仓库。(默认值:true。)

3. ES Data Operation Layer

@Repositorypublic interface CityRepository extends ElasticsearchRepository<City,Long> {}

Interfaces only inherit the Elasticsearchrepository class. Many implementations, such as CRUD and search-related implementations, are provided by default.

4. Entity classes

@Document(indexName = "cityindex", type = "city")public class City implements Serializable{    private static final long serialVersionUID = -1L;    /**     * 城市编号     */    private Long id;    /**     * 省份编号     */    private Long provinceid;    /**     * 城市名称     */    private String cityname;    /**     * 描述     */    private String description;}

Note that the index configuration must be all lowercase, or it will be a violent exception. Org.elasticsearch.indices.InvalidIndexNameException:Invalid index name [Cityindex], must be lowercase

5. ES Business Logic Layer

/** * City ES business Logic implementation class * * Created by Bysocket on 07/02/2017. */@Servicepublic class Cityesserviceimpl implements Cityservice {private static final Logger Logger = loggerfactory.ge    Tlogger (Cityesserviceimpl.class);    @Autowired cityrepository cityrepository;        @Override public Long savecity, city Cityresult = Cityrepository.save (city);    return Cityresult.getid ();                                  } @Override Public list<city> searchcity (integer pagenumber, Integer pageSize, String searchcontent) {//paging parameter pageable pageable = new Pagerequest (pagenum        ber, pageSize);                Function score Query Functionscorequerybuilder functionscorequerybuilder = Querybuilders.functionscorequery ()                    . Add (Querybuilders.boolquery (). Should (Querybuilders.matchquery ("CityName", searchcontent)), Scorefunctionbuilders.weightfactorfunction (+). Add (querybuilders.bOolquery (). Should (Querybuilders.matchquery ("description", searchcontent)), scorefunctionbuilders.we        Ightfactorfunction (100));                Create a search DSL query SearchQuery SearchQuery = new Nativesearchquerybuilder (). withpageable (pageable)        . Withquery (Functionscorequerybuilder). build ();        Logger.info ("\ n searchcity (): searchcontent [" + searchcontent + "] \ n DSL = \ n" + searchquery.getquery (). toString ());        page<city> searchpageresults = Cityrepository.search (searchQuery);    return Searchpageresults.getcontent (); }}

Saving logic is simple. The paging function score query search logic is as follows: Create a paging parameter, then define the function score query with Functionscorequerybuilder and set the weight score for the corresponding field. City name 1000 points, description 100 points. Then create the search for the DSL query and print it out.

Iv. Summary

The actual scenario can also be complex. This is just the finishing touch of the pen, the following people to optimize or change the next DSL statement can complete their own desired search rules.

Abstract: Original creation Place http://www. bysocket.com "Mason bysocket"

Spring Boot Integration Elasticsearch for function score query weighting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.