我测试了弹性搜索高亮字段功能,它工作正常。我使用弹性搜索2.4.4 和 spring-data-elasticsearch-2.0.0.RELEASE
示例代码位于以下帖子
How to provide highlighting with Spring data elasticsearch
我最近升级到弹性搜索到5.5.0 和 spring-data-elasticsearch-3.0.0.M4
当我测试相同的代码时,突出显示不会发生
以下是示例代码
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;
}
});
为了突出弹性搜索5.5.0中的字段,是否需要更改代码?
当我在弹性搜索日志中打印查询时,我发现只有一个高亮字段被传递给弹性搜索
{
"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有两个突出显示的字段,但发送到Elastic Search的最终查询只请求一个突出显示字段。
答案 0 :(得分:0)
通过更改 org.springframework.data.elasticsearch.core.ElasticsearchTemplate.doSearch
中的以下代码使其成功现有代码
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);
}