当前位置:   article > 正文

通过java代码实现ES中的常用搜索_java es搜索引擎

java es搜索引擎

目录

测试环境准备

在指定索引下搜索全部(可以指定字段)

通过ids进行搜索

对搜索结果进行分页

match分词搜索

不分词模糊搜索:wildcardQuery与matchPhraseQuery

term 搜索(精确匹配)

multi_match搜索

bool搜索 多条件匹配

filter过滤搜索

sort排序搜索

后续待补充:queryStringQuery,minimumShouldMatch,对检索结果中的关键词进行高亮


测试环境准备

测试环境:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter</artifactId>
  4. <version>2.0.6.RELEASE</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-test</artifactId>
  9. <scope>test</scope>
  10. <version>2.0.6.RELEASE</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.elasticsearch.client</groupId>
  14. <artifactId>elasticsearch-rest-high-level-client</artifactId>
  15. <version>7.3.0</version>
  16. <exclusions>
  17. <exclusion>
  18. <groupId>org.elasticsearch</groupId>
  19. <artifactId>elasticsearch</artifactId>
  20. </exclusion>
  21. </exclusions>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.elasticsearch</groupId>
  25. <artifactId>elasticsearch</artifactId>
  26. <version>7.3.0</version>
  27. </dependency>

2配置 application.yml

  1. spring:
  2. application:
  3. name: service-search
  4. eslearn:
  5. elasticsearch:
  6. hostlist: 127.0.0.1:9200 #多个结点中间用逗号分隔

 3主类代码

  1. @SpringBootApplication
  2. public class SearchApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(SearchApplication.class,args);
  5. }
  6. }

配置类:

  1. package com.learn.es.cofig;
  2. import org.apache.http.HttpHost;
  3. import org.elasticsearch.client.RestClient;
  4. import org.elasticsearch.client.RestHighLevelClient;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. /**
  9. * @author LJM
  10. * @create 2022/12/10
  11. */
  12. @Configuration
  13. public class ElasticSearchConfig {
  14. @Value("${eslearn.elasticsearch.hostlist}")
  15. private String hostList;
  16. @Bean(destroyMethod = "close") //表示连接使用完成后需要关闭
  17. public RestHighLevelClient restHighLevelClient(){
  18. String[] split = hostList.split(",");
  19. //这种写法是考虑到可能会配置多个es节点
  20. HttpHost[] httpHosts = new HttpHost[split.length];
  21. for (int i = 0; i < split.length; i++) {
  22. String item = split[i];
  23. httpHosts[i] = new HttpHost(item.split(":")[0],Integer.parseInt(item.split(":")[1]),"http");
  24. }
  25. return new RestHighLevelClient(RestClient.builder(httpHosts));
  26. }
  27. }

测试项目结构:

 在kibana中把数据插入es中:

  1. delete book //先删除索引
  2. PUT /book //往索引中插入数据进行测试,后面在api实现搜索的小节,可以使用从数据库中读取数据 然后把数据插入es库指定的索引
  3. PUT /book/_doc/1
  4. {
  5. "name": "Bootstrap开发",
  6. "description": "Bootstrap是由Twitter推出的一个前台页面开发css框架,是一个非常流行的开发框架,此框架集成了多种页面效果。此开发框架包含了大量的CSS、JS程序代码,可以帮助开发者(尤其是不擅长css页面开发的程序人员)轻松的实现一个css,不受浏览器限制的精美界面css效果。",
  7. "studymodel": "201002",
  8. "price":38.6,
  9. "timestamp":"2019-08-25 19:11:35",
  10. "pic":"group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg",
  11. "tags": [ "bootstrap", "dev"]
  12. }
  13. PUT /book/_doc/2
  14. {
  15. "name": "java编程思想",
  16. "description": "java语言是世界第一编程语言,在软件开发领域使用人数最多。",
  17. "studymodel": "201001",
  18. "price":68.6,
  19. "timestamp":"2019-08-25 19:11:35",
  20. "pic":"group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg",
  21. "tags": [ "java", "dev"]
  22. }
  23. PUT /book/_doc/3
  24. {
  25. "name": "spring开发基础",
  26. "description": "spring 在java领域非常流行,java程序员都在用。",
  27. "studymodel": "201001",
  28. "price":88.6,
  29. "timestamp":"2019-08-24 19:11:35",
  30. "pic":"group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg",
  31. "tags": [ "spring", "java"]
  32. }

测试类代码结构: 

  1. /**
  2. * @author LJM
  3. * @create 2022/12/13
  4. * 测试使用java代码实现es的各种搜索
  5. */
  6. @SpringBootTest(classes = SearchApplication.class)
  7. @RunWith(SpringRunner.class)
  8. public class TestSearch {
  9. @Autowired
  10. RestHighLevelClient client;
  11. // 后面的测试方法会全部写在这个类中 .....
  12. }

在指定索引下搜索全部(可以指定字段)

在kibana中 GET /book/_search 先获取一下本地es库中的book索引下有多少数据。

  1. /**
  2. * 搜索全部
  3. * GET book/_search
  4. * {
  5. * "query": {
  6. * "match_all": {}
  7. * }
  8. * }
  9. * @throws IOException
  10. */
  11. @Test
  12. public void testSearchAll() throws IOException {
  13. //1构建搜索请求 实际生产环境中这个索引名称的获取:①把这个索引名称写在枚举类中,然后从枚举类中获取②从配置文件中获取
  14. SearchRequest searchRequest = new SearchRequest("book");
  15. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  16. searchSourceBuilder.query(QueryBuilders.matchAllQuery());
  17. //获取某些字段
  18. searchSourceBuilder.fetchSource(new String[]{"name"}, new String[]{});
  19. searchRequest.source(searchSourceBuilder);
  20. //2执行搜索
  21. SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
  22. //3获取结果
  23. SearchHits hits = searchResponse.getHits();
  24. //数据数据
  25. SearchHit[] searchHits = hits.getHits();
  26. System.out.println("--------------------------");
  27. for (SearchHit hit : searchHits) {
  28. String id = hit.getId();
  29. float score = hit.getScore();
  30. //这个source里面就是我们存储的数据
  31. Map<String, Object> sourceAsMap = hit.getSourceAsMap();
  32. String name = (String) sourceAsMap.get("name");
  33. String description = (String) sourceAsMap.get("description");
  34. Double price = (Double) sourceAsMap.get("price");
  35. System.out.println("id:" + id);
  36. System.out.println("score:" + score);
  37. System.out.println("name:" + name);
  38. System.out.println("description:" + description);
  39. System.out.println("price:" + price);
  40. System.out.println("==========================");
  41. }
  42. }

通过ids进行搜索

  1. /**
  2. * 通过ids进行搜索 有的就查询出来,没有的也不会报错
  3. * GET /book/_search
  4. * {
  5. * "query": {
  6. * "ids" : {
  7. * "values" : ["1", "2", "6"]
  8. * }
  9. * }
  10. * }
  11. * @throws IOException
  12. */
  13. @Test
  14. public void testSearchIds() throws IOException {
  15. //1构建搜索请求
  16. SearchRequest searchRequest = new SearchRequest("book");
  17. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  18. searchSourceBuilder.query(QueryBuilders.idsQuery().addIds("1","2","6"));
  19. searchRequest.source(searchSourceBuilder);
  20. //2执行搜索
  21. SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
  22. //3获取结果
  23. SearchHits hits = searchResponse.getHits();
  24. //数据数据
  25. SearchHit[] searchHits = hits.getHits();
  26. System.out.println("--------------------------");
  27. for (SearchHit hit : searchHits) {
  28. String id = hit.getId();
  29. float score = hit.getScore();
  30. //这个source里面就是我们存储的数据
  31. Map<String, Object> sourceAsMap = hit.getSourceAsMap();
  32. String name = (String) sourceAsMap.get("name");
  33. String description = (String) sourceAsMap.get("description");
  34. Double price = (Double) sourceAsMap.get("price");
  35. System.out.println("id:" + id);
  36. System.out.println("name:" + name);
  37. System.out.println("description:" + description);
  38. System.out.println("price:" + price);
  39. System.out.println("==========================");
  40. }
  41. }

