czr_RR
czr_RR

Reputation: 591

Appending further aggregations within Terms Aggregation

Sorry if this has been asked already but been lurking around SO and couldn't find anything which suits my needs.

Basically, what I'm trying to achieve in my first quick tries with ES is to add further counters within a Terms Aggregation.

Giving it a quick try I'm sending the following request to ES.

POST http://localhost:9200/people/_search

{
    "size": 0,
    "aggs": {
        "agg_by_name": {
            "terms": { "field": "name"}
        }
    }
}

And what I'm getting right now is just what the sample shows in the docs.

{
    "took": 89,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 10000,
            "relation": "gte"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "agg_by_name": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 9837,
            "buckets": [
                {
                    "key": "James",
                    "doc_count": 437
                },
                {
                    "key": "Eduard",
                    "doc_count": 367
                },
                {
                    "key": "Leonardo",
                    "doc_count": 235
                },
                {
                    "key": "George",
                    "doc_count": 209
                },
                {
                    "key": "Harrison",
                    "doc_count": 180
                }, ...

However, I can't really get how to include further inner aggregations in the bucket. Something that would result in a document like this.

                {
                    "key": "Harrison",
                    "doc_count": 180,
                    "lives_in_NY": 40,
                    "lives_in_CA": 140,
                    "distinct_surnames": [ ... ]
                }

How should I structure my aggregation so that those are included bucket-wise?

Upvotes: 0

Views: 41

Answers (1)

Lupanoide
Lupanoide

Reputation: 3222

You could try sometihng like this:

  {
  "size": 0,
  "aggs": {
    "getAllTheNames": {
      "terms": {
        "field": "name",
        "size": 100
      },
      "aggs": {
        "getAllTheSurnames": {
          "terms": {
            "field": "surname",
            "size": 100
          }
        }
      }
    }
  }
}

For living city could be something like:

  {
  "size": 0,
  "aggs": {
    "getAllTheNames": {
      "terms": {
        "field": "name",
        "size": 100
      },
      "aggs": {
        "getAllTheCities": {
          "terms": {
            "field": "city",
            "size": 100
          }
        }
      }
    }
  }
}

Upvotes: 1

Related Questions