spring-data-elasticsearch , Aggregation Support for Spring Data Elastic Search

Aggregation Support for Spring Data Elastic Search


Tag: spring-data-elasticsearch

Elastic search has deprecated Facets and recommend to use Aggregations (http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-aggregations.html) .

Is Spring Data Elastic Search supports this currently ?

If Yes, is there any Samples available ?


Yes aggregation is supported.

Example :

    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.refresh(ArticleEntity.class, true);

    SearchQuery searchQuery = new NativeSearchQueryBuilder()
    // when
    Aggregations aggregations = elasticsearchTemplate.query(searchQuery, new ResultsExtractor<Aggregations>() {
        public Aggregations extract(SearchResponse response) {
            return response.getAggregations();
    // then
    assertThat(aggregations, is(notNullValue()));
    assertThat(aggregations.asMap().get("subjects"), is(notNullValue()));

code is copied from ElasticsearchTemplateAggregationTests.java


Spring-data-elasticsearch nested query not working

Person.java @Document(indexName = "person", type = "user") public class Person { @Id private String id; @Field(type = FieldType.String) private String name; @Field(type = FieldType.Nested) private List<Car> cars; // getter/setter } Car.java public class Car { @Field(type = FieldType.String) private String name; @Field(type = FieldType.String) private String model; // getter/setter }...

Elasticsearch NumberFormatException when running two consecutive java tests

I have two test in a class, each of them containing the following query: SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withFilter(rangeFilter("publishDate").lt(date)).build(); In one of the tests, the number of the results elasticsearchTemplate.count(searchQuery, Article.class), in the other one the returned values are verified elasticsearchTemplate.queryForPage(searchQuery,Article.class) If I run any of these two tests separately,...

Elasticsearch multiple values match without analyzer

Pardon my knowledge on ElasticSearch. I have an Elasticsearch collection which has documents like these: { "date": "2013-12-30T00:00:00.000Z", "value": 2, "dimensions": { "region": "Coimbra District" } } { "date": "2013-12-30T00:00:00.000Z", "value": 1, "dimensions": { "region": "Federal District" } } { "date": "2013-12-30T00:00:00.000Z", "value": 1, "dimensions": { "region": "Masovian Voivodeship" }...

Elasticsearch Aggregation by Day of Week and Hour of Day

I have documents of type: [{"msg":"hello", date: "some-date"},{"msg":"hi!", date: "some-date"}, ... I want to have the count of documents by day of week. For example x messages were sent on Monday and y were sent on Tuesday and so on. I have used date_histogram with aggregation but it returns me...

Spring data ElastiSearch aggregations with filters

I am trying to perform aggregations on values filtered by some conditions. I am using ElasticSearchTemplate.query() method of spring data too execute query and get the results in result extractor. I am getting the hits correctly (i.e. filters are applied and docs matching those values are only retrieved.). However, aggregations...

Aggregation Support for Spring Data Elastic Search

Elastic search has deprecated Facets and recommend to use Aggregations (http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-aggregations.html) . Is Spring Data Elastic Search supports this currently ? If Yes, is there any Samples available ?...

How to enable query logging in Spring-data-elasticsearch

I use spring-data-elasticsearch framework to get query result from elasticsearch server, the java code like this: SearchQuery searchQuery = new NativeSearchQueryBuilder() .withQuery(matchAllQuery()).withSearchType(SearchType.COUNT) .addAggregation(new MinBuilder("min_createDate").field("createDate")) .build(); List<Entity> list = template.queryForList(searchQuery, Entity.class); While how can I know the raw http query sent to elasticssearch server? How can I enable the logging, I...

Obtaining string query (JSON) from SearchQuery object

For debugging purposes, I need to know what query spring-data-elasticsearch is sending to the ElasticSearch cluster. I have tried to call the toString method on the SearchQuery object, and doesn't return what I need. What I am doing in Java (using spring-data-elasticsearch) is: private FilterBuilder getFilterBuilder(String id) { return orFilter(...

Spring data elasticSearch returns null with findOne

I'm testing Spring data with elasticSearch. The ES server is running on a remote server in tha same room. I have one index created a day, under an alias. I'm trying to find a simple tweet. But when I try a findOne(), it doesn't seem to work because it returns...