2
votes

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

How to provide highlighting with Spring data elasticsearch

I have recently upgraded to elastic search to 5.5.0 and spring-data-elasticsearch-3.0.0.M4

when I test the same code , highlight does not happen

Below is the sample code

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;
                    }
                });

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>"
            ]
         }
      }
   }
}

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.

1
Can you add the current mapping ?dadoonet
updated the questionlives
Sounds like a but in the way Spring translates the highlight part to JSON. Any chance you can show what the generated JSon is? May be with slowqueries debug on elasticsearch side?dadoonet
I tried setting this parameter -Des.logger.level=DEBUG while starting elastic search , but I dont get to see json logs . How can I enable logs in elastic search ?lives

1 Answers

0
votes

Made it work by changing the below code in org.springframework.data.elasticsearch.core.ElasticsearchTemplate.doSearch

Existing Code

if (searchQuery.getHighlightFields() != null) {
            for (HighlightBuilder.Field highlightField : searchQuery.getHighlightFields()) {
                searchRequest.highlighter(new HighlightBuilder().field(highlightField));
            }

        }

Modified Code

if (searchQuery.getHighlightFields() != null) {
            HighlightBuilder myBuilder = new HighlightBuilder();
            for (HighlightBuilder.Field highlightField : searchQuery.getHighlightFields()) {
                myBuilder.field(highlightField);

            }
            searchRequest.highlighter(myBuilder);
        }