弹性搜索已弃用Facets并建议使用聚合(http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-aggregations.html)。弹性数据弹性搜索的聚合支持
Spring Data Elastic Search目前是否支持这个功能?
如果是,是否有任何样品可用?
弹性搜索已弃用Facets并建议使用聚合(http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-aggregations.html)。弹性数据弹性搜索的聚合支持
Spring Data Elastic Search目前是否支持这个功能?
如果是,是否有任何样品可用?
是支持聚合。
实施例:
@Test
public void shouldReturnAggregatedResponseForGivenSearchQuery() {
// given
IndexQuery article1 = new ArticleEntityBuilder("1").title("article four").subject("computing").addAuthor(RIZWAN_IDREES).addAuthor(ARTUR_KONCZAK).addAuthor(MOHSIN_HUSEN).addAuthor(JONATHAN_YAN).score(10).buildIndex();
IndexQuery article2 = new ArticleEntityBuilder("2").title("article three").subject("computing").addAuthor(RIZWAN_IDREES).addAuthor(ARTUR_KONCZAK).addAuthor(MOHSIN_HUSEN).addPublishedYear(YEAR_2000).score(20).buildIndex();
IndexQuery article3 = new ArticleEntityBuilder("3").title("article two").subject("computing").addAuthor(RIZWAN_IDREES).addAuthor(ARTUR_KONCZAK).addPublishedYear(YEAR_2001).addPublishedYear(YEAR_2000).score(30).buildIndex();
IndexQuery article4 = new ArticleEntityBuilder("4").title("article one").subject("accounting").addAuthor(RIZWAN_IDREES).addPublishedYear(YEAR_2002).addPublishedYear(YEAR_2001).addPublishedYear(YEAR_2000).score(40).buildIndex();
elasticsearchTemplate.index(article1);
elasticsearchTemplate.index(article2);
elasticsearchTemplate.index(article3);
elasticsearchTemplate.index(article4);
elasticsearchTemplate.refresh(ArticleEntity.class, true);
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchAllQuery())
.withSearchType(COUNT)
.withIndices("articles").withTypes("article")
.addAggregation(terms("subjects").field("subject"))
.build();
// when
Aggregations aggregations = elasticsearchTemplate.query(searchQuery, new ResultsExtractor<Aggregations>() {
@Override
public Aggregations extract(SearchResponse response) {
return response.getAggregations();
}
});
// then
assertThat(aggregations, is(notNullValue()));
assertThat(aggregations.asMap().get("subjects"), is(notNullValue()));
}
复制为了防止第二次调用elasticsearch就可以直接提取serachResult。
elasticsearchTemplate.query(query.build(), new ResultsExtractor<Object>() {
@Override
public Object extract(SearchResponse searchResponse) {
Aggregations aggregations = searchResponse.getAggregations();
List<AnyClass> ta = new DefaultResultMapper().mapResults(searchResponse, AnyClass.class, new PageRequest(page != null ? page : 0, 15)).getContent();
return ta;
}
});
我已经使用过相同的部分,但提取聚合部分需要相当长的时间,因此我的响应时间正在增加 –