当前位置:   article > 正文

JAVA代码实现ElasticSearch搜索(入门-进阶)(一):搜索方法、多字段查询、高亮展示_searchsourcebuilder 查询多个关键字

searchsourcebuilder 查询多个关键字

一、搜索方法对比

首先存入一条数据count=" i like eating and kuing" 默认分词器应该将内容分为 “i” “like” “eating” “and” “kuing”

1.QueryBuilders.matchQuery("count",count);

会将搜索词分词,再与目标查询字段进行匹配,若分词中的任意一个词与目标字段匹配上,则可查询到。

  1. count= "i" 可查出
  2. count= "i li" 可查出
  3. count= "i like" 可查出
  4. count= "i like eat" 可查出
  5. count= "and" 可查出
  6. count= "kuing" 可查出
  1. count= "ku" 查不出
  2. count= "li" 查不出
  3. count= "eat" 查不出

2.QueryBuilders.matchPhraseQuery("count",count)

默认使用 match_phrase 时会精确匹配查询的短语,需要全部单词和顺序要完全一样,标点符号除外。(一般用于内容模糊查询)

  1. count= "i" 可查出
  2. count= "i li" 查不出
  3. count= "i like" 可查出
  4. count= "i like eat" 查不出
  5. count= "and" 可查出
  6. count= "kuing" 可查出
  1. count= "ku" 查不出
  2. count= "li" 查不出
  3. count= "eat" 查不出

3.QueryBuilders.matchPhrasePrefixQuery("count",count)

  1. count= "i" 可查出
  2. count= "i li" 可查出
  3. count= "i like" 可查出
  4. count= "i like eat" 可查出
  5. count= "and" 可查出
  6. count= "kuing" 可查出
  1. ****count= "ku" 可查出***
  2. **count= "li" 可查出
  3. count= "eat" 可查出***

4.QueryBuilders.termQuery("count",count)

term query,输入的查询内容是什么,就会按照什么去查询,并不会解析查询内容,对它分词。(一般用于关键字精确查询)

  1. count= "i" 可查出
  2. count = "i li" 查不出
  3. count= "i like" 查不出
  4. count= "i like eat" 查不出
  5. count= "and" 可查出
  6. count= "kuing" 可查出
  1. ****count= "ku" 查不出***
  2. **count= "li" 查不出
  3. count= "eat" 查不出***

 二、多字段搜索

1.BoolQueryBuilder

  1. BoolQueryBuilder qb = QueryBuilders.boolQuery();
  2. 1,qb.must(QueryBuilder queryBuilder); // 相当于and
  3. 2,qb.mustNot(QueryBuilder queryBuilder) // 相当于 and !=
  4. 3,qb.should(QueryBuilder queryBuilder)) // 相当于 or
  5. 4,qb.filter(QueryBuilder queryBuilder)) // 返回的文档必须满足filter子句的条件,但是不会像must一样,参与计算分值

2.示例代码 

  1. //1.声明搜索客户端
  2. SearchRequest searchRequest = new SearchRequest();
  3. //2.设置索引库的名称
  4. searchRequest.indices("qfedu-course");
  5. //3.声明搜索资源对象,声明搜索的规则对象
  6. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  7. //4设置搜索规则
  8. //4.1QueryBuilders.matchAllQuery() 查询全部数据
  9. searchSourceBuilder .query(QueryBuilders.matchAllQuery());
  10. //4.2 QueryBuilders.termQuery :对搜索关键字不分词进行查询 精准查询
  11. searchSourceBuilder.query(QueryBuilders.termQuery("name","张三"));
  12. //4.2.1 termsQuery: 精准查询,一次匹配多个数据
  13. searchSourceBuilder.query(QueryBuilders.termsQuery("id","25","24"));
  14. //4.3 QueryBuilders.matchQuery(). 关键字分词进行查询
  15. searchSourceBuilder.query(QueryBuilders.matchQuery("courseName","分布式课程入门").operator(Operator.AND));
  16. //4.3.1 QueryBuilders.multiMatchQuery() 关键字匹配多个字段,可以给某一个字段设置 权重,提升的倍数。
  17. searchSourceBuilder.query(QueryBuilders.multiMatchQuery("分布式OK", "courseName", "courseDescription").field("courseName", 10));
  18. //4.4 时间范围搜索 .from(起始时间).to(结束时间)
  19. searchSourceBuilder.query(QueryBuilders.must(QueryBuilders.rangeQuery("commentTime").from("2022-01-02 13:11:00").to("2022-01-02 14:11:00")));
  20. //4.5 QueryBuilders.rangeQuery 范围查询 gte 大于等于 lte 小于等于
  21. searchSourceBuilder.query(QueryBuilders.rangeQuery("pay").gte(1000).lte(2000));
  22. //5.设置分页
  23. searchSourceBuilder.size(realTimeInformationReq.getSize());
  24. searchSourceBuilder.from((realTimeInformationReq.getPage() - 1) * realTimeInformationReq.getSize());
  25. //6.将搜索资源对象设置到搜索客户端中
  26. searchRequest.source(searchSourceBuilder);
  27. //7.进行搜索
  28. SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
  29. //8.解析搜索返回值
  30. SearchHit[] hits = search.getHits().getHits();
  31. //9.查询的总数
  32. long count = search.getHits().getTotalHits().value;
  33. //10.解析返回值
  34. List list = new ArrayList();
  35. for (SearchHit his : hits1) {
  36. Map<String, Object> sourceAsMap = his.getSourceAsMap();
  37. list.add(sourceAsMap);
  38. }

 三、高亮展示

1.工具类

  1. public static String setHighLight(String keywords, String info) {
  2. if (StringUtils.isBlank(keywords)||StringUtils.isBlank(info)) {
  3. return info;
  4. }
  5. String[] keywordArray = keywords.split(" ");
  6. String newInfo = info;
  7. for (String keyword : keywordArray) {
  8. newInfo = newInfo.replace(keyword, "<em style=\"color:red\">" + keyword + "</em>");
  9. }
  10. return newInfo;
  11. }

2.高亮展示

  1. for (TopicAnalysisMsg topicAnalysisMsg :list){
  2. //参数一:需要高亮展示的关键字;参数二:需要高亮展示的字段
  3. topicAnalysisMsg.setTitle(DisplayUtil.setHighLight(topicAnalysisMsg.getKeyword(),topicAnalysisMsg.getTitle()));
  4. topicAnalysisMsg.setAbstracts(DisplayUtil.setHighLight(topicAnalysisMsg.getKeyword(),topicAnalysisMsg.getAbstracts()));
  5. topicAnalysisMsg.setContent(DisplayUtil.setHighLight(topicAnalysisMsg.getKeyword(),topicAnalysisMsg.getContent()));
  6. }

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

闽ICP备14008679号