Bucketing/Counting items inside list field Elasticsearch 5.5.2


(Alex Tam) #1

I have an index that looks like this:

{
"id":1,
"countries":["china","japan","united states"]
},
{
"id":2,
"countries":["china","united states"]
}

I want an output like this:

{ china: 2 }{ united states: 2} { japan: 1}

Is there a query or aggregation that does this?


(Alex Tam) #2

I found it. I need the terms aggregator.

curl -XPOST 'localhost:9200/someindex/_search?size=0&pretty' -H 'Content-Type: application/json' -d'
{
    
    "aggs" : {
        "countries" : {
            "terms" : { "field" : "countries" }
        }
    }

}
'


(system) #3

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