主要原因是,需要设置高亮的片数;这里直接看最后的代码片段即可
public NativeSearchQuery getNativeSearchQuery(ProcessLog condition, PageParam pageParam) {
Sort sortBy = Sort.by(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, pageParam.getSortCondition())).descending();
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
if (StringUtils.equalsIgnoreCase("asc", pageParam.getSortModel())) {
sortBy = sortBy.ascending();
}
Pageable pageable = PageRequest.of(pageParam.getPageNum() - 1, pageParam.getPageSize(), sortBy);
if (StringUtils.isEmpty(pageParam.getSortCondition())) {
pageParam.setSortCondition("requestTime");
}
BoolQueryBuilder boolQueryBuilder = boolQuery();
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
if (StringUtils.isNotEmpty(condition.getUser())) {
boolQueryBuilder.must(matchPhraseQuery("user", condition.getUser()));
}
if (StringUtils.isNotEmpty(condition.getRequestUri())) {
boolQueryBuilder.must(matchPhraseQuery("requestUri", condition.getRequestUri()));
}
if (StringUtils.isNotEmpty(condition.getParams())) {
String params = condition.getParams();
String[] paramArray = params.split(",");
for (String param : paramArray) {
boolQueryBuilder.must(matchPhraseQuery("params", param));
}
}
if (StringUtils.isNotEmpty(condition.getEmail())) {
boolQueryBuilder.must(matchPhraseQuery("email", condition.getEmail()));
}
if (StringUtils.isNotEmpty(condition.getPeerId())) {
boolQueryBuilder.must(matchPhraseQuery("peerId", condition.getPeerId()));
}
if (StringUtils.isNotEmpty(condition.getServerName())) {
boolQueryBuilder.must(matchPhraseQuery("serverName.keyword", condition.getServerName()));
}
if (condition.getStatus() != null) {
boolQueryBuilder.must(matchPhraseQuery("status", condition.getStatus()));
}
if (StringUtils.isNotEmpty(condition.getMethod())) {
boolQueryBuilder.must(matchPhraseQuery("method", condition.getMethod()));
}
if (condition.getStartTime() != null && condition.getEndTime() != null) {
boolQueryBuilder.must(
rangeQuery("requestTime")
.gte(condition.getStartTime())
.lt(condition.getEndTime())
);
}
nativeSearchQueryBuilder.withQuery(boolQueryBuilder);
nativeSearchQueryBuilder.withHighlightBuilder(new HighlightBuilder()
.field("id")
.field("email")
.field("serverName")
.field("method")
.field("requestUri")
.field("params")
.field("peerId")
.field("status")
.preTags("<span style=\'color:red\'>")
.postTags("</span>")
//注意加上高亮片数即可
.fragmentSize(800000)
//第一个分片获取高亮片段
.numOfFragments(0)
.requireFieldMatch(false))
.withPageable(pageable);
return nativeSearchQueryBuilder.build();
}
标签:boolQueryBuilder,高亮,matchPhraseQuery,field,ElasticSearch,文本,StringUtils,conditio
From: https://www.cnblogs.com/itcastwzp/p/16842777.html