本文主要是介绍elasticsearch 简单实现多字段组合查询,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/*** 多字段组合查询* @param type * @param fieldName1 字段1* @param fieldName2 字段2* @param keyword 关键字* @param start* @param count* @return*/public static Map<String,Object> searchQueary(String type, String fieldName1,String fieldName2, String keyword, int start, int count) {Map<String,Object> map = new HashMap<>(2);try {SearchRequest searchRequest = new SearchRequest(indexName);SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();// 自己写的方法判断是否为空// 可看我 [另一个博客](https://blog.csdn.net/SuperChen12356/article/details/115948830)if (!Util.isEmpty(keyword)){// 排序按照 fieldName1 fieldName2 的查询顺序,1的数据在2 的前面MatchQueryBuilder matchQueryBuilder1 = QueryBuilders.matchQuery(fieldName1, keyword);matchQueryBuilder1.fuzziness(Fuzziness.AUTO);MatchQueryBuilder matchQueryBuilder2 = QueryBuilders.matchQuery(fieldName2, keyword);matchQueryBuilder2.fuzziness(Fuzziness.AUTO);BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();boolBuilder.should(matchQueryBuilder1);boolBuilder.should(matchQueryBuilder2);sourceBuilder.query(boolBuilder);}//高亮HighlightBuilder highlightBuilder = new HighlightBuilder();highlightBuilder.field(fieldName1);highlightBuilder.requireFieldMatch(false);//多个高亮显示highlightBuilder.preTags("<span style=\"color:red\">");highlightBuilder.postTags("</span>");sourceBuilder.highlighter(highlightBuilder);//第几页sourceBuilder.from(start);//第几条sourceBuilder.size(count);// 总条数10000条限制sourceBuilder.trackTotalHits(true);//匹配度从高到低sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));searchRequest.source(sourceBuilder);searchRequest.types(type);SearchResponse searchResponse = client.search(searchRequest,RequestOptions.DEFAULT);SearchHits hits = searchResponse.getHits();// 总条数long totalHits = hits.getTotalHits();//System.out.println(totalHits+"------------------------------");List<Map<String, Object>> matchRsult = new LinkedList<Map<String, Object>>();for (SearchHit hit : hits.getHits()){//获取高亮字段Map<String, HighlightField> highlightFields = hit.getHighlightFields();HighlightField title = highlightFields.get(fieldName1);//原来的结果Map<String, Object> sourceAsMap = hit.getSourceAsMap();//解析高亮字段,将原来的字段换为我们高亮字段即可if(title!=null){Text[] fragments = title.fragments();String n_title="";for(Text text:fragments){n_title += text;}//高亮字段替换原来的内容sourceAsMap.put(fieldName1,n_title);}matchRsult.add(hit.getSourceAsMap());}map.put("list",matchRsult);map.put("count",totalHits);}catch (Exception e){e.printStackTrace();}return map;}
多字段 组合查询,我这里用的是should,试过must没有成功,有大神可以帮忙解决一下。
// 排序按照 fieldName1 fieldName2 的查询顺序,1的数据在2 的前面//关键字匹配对应字段MatchQueryBuilder matchQueryBuilder1 = QueryBuilders.matchQuery(fieldName1, keyword);//模糊匹配matchQueryBuilder1.fuzziness(Fuzziness.AUTO);//关键字匹配对应字段MatchQueryBuilder matchQueryBuilder2 = QueryBuilders.matchQuery(fieldName2, keyword);//模糊匹配matchQueryBuilder2.fuzziness(Fuzziness.AUTO);BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();boolBuilder.should(matchQueryBuilder1);boolBuilder.should(matchQueryBuilder2);sourceBuilder.query(boolBuilder);
这篇关于elasticsearch 简单实现多字段组合查询的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!