Get counting data by comma-separated value field in Elastic Search

Need Help.

I have data in elasticsearch, field "interests" like this:

{
"interests": "A,C,D,E"
},
{
"interests": "B,C,D"
},
{
"interests": "A,B,C,D,E"
},
{
"interests": "D,E"
}

I want to get data like this:
{
"key": "A",
"doc_count": 2
},
{
"key": "B",
"doc_count": 2
},
{
"key": "C",
"doc_count": 3
},
{
"key": "D",
"doc_count": 4
},
{
"key": "E",
"doc_count": 3
}

what steps should I take.
Thank you.

>Solution :

A simple terms aggregation should do the job:

POST test/_search
{
  "aggs": {
    "interests": {
      "terms": { "field": "interests" }
    }
  }
}

Leave a Reply