@Testpublic void shouldReturnHighlightedFieldsForGivenQueryAndFields() { //given String documentId = randomNumeric(5); String actualMessage = "some test message"; String highlightedMessage = "some test 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(); PagesampleEntities = elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class, new SearchResultMapper() { @Override public Page mapResults(SearchResponse response, Class clazz, Pageable pageable) { List chunk = new ArrayList (); 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 ((List ) chunk); } return null; } }); assertThat(sampleEntities.getContent().get(0).getHighlightedMessage(), is(highlightedMessage));}