对搜索结果进行分页

  1. /**
  2. * 对搜索结果进行分页
  3. * GET book/_search
  4. * {
  5. * "query": {
  6. * "match_all": {}
  7. * },
  8. * "from": 0,
  9. * "size": 2
  10. * }
  11. */
  12. @Test
  13. public void testSearchPage() throws IOException {
  14. //1构建搜索请求
  15. SearchRequest searchRequest = new SearchRequest("book");
  16. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  17. searchSourceBuilder.query(QueryBuilders.matchAllQuery());
  18. //搜索第几页的数据
  19. int page=1;
  20. //每页展示几个数据
  21. //int size=2; //因为本地es一共就插入了三条数据进行测试,所以可以把大小分别设置为2和3看一下输出效果
  22. int size=3;
  23. //下标计算
  24. int from = (page-1) / size;
  25. searchSourceBuilder.from(from);
  26. searchSourceBuilder.size(size);
  27. searchRequest.source(searchSourceBuilder);
  28. //2执行搜索
  29. SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
  30. //3获取结果
  31. SearchHits hits = searchResponse.getHits();
  32. //数据数据
  33. SearchHit[] searchHits = hits.getHits();
  34. System.out.println("--------------------------");
  35. for (SearchHit hit : searchHits) {
  36. String id = hit.getId();
  37. float score = hit.getScore();
  38. //这个source里面就是我们存储的数据
  39. Map<String, Object> sourceAsMap = hit.getSourceAsMap();
  40. String name = (String) sourceAsMap.get("name");
  41. String description = (String) sourceAsMap.get("description");
  42. Double price = (Double) sourceAsMap.get("price");
  43. System.out.println("id:" + id);
  44. System.out.println("name:" + name);
  45. System.out.println("description:" + description);
  46. System.out.println("price:" + price);
  47. System.out.println("==========================");
  48. }
  49. }

match分词搜索

  1. /**
  2. * match搜索 这个是会进行分词搜索的
  3. * GET /book/_search
  4. * {
  5. * "query": {
  6. * "match": {
  7. * "description": "java程序员"
  8. * }
  9. * }
  10. * }
  11. * @throws IOException
  12. */
  13. @Test
  14. public void testSearchMatch() throws IOException {
  15. //1构建搜索请求
  16. SearchRequest searchRequest = new SearchRequest("book");
  17. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  18. searchSourceBuilder.query(QueryBuilders.matchQuery("description", "java程序员"));
  19. searchRequest.source(searchSourceBuilder);
  20. //2执行搜索
  21. SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
  22. //3获取结果
  23. SearchHits hits = searchResponse.getHits();
  24. //数据数据
  25. SearchHit[] searchHits = hits.getHits();
  26. System.out.println("--------------------------");
  27. for (SearchHit hit : searchHits) {
  28. String id = hit.getId();
  29. float score = hit.getScore();
  30. //这个source里面就是我们存储的数据
  31. Map<String, Object> sourceAsMap = hit.getSourceAsMap();
  32. String name = (String) sourceAsMap.get("name");
  33. String description = (String) sourceAsMap.get("description");
  34. Double price = (Double) sourceAsMap.get("price");
  35. System.out.println("id:" + id);
  36. System.out.println("name:" + name);
  37. System.out.println("description:" + description);
  38. System.out.println("price:" + price);
  39. System.out.println("==========================");
  40. }
  41. }

