List of usage examples for org.springframework.data.elasticsearch.core SearchResultMapper SearchResultMapper
SearchResultMapper
From source file:org.bisen.chatamari.service.BlogService.java
@Transactional(readOnly = true) public Page<ElasticBlog> search(String query, Pageable pageable) throws DataAccessException { SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(QueryBuilders.queryString(query)) .withPageable(pageable).withHighlightFields(new HighlightBuilder.Field("strippedContent"), new HighlightBuilder.Field("title")) .build();/*from ww w . j a v a 2 s . c o m*/ Page<ElasticBlog> pageResult = elasticsearchTemplate.queryForPage(searchQuery, ElasticBlog.class, new SearchResultMapper() { @Override public <T> FacetedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) { List<ElasticBlog> blogs = new ArrayList<>(); for (SearchHit hit : response.getHits()) { if (response.getHits().getHits().length <= 0) { return null; } ElasticBlog blog = new ElasticBlog(); blog.setId(Integer.parseInt(hit.getId())); blog.setContent((String) hit.getSource().get("content")); blog.setCreatedByEmail((String) hit.getSource().get("createdByEmail")); blog.setCreatedDate(hit.getSource().get("createdDate") != null ? new Date((long) hit.getSource().get("createdDate")) : null); blog.setLastModifiedByEmail((String) hit.getSource().get("createdModifiedEmail")); blog.setLastModifiedDate(hit.getSource().get("lastModifiedDate") != null ? new Date((long) hit.getSource().get("lastModifiedDate")) : null); blog.setTags((ArrayList<String>) hit.getSource().get("tags")); blog.setTitle((String) hit.getSource().get("title")); if (hit.getHighlightFields() != null) { if (hit.getHighlightFields().get("strippedContent") != null) { blog.setHighlightedString( hit.getHighlightFields().get("strippedContent").fragments()[0] .toString()); } else { blog.setHighlightedString( hit.getHighlightFields().get("title").fragments()[0].toString()); } } blogs.add(blog); } if (blogs.size() > 0) { FacetedPage<T> page = new FacetedPageImpl<>((List<T>) blogs); return page; } return null; } }); return pageResult; }