# Get count of string occurrence from an array in aggregation query

**URL:** <https://discuss.elastic.co/t/get-count-of-string-occurrence-from-an-array-in-aggregation-query/167117>\
**Category:** Elasticsearch\
**Created:** [February 5, 2019, 12:21pm UTC](https://discuss.elastic.co/t/get-count-of-string-occurrence-from-an-array-in-aggregation-query/167117 "2019-02-05T12:21:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ijas\_ahamed](https://avatars.discourse-cdn.com/v4/letter/i/c89c15/32.png) [@Ijas\_ahamed](https://discuss.elastic.co/u/Ijas_ahamed)\
**Post date:** [February 5, 2019, 12:21pm UTC](https://discuss.elastic.co/t/get-count-of-string-occurrence-from-an-array-in-aggregation-query/167117/1 "2019-02-05T12:21:21Z")

</div>

I have document of structure as below  
{  
user\_id: 'user\_id',  
tags: ['tag\_1', 'tag\_2', 'tag\_3', 'tag\_1'],  
time: 123 // timestamp  
}

I am using below query to get users having a set of tags, but result is returning according to document count not the sum of the tag values

Query is  
{  
"query": {  
"bool": {  
"must": [  
],  
"filter": {  
"terms": {  
"tags": [  
"tag\_1",  
"tag\_6"  
]  
}  
}  
}  
},  
"aggs": {  
"users": {  
"terms": {  
"field": "user\_id",  
"size": 100  
}  
}  
}  
}

Result I am getting is as below  
{  
"aggregations": {  
"users": {  
"doc\_count\_error\_upper\_bound": 9882,  
"sum\_other\_doc\_count": 435400587,  
"buckets": [  
{  
"key": 34611,  
"doc\_count": 1026  
}  
]  
}  
}  
}

But actual result I need is

```
{
  "aggregations": {
    "users": {
      "doc_count_error_upper_bound": 9882,
      "sum_other_doc_count": 435400587,
      "buckets": [
        {
          "key": 34611,
          "tag_occurrence_count": 1026 // number of time all tags in the search comes up
        }
      ]
    }
  }
}

```

Is this possible in elastic search?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 5, 2019, 12:21pm UTC](https://discuss.elastic.co/t/get-count-of-string-occurrence-from-an-array-in-aggregation-query/167117/2 "2019-03-05T12:21:22Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
