Elasticsearch 1.7.3: Is there any aggregation to count how many items are in a terms aggregation?

Viewed 41

I have a field that have an array of items and I need to count how many items of each type are there across the other documents.

Elasticsearch mapping.

{
    "properties": {
        "items": {
            "type": "string",
            "index": "not_analyzed"
        }
    }
}

Document example.

Document 1

{
    "items": [
        "a", "b", "a", "c"
    ]
}

I have the following aggregation, which is looking for each item in how many documents are present. Also the aggregation values_count is returning the total items in the array.

{
    "items_distribution": {
        "terms": {
            "field": "items"
        },
        "aggs": {
            "terms_count": {
                "value_count": {
                    "field": "items"
                }
            }
        }
    }
}

With the following result

{
    "took": 311,
    "timed_out": false,
    "_shards": {
        "total": 200,
        "successful": 200,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.0,
        "hits": []
    },
    "aggregations": {
        "items_distribution": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "a",
                    "doc_count": 1,
                    "terms_count": {
                        "value": 4
                    }
                },
                {
                    "key": "b",
                    "doc_count": 1,
                    "terms_count": {
                        "value": 4
                    }
                },
                {
                    "key": "c",
                    "doc_count": 1,
                    "terms_count": {
                        "value": 4
                    }
                }
            ]
        }
    }
}

But I'm searching for something like that, where is counting how many times is present that item of the terms, not counting the whole list

{
    "took": 311,
    "timed_out": false,
    "_shards": {
        "total": 200,
        "successful": 200,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.0,
        "hits": []
    },
    "aggregations": {
        "items_distribution": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "a",
                    "doc_count": 1,
                    "terms_count": {
                        "value": 2  <---- twice in the array
                    }
                },
                {
                    "key": "b",
                    "doc_count": 1,
                    "terms_count": {
                        "value": 1  <---- once in the array
                    }
                },
                {
                    "key": "c",
                    "doc_count": 1,
                    "terms_count": {
                        "value": 1  <---- once in the array
                    }
                }
            ]
        }
    }
}
0 Answers
Related