Instantly share code, notes, and snippets.

Embed
What would you like to do?
public List<ElasticsearchPost> dismaxSearch(String value, int start, int size, float minScore) {
QueryBuilder queryBuilder = getQueryBuilder(value);
Client client = elasticsearchTemplate.getClient();
SearchResponse response = client.prepareSearch()
.setQuery(queryBuilder)
.setSize(size)
.setFrom(start)
.setMinScore(minScore)
.addSort("_score", SortOrder.DESC)
.setExplain(true)
.execute()
.actionGet();
List<SearchHit> searchHits = Arrays.asList(response.getHits().getHits());
ObjectMapper mapper = new ObjectMapper();
List<ElasticsearchPost> elasticsearchPosts = new ArrayList<>();
searchHits.forEach(hit -> {
try {
elasticsearchPosts.add(mapper.readValue(hit.getSourceAsString(), ElasticsearchPost.class));
elasticsearchPosts.get(elasticsearchPosts.size() - 1).setScore(hit.getScore());
} catch (IOException e) {
e.printStackTrace();
}
});
return elasticsearchPosts;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment