Search code examples
elasticsearchlucenespring-dataelasticsearch-highlight

spring data elastic search highlight text


I am using Spring Data Elastic Search for querying from elastic search DB.

I need to highlight and display the searched text in the search results.

I have my setting like below

    SearchQuery searchQuery = new NativeSearchQueryBuilder()
        .matchQuery("title", "Damaged"))
        .withHighlightFields(new HighlightBuilder.Field("Damaged").preTags("<em>").postTags("</em>").fragmentSize(250))
        .build();

FacetedPage<Result> results = myRepository.search(searchQuery); 

But the data that is returned as search results is not highlighted .


Solution

  • Refer the below link - It works

    How to provide highlighting with Spring data elasticsearch