Elasticsearch5.5高亮字段不起作用

我已经测试了elasticsearch突出显示字段功能,并且工作正常。我使用了 和

我最近将 升级到 和

当我测试相同的代码时,突出显示不会发生

下面是示例代码

SearchQuery searchQuery = new NativeSearchQueryBuilder().withIndices("occindex")

.withPageable(new PageRequest(0, mySpecification.getNoOfRecords()))

.withQuery(QueryBuilders.multiMatchQuery(

searchText.toLowerCase()).field("transformedTitle", 10.0f).

minimumShouldMatch("50%").fuzziness(Fuzziness.ONE).prefixLength(3)

.field("transformedDesription").type(MultiMatchQueryBuilder.Type.BEST_FIELDS))

.withHighlightFields(

new HighlightBuilder.Field("transformedTitle").preTags("<span style='background-color: #FFFF00'>")

.postTags("</span>"),

new HighlightBuilder.Field("transformedDesription").fragmentSize(250).numOfFragments(3)

.preTags("<span style='background-color: #FFFF00'>").postTags("</span>"))

.build();

Page<MyResultRecord> sampleEntities = elasticsearchTemplate.queryForPage(searchQuery,

MyResultRecord.class, new SearchResultMapper() {

@Override

public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {

List<MyResultRecord> chunk = new ArrayList<MyResultRecord>();

for (SearchHit searchHit : response.getHits()) {

if (response.getHits().getHits().length <= 0) {

return null;

}

MyResultRecord myResultRecord = new MyResultRecord();

myResultRecord.setRecordId(searchHit.getId());

Map<String, Object> source = searchHit.getSource();

myResultRecord.setRisk((String) source.get("actualRisk"));

String highlightedTitle = null;

System.out.println( " Check the highlighted fileds " + searchHit.getHighlightFields());

System.out.println( " Is this null ?? " + searchHit.getHighlightFields().get("transformedTitle"));

if (searchHit.getHighlightFields().get("transformedTitle") != null)

highlightedTitle = searchHit.getHighlightFields().get("transformedTitle").fragments()[0]

.toString();

else

highlightedTitle = (String) source.get("transformedTitle");

myResultRecord.setHighlightedTitle(highlightedTitle);

myResultRecord.setScore(searchHit.getScore());

chunk.add(myResultRecord);

}

if (chunk.size() > 0) {

return new AggregatedPageImpl(chunk);

}

return null;

}

});

为了突出显示elasticsearch5.5.0中的字段,是否需要进行任何代码更改?

当我在elasticsearch日志中打印查询时,我发现只有一个突出显示字段传递给elasticsearch

{  

"from":0,

"size":2,

"query":{

"multi_match":{

"query":" My Query String",

"fields":[

"transformedDesription^1.0",

"transformedTitle^1.0"

],

"type":"best_fields",

"operator":"OR",

"slop":0,

"prefix_length":0,

"max_expansions":50,

"lenient":false,

"zero_terms_query":"NONE",

"boost":1.0

}

},

"highlight":{

"fields":{

"transformedDesription":{

"pre_tags":[

"<bold>"

],

"post_tags":[

"</bold>"

]

}

}

}

}

调试时,我发现NativeSearchQuery具有两个突出显示的字段,但是发送到ElasticSearch的最终查询仅请求一个突出显示字段。

回答:

通过更改

*

现有代码

if (searchQuery.getHighlightFields() != null) {

for (HighlightBuilder.Field highlightField : searchQuery.getHighlightFields()) {

searchRequest.highlighter(new HighlightBuilder().field(highlightField));

}

}

修改后的代码

if (searchQuery.getHighlightFields() != null) {

HighlightBuilder myBuilder = new HighlightBuilder();

for (HighlightBuilder.Field highlightField : searchQuery.getHighlightFields()) {

myBuilder.field(highlightField);

}

searchRequest.highlighter(myBuilder);

}

以上是 Elasticsearch5.5高亮字段不起作用 的全部内容, 来源链接: utcz.com/qa/432105.html

回到顶部