本文介绍了弹性搜索5.5高亮字段不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经测试了弹性搜索突出显示字段功能,并且工作正常。我使用了弹性搜索2.4.4 spring-data-elasticsearch-2.0.0.RELEASE

I have tested the elastic search highlight field function and it was working fine . I used elastic search 2.4.4 and spring-data-elasticsearch-2.0.0.RELEASE

示例代码在下面的帖子中

The sample code is in the below post

我最近已将弹性搜索升级到5.5.0 spring-data-elasticsearch-3.0.0.M4

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

when I test the same code , highlight does not happen

下面是示例代码

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中的字段?

Is there any code change needed in order to highlight fields in elastic search 5.5.0 ?

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

When I print the query in the elastic search log , I find that only one highlight field is passed to Elastic Search

{
   "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的最终查询仅请求一个突出显示字段。

When I debugged , I observed that NativeSearchQuery has two highlighted fields , but the final query sent to Elastic Search has only request for one highlight field.

推荐答案

通过在 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);
        }

这篇关于弹性搜索5.5高亮字段不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 21:55