不分词模糊搜索:wildcardQuery与matchPhraseQuery

  1. /**
  2. * 不分词模糊搜索 like '%检索词%'
  3. * @throws IOException
  4. */
  5. @Test
  6. public void testSearchLike() throws IOException {
  7. //1构建搜索请求
  8. SearchRequest searchRequest = new SearchRequest("book");
  9. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  10. //使用matchPhraseQuery 需要对检索的关键词的前后加 * 否则不是完全的模糊匹配 可以对检索的结果中的检索关键词进行高亮
  11. searchSourceBuilder.query(QueryBuilders.matchPhraseQuery("description", "*"+"程序员"+"*"));
  12. //还可以使用wildcardQuery 但是这种就会导致检索的结果不能高亮(这个不太确定,但是我自己试的时候,这样确实是不能对检索结果中的检索词进行高亮)
  13. //需要在检索的字段名后拼接 ".keyword" 并且也需要对检索词前后添加 *
  14. // searchSourceBuilder.query(QueryBuilders.wildcardQuery("description"+".keyword","*"+"程序员"+"*"));
  15. searchRequest.source(searchSourceBuilder);
  16. //2执行搜索
  17. SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
  18. //3获取结果
  19. SearchHits hits = searchResponse.getHits();
  20. //数据数据
  21. SearchHit[] searchHits = hits.getHits();
  22. System.out.println("--------------------------");
  23. for (SearchHit hit : searchHits) {
  24. String id = hit.getId();
  25. float score = hit.getScore();
  26. //这个source里面就是我们存储的数据
  27. Map<String, Object> sourceAsMap = hit.getSourceAsMap();
  28. String name = (String) sourceAsMap.get("name");
  29. String description = (String) sourceAsMap.get("description");
  30. Double price = (Double) sourceAsMap.get("price");
  31. System.out.println("id:" + id);
  32. System.out.println("name:" + name);
  33. System.out.println("description:" + description);
  34. System.out.println("price:" + price);
  35. System.out.println("==========================");
  36. }
  37. }

term 搜索(精确匹配)

  1. /**
  2. * term 搜索 如果字段为keyword那么【存储】和【搜索】都不分词。 搜索的时候相当于在使用 = 符号进行搜索
  3. * GET / book / _search
  4. * {
  5. * "query":{
  6. * "term":{
  7. * "description":"java程序员"
  8. * }
  9. * }
  10. * }
  11. * @throws IOException
  12. */
  13. @Test
  14. public void testSearchTerm() throws IOException {
  15. //1构建搜索请求
  16. SearchRequest searchRequest = new SearchRequest("book");
  17. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  18. //如果字段的映射是text但是也想要精确匹配,可以这样操作:QueryBuilders.termQuery("description" + ".keyword","java程序员")
  19. searchSourceBuilder.query(QueryBuilders.termQuery("description", "java语言"));
  20. searchRequest.source(searchSourceBuilder);
  21. //2执行搜索
  22. SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
  23. //3获取结果
  24. SearchHits hits = searchResponse.getHits();
  25. //数据数据
  26. SearchHit[] searchHits = hits.getHits();
  27. System.out.println("--------------------------");
  28. for (SearchHit hit : searchHits) {
  29. String id = hit.getId();
  30. float score = hit.getScore();
  31. //这个source里面就是我们存储的数据
  32. Map<String, Object> sourceAsMap = hit.getSourceAsMap();
  33. String name = (String) sourceAsMap.get("name");
  34. String description = (String) sourceAsMap.get("description");
  35. Double price = (Double) sourceAsMap.get("price");
  36. System.out.println("id:" + id);
  37. System.out.println("name:" + name);
  38. System.out.println("description:" + description);
  39. System.out.println("price:" + price);
  40. System.out.println("==========================");
  41. }
  42. }

multi_match搜索

  1. /**
  2. * multi_match搜索
  3. * GET /book/_search
  4. * {
  5. * "query": {
  6. * "multi_match": {
  7. * "query": "java程序员",
  8. * "fields": ["name", "description"]
  9. * }
  10. * }
  11. * }
  12. * @throws IOException
  13. */
  14. @Test
  15. public void testSearchMultiMatch() throws IOException {
  16. //1构建搜索请求
  17. SearchRequest searchRequest = new SearchRequest("book");
  18. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  19. //第二个参数才是 字段 第一个参数是匹配的内容
  20. searchSourceBuilder.query(QueryBuilders.multiMatchQuery("java程序员","name","description"));
  21. searchRequest.source(searchSourceBuilder);
  22. //2执行搜索
  23. SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
  24. //3获取结果
  25. SearchHits hits = searchResponse.getHits();
  26. //数据数据
  27. SearchHit[] searchHits = hits.getHits();
  28. System.out.println("--------------------------");
  29. for (SearchHit hit : searchHits) {
  30. String id = hit.getId();
  31. float score = hit.getScore();
  32. //这个source里面就是我们存储的数据
  33. Map<String, Object> sourceAsMap = hit.getSourceAsMap();
  34. String name = (String) sourceAsMap.get("name");
  35. String description = (String) sourceAsMap.get("description");
  36. Double price = (Double) sourceAsMap.get("price");
  37. System.out.println("id:" + id);
  38. System.out.println("name:" + name);
  39. System.out.println("description:" + description);
  40. System.out.println("price:" + price);
  41. System.out.println("==========================");
  42. }
  43. }

