Spring Boot整合Elasticsearch,实现function score query权重分查询

企业动态
实际场景还会很复杂。这里只是点睛之笔,优化或者更改下DSL语句就可以完成自己想要的搜索规则。

本文提纲

一、ES 的使用场景

二、运行 springboot-elasticsearch 工程

三、springboot-elasticsearch 工程代码详解

[[191772]]

运行环境:JDK 7 或 8,Maven 3.0+

技术栈:SpringBoot 1.5+,ElasticSearch 2.3.2

一、ES 的使用场景

简单说,ElasticSearch(简称 ES)是搜索引擎,是结构化数据的分布式搜索引擎。在

《Elasticsearch 和插件 elasticsearch-head 安装详解》

《Elasticsearch 默认配置 IK 及 Java AnalyzeRequestBuilder 使用》

我详细的介绍了如何安装,初步使用了 IK 分词器。这里,我主要讲下 SpringBoot 工程中如何使用 ElasticSearch。

ES 的使用场景大致分为两块:

1. 全文检索。加上分词(IK 是其中一个)、拼音插件等可以成为强大的全文搜索引擎。

2. 日志统计分析。可以实时动态分析海量日志数据。

二、运行 springboot-elasticsearch 工程

注意的是这里使用的是 ElasticSearch 2.3.2。是因为版本对应关系 :

  • 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 下载工程 springboot-elasticsearch ,项目地址见 GitHub - https://github.com/JeffLi1993/springboot-learning-example

1. 后台起守护线程启动 Elasticsearch

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

下面开始运行工程步骤(Quick Start):

2. 项目结构介绍

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

本地启动的 ES ,就不需要改配置文件了。如果连测试 ES 服务地址,需要修改相应配置

3.编译工程

在项目根目录 springboot-elasticsearch,运行 maven 指令:

  1. mvn clean install 

4.运行工程

右键运行 Application 应用启动类(位置:/springboot-learning-example/springboot-elasticsearch/src/main/java/org/spring/springboot/Application.java)的 main 函数,这样就成功启动了 springboot-elasticsearch 案例。

用 Postman 工具新增两个城市

新增城市信息

  1. POST http://127.0.0.1:8080/api/city 
  2.     "id":"1"
  3.     "provinceid":"1"
  4.     "cityname":"温岭"
  5.     "description":"温岭是个好城市" 
  1. POST http://127.0.0.1:8080/api/city 
  2.     "id":"2"
  3.     "provinceid":"2"
  4.     "cityname":"温州"
  5.     "description":"温州是个热城市" 

可以打开 ES 可视化工具 head 插件:http://localhost:9200/_plugin/head/

(如果不知道怎么安装,请查阅

《Elasticsearch 和插件 elasticsearch-head 安装详解》。)

在「数据浏览」tab,可以查阅到 ES 中数据是否被插入,插入后的数据格式如下:

  1.      "_index""cityindex"
  2.      "_type""city"
  3.      "_id""1"
  4.      "_version": 1, 
  5.      "_score": 1, 
  6.      "_source": { 
  7.           "id": 1, 
  8.           "provinceid": 1, 
  9.           "cityname""温岭"
  10.           "description""温岭是个好城市" 
  11.      } 

下面验证下权重分查询搜索接口的实现:

  1. GET http://localhost:8080/api/city/search?pageNumber=0&pageSize=10&searchContent=温岭 

数据是会出现:

  1.   { 
  2.     "id": 1, 
  3.     "provinceid": 1, 
  4.     "cityname""温岭"
  5.     "description""温岭是个好城市" 
  6.   }, 
  7.   { 
  8.     "id": 2, 
  9.     "provinceid": 2, 
  10.     "cityname""温州"
  11.     "description""温州是个热城市" 
  12.   } 

