当前位置:   article > 正文

ElasticSearch学习5-- 使用RestClient查询文档_elasticsearchclient.search

elasticsearchclient.search

1、查询基本步骤

1、创建SearchRequest对象
2、准备Request.source(),也就是DSL
        QueryBuilders来构建查询条件
       传入Request.source() 的 query() 方法
3、发送请求,得到结果
4、解析结果(参考JSON结果,从外到内,逐层解析)

RestAPI中其中构建DSL是通过HighLevelRestClient中的resource()返回的SearchSourceBuilder来实现的,其中包含了查询、排序、分页、高亮等所有功能

SearchSourceBuilder 进行查询、排序、分页、高亮

RestAPI中其中构建查询条件的核心部分是由一个名为QueryBuilders的工具类提供的,其中包含了各种查询方法。

QueryBuilders 进行match_all /match /multi_match /term /range/boolmatch等查询

查询所有

  1. @Test
  2. public void testMatchAll() throws IOException {
  3. //创建查询请求,指定要查询的索引库
  4. SearchRequest request = new SearchRequest("case_management");
  5. //构建查询条件,下边是所有查询
  6. MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
  7. //将查询条件赋值给查询语句
  8. request.source().query(matchAllQueryBuilder);
  9. //进行查询
  10. SearchResponse response = client.search(request, RequestOptions.DEFAULT);
  11. //对结果进行处理
  12. SearchHits hits = response.getHits();
  13. //返回结果列表
  14. SearchHit[] hitsList = hits.getHits();
  15. //总数
  16. long totalHits = hits.getTotalHits();
  17. System.out.println(totalHits);
  18. SearchHit documentFields = hitsList[0];
  19. String json = documentFields.getSourceAsString();
  20. System.out.println(json);
  21. }

2、多种检索查询

match、multi_match查询

  1. @Test
  2. public void testMatchQuery() throws IOException {
  3. //创建查询请求,指定要查询的索引库
  4. SearchRequest request = new SearchRequest("case_management");
  5. //构建查询条件,下边是所有查询
  6. MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("fileId","赎吧");
  7. //将查询条件赋值给查询语句
  8. request.source().query(queryBuilder);
  9. //进行查询
  10. SearchResponse response = client.search(request, RequestOptions.DEFAULT);
  11. //对结果进行处理
  12. SearchHits hits = response.getHits();
  13. //返回结果列表
  14. SearchHit[] hitsList = hits.getHits();
  15. //总数
  16. long totalHits = hits.getTotalHits();
  17. System.out.println(totalHits);
  18. SearchHit documentFields = hitsList[0];
  19. String json = documentFields.getSourceAsString();
  20. System.out.println(json);
  21. }
  1. @Test
  2. public void testMultiMatchQuery() throws IOException {
  3. //创建查询请求,指定要查询的索引库
  4. SearchRequest request = new SearchRequest("case_management");
  5. //构建查询条件,下边是所有查询
  6. MultiMatchQueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("B端赎吧","fileId","caseDes");
  7. //将查询条件赋值给查询语句
  8. request.source().query(queryBuilder);
  9. //进行查询
  10. SearchResponse response = client.search(request, RequestOptions.DEFAULT);
  11. //对结果进行处理
  12. SearchHits hits = response.getHits();
  13. //返回结果列表
  14. SearchHit[] hitsList = hits.getHits();
  15. //总数
  16. long totalHits = hits.getTotalHits();
  17. System.out.println(totalHits);
  18. SearchHit documentFields = hitsList[0];
  19. String json = documentFields.getSourceAsString();
  20. System.out.println(json);
  21. }

term 、range查询

  1. @Test
  2. public void testTermQuery() throws IOException {
  3. //创建查询请求,指定要查询的索引库
  4. SearchRequest request = new SearchRequest("case_management");
  5. //构建查询条件,下边是所有查询
  6. TermQueryBuilder queryBuilder = QueryBuilders.termQuery("oa","lilan04");
  7. //将查询条件赋值给查询语句
  8. request.source().query(queryBuilder);
  9. //进行查询
  10. SearchResponse response = client.search(request, RequestOptions.DEFAULT);
  11. //对结果进行处理
  12. SearchHits hits = response.getHits();
  13. //返回结果列表
  14. SearchHit[] hitsList = hits.getHits();
  15. //总数
  16. long totalHits = hits.getTotalHits();
  17. System.out.println(totalHits);
  18. SearchHit documentFields = hitsList[0];
  19. String json = documentFields.getSourceAsString();
  20. System.out.println(json);
  21. }
  22. @Test
  23. public void testRangeQuery() throws IOException {
  24. //创建查询请求,指定要查询的索引库
  25. SearchRequest request = new SearchRequest("test_1");
  26. //构建查询条件,下边是所有查询
  27. RangeQueryBuilder queryBuilder = QueryBuilders.rangeQuery("age").gte(10).lte(20);
  28. //将查询条件赋值给查询语句
  29. request.source().query(queryBuilder);
  30. //进行查询
  31. SearchResponse response = client.search(request, RequestOptions.DEFAULT);
  32. //对结果进行处理
  33. SearchHits hits = response.getHits();
  34. //返回结果列表
  35. SearchHit[] hitsList = hits.getHits();
  36. //总数
  37. long totalHits = hits.getTotalHits();
  38. System.out.println(totalHits);
  39. SearchHit documentFields = hitsList[0];
  40. String json = documentFields.getSourceAsString();
  41. System.out.println(json);
  42. }