bool搜索 多条件匹配

  1. /**
  2. * bool搜索 多条件匹配 and or !=
  3. * GET / book / _search
  4. * {
  5. * "query":{
  6. * "bool":{
  7. * "must": [
  8. * {
  9. * "multi_match":{
  10. * "query":"java程序员",
  11. * "fields": ["name", "description"]
  12. * }
  13. * }
  14. * ],
  15. * "should": [
  16. * {
  17. * "match":{
  18. * "studymodel":"201001"
  19. * }
  20. * }
  21. * ]
  22. * }
  23. * }
  24. * }
  25. * @throws IOException
  26. */
  27. @Test
  28. public void testSearchBool() throws IOException {
  29. //1构建搜索请求
  30. SearchRequest searchRequest = new SearchRequest("book");
  31. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  32. //构建multiMatch请求 第一个参数是检索内容 第二个参数是检索的字段
  33. MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("java程序员", "name", "description");
  34. //构建match请求
  35. MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "测试没有的字段");
  36. BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
  37. boolQueryBuilder.must(multiMatchQueryBuilder);
  38. boolQueryBuilder.should(matchQueryBuilder);
  39. // boolQueryBuilder.must(matchQueryBuilder); //把should改成must就相当于用 and进行了再一次的过滤 就会查询不到数据
  40. searchSourceBuilder.query(boolQueryBuilder);
  41. searchRequest.source(searchSourceBuilder);
  42. //2执行搜索
  43. SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
  44. //3获取结果
  45. SearchHits hits = searchResponse.getHits();
  46. //数据数据
  47. SearchHit[] searchHits = hits.getHits();
  48. System.out.println("--------------------------");
  49. for (SearchHit hit : searchHits) {
  50. String id = hit.getId();
  51. float score = hit.getScore();
  52. //这个source里面就是我们存储的数据
  53. Map<String, Object> sourceAsMap = hit.getSourceAsMap();
  54. String name = (String) sourceAsMap.get("name");
  55. String description = (String) sourceAsMap.get("description");
  56. Double price = (Double) sourceAsMap.get("price");
  57. System.out.println("id:" + id);
  58. System.out.println("name:" + name);
  59. System.out.println("description:" + description);
  60. System.out.println("price:" + price);
  61. System.out.println("==========================");
  62. }
  63. }
  64. }

filter过滤搜索

过滤搜索和范围搜索的区别:

filter,仅仅只是按照搜索条件过滤出需要的数据而已,不计算任何相关度分数,对相关度没有任何影响。

