Aggregation Support for Spring Data Elastic Search -
elastic search has deprecated facets , recommend use aggregations (http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-aggregations.html) .
is spring data elastic search supports ?
if yes, there samples available ?
yes aggregation supported.
example :
@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(); } }); // assertthat(aggregations, is(notnullvalue())); assertthat(aggregations.asmap().get("subjects"), is(notnullvalue())); }
code copied elasticsearchtemplateaggregationtests.java
Comments
Post a Comment