BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
{
BoolQueryBuilder Builder = QueryBuilders.boolQuery();
Builder.should(QueryBuilders.termQuery("isvalid", 1));
boolBuilder.must(Builder);
}
{
// 全文检索
QueryStringQueryBuilder title = QueryBuilders.queryStringQuery(fullSearch);
title.field("standard_name.text");
boolBuilder.must(title);
}
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(boolBuilder);
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.field("standard_name.text");//高亮的字段
highlightBuilder.preTags("<span style='color:green'>");//前缀后缀
highlightBuilder.postTags("</span>");
// highlightBuilder.requireFieldMatch(true);//是否多个字段都高亮
// highlightBuilder.numOfFragments(0);
sourceBuilder.highlighter(highlightBuilder);
sourceBuilder.explain(true);
sourceBuilder.from(0);
sourceBuilder.size(250);
SearchRequest searchRequest = new SearchRequest(modelStandardRelationIndex);
searchRequest.source(sourceBuilder);
response = EsClientUtil.getClient().search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
SearchHit[] searchHits = hits.getHits();
for (SearchHit hit : searchHits) {
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
ModelStandardEsVo bean = new ModelStandardEsVo();
bean.setId((String) sourceAsMap.get("id"));
bean.setModelChineseName((String) sourceAsMap.get("model_chinese_name"));
bean.setModelIdentification((String) sourceAsMap.get("model_identification"));
bean.setModelVersion((String) sourceAsMap.get("model_version"));
bean.setStandardIdentification((String) sourceAsMap.get("standard_identification"));
bean.setStandardVersion((String) sourceAsMap.get("standard_version"));
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
{
HighlightField field = highlightFields.get("standard_name.text");
if (field != null) {
Text[] fragments = field.fragments();
String n_field = "";
for (Text fragment : fragments) {
n_field += fragment;
}
//高亮标题覆盖原标题
sourceAsMap.put("standard_name", n_field);
}
}
}