Jasmine
Jasmine

Reputation: 153

sub aggregation with elasticsearch

my index consists of documents like this one

{
  "members": [{
    "name": "James",
    "language": "english"
  }],
  "signupdate": "1/1/2016"
}

I want to get the top languages and their sign up date histogram using sub aggregation

This is what I have but the resulting sub aggregation is empty []

{
  "aggs": {
    "members": {
      "nested": { "path": "members" },
      "aggs": {
        "language": {
          "terms": {
            "field": "members.language"
          },
          "aggs": {
            "date": {
              "date_histogram": {
                "field": "signupdate",
                "interval": "month"
              }
            }
          }
        }
      }
    }
  }
}

Is this even doable with elasticsearch?

Upvotes: 2

Views: 1008

Answers (1)

Val
Val

Reputation: 217554

Here is the correct aggregation query (hint: you need to jump out of the nested context back into the parent one using reverse_nested)

{
  "aggs": {
    "members": {
      "nested": {
        "path": "members"
      },
      "aggs": {
        "language": {
          "terms": {
            "field": "members.language"
          },
          "aggs": {
            "parent": {
              "reverse_nested": {},
              "aggs": {
                "date": {
                  "date_histogram": {
                    "field": "signupdate",
                    "interval": "month"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Upvotes: 2

Related Questions