elasticsearchspring-dataspring-data-elasticsearch

How to provide highlighting with Spring data elasticsearch


it seems that SpringData ES don't provide classes to fetch highlights returned by ES. Spring Data can return Lists of Objects but the highlights sections in the Json returned by ES is in a separated part that is not handled by the "ElasticSearchTemplate" class.

Code example :-

QueryBuilder query = QueryBuilders.matchQuery("name","tom"); 
SearchQuery searchQuery =new NativeSearchQueryBuilder().withQuery(query).
                               with HighlightFields(new Field("name")).build();
List<ESDocument> publications = elasticsearchTemplate.queryForList
                                                (searchQuery, ESDocument.class);

I might be wrong, but I can't figure out to do only with SpringDataES. Someone can post an example of how we can get highlights with Spring Data ES ?

Thanks in advance !


Solution

  • From the test cases in spring data elasticsearch I've found solution to this :

    This can be helpful.

    @Test
    public void shouldReturnHighlightedFieldsForGivenQueryAndFields() {
    
        //given
        String documentId = randomNumeric(5);
        String actualMessage = "some test message";
        String highlightedMessage = "some <em>test</em> message";
    
        SampleEntity sampleEntity = SampleEntity.builder().id(documentId)
                .message(actualMessage)
                .version(System.currentTimeMillis()).build();
    
        IndexQuery indexQuery = getIndexQuery(sampleEntity);
    
        elasticsearchTemplate.index(indexQuery);
        elasticsearchTemplate.refresh(SampleEntity.class);
    
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(termQuery("message", "test"))
                .withHighlightFields(new HighlightBuilder.Field("message"))
                .build();
    
        Page<SampleEntity> sampleEntities = elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class, new SearchResultMapper() {
            @Override
            public <T> Page<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
                List<SampleEntity> chunk = new ArrayList<SampleEntity>();
                for (SearchHit searchHit : response.getHits()) {
                    if (response.getHits().getHits().length <= 0) {
                        return null;
                    }
                    SampleEntity user = new SampleEntity();
                    user.setId(searchHit.getId());
                    user.setMessage((String) searchHit.getSource().get("message"));
                    user.setHighlightedMessage(searchHit.getHighlightFields().get("message").fragments()[0].toString());
                    chunk.add(user);
                }
                if (chunk.size() > 0) {
                    return new PageImpl<T>((List<T>) chunk);
                }
                return null;
            }
        });
    
        assertThat(sampleEntities.getContent().get(0).getHighlightedMessage(), is(highlightedMessage));
    }