gpt4 book ai didi

spring-data-elasticsearch - Spring Data Elastic Search 的聚合支持

转载 作者:行者123 更新时间:2023-12-04 21:52:01 24 4
gpt4 key购买 nike

Elasticsearch 已弃用 Facets 并推荐使用聚合 ( http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-aggregations.html ) 。

Spring Data Elastic Search 目前支持这个吗?

如果是,是否有任何可用的 sample ?

最佳答案

是的,支持聚合。

例子 :

    @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()));
}

代码复制自 ElasticsearchTemplateAggregationTests.java

关于spring-data-elasticsearch - Spring Data Elastic Search 的聚合支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30184764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com