Get count of string occurrence from an array in aggregation query

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?

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