2

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.

lives
  • 1,243
  • 5
  • 25
  • 61
  • Can you add the current mapping ? – dadoonet Oct 15 '17 at 08:02
  • updated the question – lives Oct 15 '17 at 08:15
  • 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 Oct 15 '17 at 08:23
  • 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 Oct 17 '17 at 12:50
  • https://www.elastic.co/guide/en/elasticsearch/reference/5.6/index-modules-slowlog.html could help – dadoonet Oct 17 '17 at 13:08
  • Apologies dadoonet - I am not sure where the problem is . When I debugged SearchQuery , I am able to see two highlighted fields , but the final query sent to Elastic Search has only one highlighted field – lives Oct 18 '17 at 11:33
  • So that's a spring data issue. I think you should open an issue in their repository. – dadoonet Oct 18 '17 at 11:49
  • Raised JIRA https://jira.spring.io/browse/DATAES-412 – lives Oct 27 '17 at 22:05
  • Your analysis seems correct to me. Well done – dadoonet Oct 27 '17 at 22:33

1 Answers1

0

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);
        }
lives
  • 1,243
  • 5
  • 25
  • 61