从启动后台 Console 可以看出,打印出来对应的 DSL 语句:

  1.  { 
  2.   "function_score" : { 
  3.     "functions" : [ { 
  4.       "filter" : { 
  5.         "bool" : { 
  6.           "should" : { 
  7.             "match" : { 
  8.               "cityname" : { 
  9.                 "query" : "温岭"
  10.                 "type" : "boolean" 
  11.               } 
  12.             } 
  13.           } 
  14.         } 
  15.       }, 
  16.       "weight" : 1000.0 
  17.     }, { 
  18.       "filter" : { 
  19.         "bool" : { 
  20.           "should" : { 
  21.             "match" : { 
  22.               "description" : { 
  23.                 "query" : "温岭"
  24.                 "type" : "boolean" 
  25.               } 
  26.             } 
  27.           } 
  28.         } 
  29.       }, 
  30.       "weight" : 100.0 
  31.     } ] 
  32.   } 

为什么会出现 温州 城市呢?因为 function score query 权重分查询,无相关的数据默认分值为 1。如果想除去,设置一个 setMinScore 分值即可。

三、springboot-elasticsearch 工程代码详解

具体代码见 GitHub - https://github.com/JeffLi1993/springboot-learning-example

1.pom.xml 依赖

  1. <dependencies> 
  2.  
  3.     <!-- Spring Boot Elasticsearch 依赖 --> 
  4.     <dependency> 
  5.         <groupId>org.springframework.boot</groupId> 
  6.         <artifactId>spring-boot-starter-data-elasticsearch</artifactId> 
  7.     </dependency> 
  8.  
  9.     <!-- Spring Boot Web 依赖 --> 
  10.     <dependency> 
  11.         <groupId>org.springframework.boot</groupId> 
  12.         <artifactId>spring-boot-starter-web</artifactId> 
  13.     </dependency> 
  14.  
  15.     <!-- Junit --> 
  16.     <dependency> 
  17.         <groupId>junit</groupId> 
  18.         <artifactId>junit</artifactId> 
  19.         <version>4.12</version> 
  20.     </dependency> 
  21. </dependencies> 

2. application.properties 配置 ES 地址

  1. # ES 
  2. spring.data.elasticsearch.repositories.enabled = true 
  3. spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300 

默认 9300 是 Java 客户端的端口。9200 是支持 Restful HTTP 的接口。

3. ES 数据操作层

  1. @Repository 
  2. public interface CityRepository extends ElasticsearchRepository<City,Long> { 
  3.  

接口只要继承 ElasticsearchRepository 类即可。默认会提供很多实现,比如 CRUD 和搜索相关的实现。

4. 实体类

  1. @Document(indexName = "cityindex", type = "city"
  2. public class City implements Serializable
  3.  
  4.     private static final long serialVersionUID = -1L; 
  5.  
  6.     /** 
  7.      * 城市编号 
  8.      */ 
  9.     private Long id; 
  10.  
  11.     /** 
  12.      * 省份编号 
  13.      */ 
  14.     private Long provinceid; 
  15.  
  16.     /** 
  17.      * 城市名称 
  18.      */ 
  19.     private String cityname; 
  20.  
  21.     /** 
  22.      * 描述 
  23.      */ 
  24.     private String description; 

注意

index 配置必须是全部小写,不然会引出异常:

  1. org.elasticsearch.indices.InvalidIndexNameException: Invalid index name [cityIndex], must be lowercase 

5. ES 业务逻辑层

Service 实现类:

  1. /** 
  2.  * 城市 ES 业务逻辑实现类 
  3.  * 
  4.  * Created by bysocket on 07/02/2017. 
  5.  */ 
  6. @Service 
  7. public class CityESServiceImpl implements CityService { 
  8.  
  9.     private static final Logger LOGGER = LoggerFactory.getLogger(CityESServiceImpl.class); 
  10.  
  11.     @Autowired 
  12.     CityRepository cityRepository; 
  13.  
  14.     @Override 
  15.     public Long saveCity(City city) { 
  16.  
  17.         City cityResult = cityRepository.save(city); 
  18.         return cityResult.getId(); 
  19.     } 
  20.  
  21.     @Override 
  22.     public List<City> searchCity(Integer pageNumber, 
  23.                                  Integer pageSize, 
  24.                                  String searchContent) { 
  25.         // 分页参数 
  26.         Pageable pageable = new PageRequest(pageNumber, pageSize); 
  27.  
  28.         // Function Score Query 
  29.         FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery() 
  30.                 .add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("cityname", searchContent)), 
  31.                     ScoreFunctionBuilders.weightFactorFunction(1000)) 
  32.                 .add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("description", searchContent)), 
  33.                         ScoreFunctionBuilders.weightFactorFunction(100)); 
  34.  
  35.         // 创建搜索 DSL 查询 
  36.         SearchQuery searchQuery = new NativeSearchQueryBuilder() 
  37.                 .withPageable(pageable) 
  38.                 .withQuery(functionScoreQueryBuilder).build(); 
  39.  
  40.         LOGGER.info("\n searchCity(): searchContent [" + searchContent + "] \n DSL  = \n " + searchQuery.getQuery().toString()); 
  41.  
  42.         Page<City> searchPageResults = cityRepository.search(searchQuery); 
  43.         return searchPageResults.getContent(); 
  44.     } 
  45.  

保存逻辑很简单,这里不解释了。

分页 function score query 搜索逻辑:

先创建分页参数,然后用 FunctionScoreQueryBuilder 定义 Function Score Query,并设置对应字段的权重分值。城市名称 1000 分,description 100 分。

然后创建该搜索的 DSL 查询,并打印出来。

四、小结

实际场景还会很复杂。这里只是点睛之笔,后续大家优化或者更改下 DSL 语句就可以完成自己想要的搜索规则。

【本文为51CTO专栏作者“李强强”的原创稿件,转载请通过51CTO联系作者获取授权】

戳这里,看该作者更多好文

责任编辑:武晓燕 来源: 51CTO专栏
相关推荐

2022-01-04 19:15:33

ElasticsearSpring BootLogstash

2017-04-17 10:35:40

Spring BooRedis 操作

2023-08-02 07:21:30

工具搜索排序

2022-12-23 08:28:42

策略模式算法

2022-07-21 11:04:53

Swagger3Spring

2017-10-17 15:14:33

Spring BooThymeleafWeb

2022-08-24 08:42:59

Minio存储Golang

2023-10-12 10:32:51

2022-05-30 07:31:38

SpringBoot搜索技巧

2022-05-06 10:42:09

JavaFlowable引擎

2021-12-27 09:59:57

SpringCanal 中间件

2024-03-26 08:08:08

SpringBPMN模型

2021-09-16 10:29:05

开发技能代码

2009-06-18 09:47:50

2023-10-09 16:35:19

方案Spring支付

2017-05-12 15:47:15

Spring BootMybatis Ann Web

2022-05-18 12:04:19

Mybatis数据源Spring

2023-11-13 12:48:32

语言DSL

2021-09-08 10:23:08

读写分离Java数据库

2022-02-09 20:39:52

Actuator应用监控
点赞
收藏

51CTO技术栈公众号