query,会去计算每个document相对于搜索条件的相关度,并按照相关度进行排序。

  1. /**
  2. * filter过滤搜索
  3. * GET /book/_search
  4. * {
  5. * "query": {
  6. * "bool": {
  7. * "must": [
  8. * {
  9. * "multi_match": {
  10. * "query": "java程序员",
  11. * "fields": ["name","description"]
  12. * }
  13. * }
  14. * ],
  15. * "should": [
  16. * {
  17. * "match": {
  18. * "studymodel": "201001"
  19. * }
  20. * }
  21. * ],
  22. * "filter": {
  23. * "range": {
  24. * "price": {
  25. * "gte": 50,
  26. * "lte": 90
  27. * }
  28. * }
  29. *
  30. * }
  31. * }
  32. * }
  33. * }
  34. * @throws IOException
  35. */
  36. @Test
  37. public void testSearchFilter() throws IOException {
  38. //1构建搜索请求
  39. SearchRequest searchRequest = new SearchRequest("book");
  40. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  41. //构建multiMatch请求
  42. MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("java程序员", "name", "description");
  43. //构建match请求
  44. MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("studymodel", "201001");
  45. BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
  46. boolQueryBuilder.must(multiMatchQueryBuilder);
  47. boolQueryBuilder.should(matchQueryBuilder);
  48. boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(50).lte(90));
  49. searchSourceBuilder.query(boolQueryBuilder);
  50. searchRequest.source(searchSourceBuilder);
  51. //2执行搜索
  52. SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
  53. //3获取结果
  54. SearchHits hits = searchResponse.getHits();
  55. //数据数据
  56. SearchHit[] searchHits = hits.getHits();
  57. System.out.println("--------------------------");
  58. for (SearchHit hit : searchHits) {
  59. String id = hit.getId();
  60. float score = hit.getScore();
  61. //这个source里面就是我们存储的数据
  62. Map<String, Object> sourceAsMap = hit.getSourceAsMap();
  63. String name = (String) sourceAsMap.get("name");
  64. String description = (String) sourceAsMap.get("description");
  65. Double price = (Double) sourceAsMap.get("price");
  66. System.out.println("id:" + id);
  67. System.out.println("name:" + name);
  68. System.out.println("description:" + description);
  69. System.out.println("price:" + price);
  70. System.out.println("==========================");
  71. }
  72. }

sort排序搜索

  1. /**
  2. * sort排序搜索
  3. * GET /book/_search
  4. * {
  5. * "query": {
  6. * "bool": {
  7. * "must": [
  8. * {
  9. * "multi_match": {
  10. * "query": "java程序员",
  11. * "fields": ["name","description"]
  12. * }
  13. * }
  14. * ],
  15. * "should": [
  16. * {
  17. * "match": {
  18. * "studymodel": "201001"
  19. * }
  20. * }
  21. * ],
  22. * "filter": {
  23. * "range": {
  24. * "price": {
  25. * "gte": 50,
  26. * "lte": 90
  27. * }
  28. * }
  29. *
  30. * }
  31. * }
  32. * },
  33. * "sort": [
  34. * {
  35. * "price": {
  36. * "order": "asc"
  37. * }
  38. * }
  39. * ]
  40. * }
  41. * @throws IOException
  42. */
  43. @Test
  44. public void testSearchSort() throws IOException {
  45. //1构建搜索请求
  46. SearchRequest searchRequest = new SearchRequest("book");
  47. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  48. //构建multiMatch请求
  49. MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("java程序员", "name", "description");
  50. //构建match请求
  51. MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("studymodel", "201001");
  52. BoolQueryBuilder boolQueryBuilder=QueryBuilders.boolQuery();
  53. boolQueryBuilder.must(multiMatchQueryBuilder);
  54. boolQueryBuilder.should(matchQueryBuilder);
  55. boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(50).lte(90));
  56. searchSourceBuilder.query(boolQueryBuilder);
  57. //按照价格升序
  58. searchSourceBuilder.sort("price", SortOrder.ASC);
  59. searchRequest.source(searchSourceBuilder);
  60. //2执行搜索
  61. SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
  62. //3获取结果
  63. SearchHits hits = searchResponse.getHits();
  64. //数据数据
  65. SearchHit[] searchHits = hits.getHits();
  66. System.out.println("--------------------------");
  67. for (SearchHit hit : searchHits) {
  68. String id = hit.getId();
  69. float score = hit.getScore();
  70. //这个source里面就是我们存储的数据
  71. Map<String, Object> sourceAsMap = hit.getSourceAsMap();
  72. String name = (String) sourceAsMap.get("name");
  73. String description = (String) sourceAsMap.get("description");
  74. Double price = (Double) sourceAsMap.get("price");
  75. System.out.println("id:" + id);
  76. System.out.println("name:" + name);
  77. System.out.println("description:" + description);
  78. System.out.println("price:" + price);
  79. System.out.println("==========================");
  80. }
  81. }

后续待补充:queryStringQuery,minimumShouldMatch,对检索结果中的关键词进行高亮

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/68277
推荐阅读
相关标签
  

闽ICP备14008679号