Elasticsearch: Aggregation framework - filtering aggregations by nested object fields doesn't work correctly

Created on 14 Dec 2013  路  2Comments  路  Source: elastic/elasticsearch

Here i setup an mapping, queries and result:
https://gist.github.com/darklow/7964005

I also tried different queries, more simplified, without so much "nested" filters and queries (since in mapping i have include_in_parent: true nested is not required), but results all the time were same.

Most helpful comment

Does the following aggregation give back the result that you are expecting?

GET /movies/_search
{
   "query": {
      "nested": {
         "path": "credits",
         "query": {
            "match": {
               "credits.person_id": 1
            }
         }
      }
   },
   "aggs": {
      "credits" : {
          "nested" : {
              "path" : "credits"
          },
          "aggs" : {
              "person_1" : {
                  "filter" : {
                      "term" : {
                          "person_id" : 1
                      }
                  },
                  "aggs" : {
                      "departments" : {
                          "terms" : {
                              "field" : "department"
                          }
                      }
                  }
              }
          }
      }
   }
}

For every match, the aggregator:

  • first goes from the movies to the credits ("credits" aggregation)
  • then filter credits to only keep those that are about person_id: 1 ("person_1" aggregation)
  • finally compute counts of the departments for person_id: 1 ("department" aggregation)

All 2 comments

Does the following aggregation give back the result that you are expecting?

GET /movies/_search
{
   "query": {
      "nested": {
         "path": "credits",
         "query": {
            "match": {
               "credits.person_id": 1
            }
         }
      }
   },
   "aggs": {
      "credits" : {
          "nested" : {
              "path" : "credits"
          },
          "aggs" : {
              "person_1" : {
                  "filter" : {
                      "term" : {
                          "person_id" : 1
                      }
                  },
                  "aggs" : {
                      "departments" : {
                          "terms" : {
                              "field" : "department"
                          }
                      }
                  }
              }
          }
      }
   }
}

For every match, the aggregator:

  • first goes from the movies to the credits ("credits" aggregation)
  • then filter credits to only keep those that are about person_id: 1 ("person_1" aggregation)
  • finally compute counts of the departments for person_id: 1 ("department" aggregation)

This resolved my issue and returned correct results. Apparently i used wrong syntax.
Thank you for explaining and giving full example. New aggregation feature is great!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ttaranov picture ttaranov  路  3Comments

Praveen82 picture Praveen82  路  3Comments

rjernst picture rjernst  路  3Comments

matthughes picture matthughes  路  3Comments

abtpst picture abtpst  路  3Comments