布尔查询

  1. @Test
  2. public void testBooleanQuery() throws IOException {
  3. //创建查询请求,指定要查询的索引库
  4. SearchRequest request = new SearchRequest("case_management");
  5. //构建查询条件,下边是所有查询
  6. BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
  7. queryBuilder.must(QueryBuilders.matchQuery("oa","lilan04"));
  8. queryBuilder.should(QueryBuilders.rangeQuery("editTime").gte(1684740000000L).lte(1684740070219l));
  9. queryBuilder.filter(QueryBuilders.termQuery("isDelete",1));
  10. //将查询条件赋值给查询语句
  11. request.source().query(queryBuilder);
  12. //进行查询
  13. SearchResponse response = client.search(request, RequestOptions.DEFAULT);
  14. //对结果进行处理
  15. SearchHits hits = response.getHits();
  16. //返回结果列表
  17. SearchHit[] hitsList = hits.getHits();
  18. //总数
  19. long totalHits = hits.getTotalHits();
  20. System.out.println(totalHits);
  21. SearchHit documentFields = hitsList[0];
  22. String json = documentFields.getSourceAsString();
  23. System.out.println(json);
  24. }

分页、排序、高亮查询

  1. @Test
  2. public void testSortQuery() throws IOException {
  3. //创建查询请求,指定要查询的索引库
  4. SearchRequest request = new SearchRequest("case_management");
  5. //构建查询条件,下边是布尔查询
  6. BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
  7. queryBuilder.must(QueryBuilders.matchQuery("oa","lilan04"));
  8. queryBuilder.should(QueryBuilders.rangeQuery("editTime").gte(1684740000000L).lte(1684740070219l));
  9. queryBuilder.filter(QueryBuilders.termQuery("isDelete",1));
  10. //将查询条件赋值给查询语句
  11. request.source().query(queryBuilder);
  12. //排序
  13. request.source().sort("editTime", SortOrder.ASC);
  14. //分页
  15. request.source().from(1).size(10);
  16. //进行查询
  17. SearchResponse response = client.search(request, RequestOptions.DEFAULT);
  18. //对结果进行处理
  19. SearchHits hits = response.getHits();
  20. //返回结果列表
  21. SearchHit[] hitsList = hits.getHits();
  22. //总数
  23. long totalHits = hits.getTotalHits();
  24. System.out.println(totalHits);
  25. SearchHit documentFields = hitsList[0];
  26. String json = documentFields.getSourceAsString();
  27. System.out.println(json);
  28. }
  29. @Test
  30. public void testHightLightQuery() throws IOException {
  31. //创建查询请求,指定要查询的索引库
  32. SearchRequest request = new SearchRequest("case_management");
  33. //构建查询条件,下边是布尔查询
  34. BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
  35. queryBuilder.must(QueryBuilders.matchQuery("oa","lilan04"));
  36. queryBuilder.should(QueryBuilders.rangeQuery("editTime").gte(1684740000000L).lte(1684740070219l));
  37. queryBuilder.filter(QueryBuilders.termQuery("isDelete",1));
  38. //将查询条件赋值给查询语句
  39. request.source().query(queryBuilder);
  40. //自定义高亮 查找
  41. HighlightBuilder highlightBuilder = new HighlightBuilder();
  42. highlightBuilder.preTags("<font color='red'>");
  43. highlightBuilder.postTags("</font>");
  44. highlightBuilder.field("oa");
  45. // highlightBuilder.requireFieldMatch(false); //多字段时,需要设置为false
  46. // request.source().highlighter(highlightBuilder);
  47. //排序
  48. request.source().sort("editTime", SortOrder.ASC);
  49. //分页
  50. request.source().from(1).size(10);
  51. //进行查询
  52. SearchResponse response = client.search(request, RequestOptions.DEFAULT);
  53. System.out.println("respon话费时间 " + response.getTook());
  54. //对结果进行处理
  55. SearchHits hits = response.getHits();
  56. //返回结果列表
  57. SearchHit[] hitsList = hits.getHits();
  58. //总数
  59. long totalHits = hits.getTotalHits();
  60. System.out.println(totalHits);
  61. SearchHit documentFields = hitsList[0];
  62. String json = documentFields.getSourceAsString();
  63. System.out.println(json);
  64. //获取到高亮字段
  65. System.out.println(documentFields.getHighlightFields());
  66. }

3、错误解决

场景

    ES对text类型的字段进行聚合操作的时候, 报 Fielddata is disabled on text fields by default. Set fielddata=true on [make] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.

分析

    官方文档
    sort, aggregate, or access values from a script on a text field 这些行为会需要从内存中加载数据, 但是text类型的数据可能会消耗非常多内存, 另外可能会导致查询延迟, 所以默认不允许对text字段这样操作
所以最好对非text类型字段进行排序等操作

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

闽ICP备14008679号