Kitt Holland
Kitt Holland

Reputation: 83

How to perform a distinct count query in Elasticsearch

I have an index with a host field. I am trying to retrieve the count of documents by distinct host name.

IE:

Host1: 
    Count: 72
Host2:
    Count: 33
Host3:
    Count: 153

Each document has a host field and it is a string. I assume I need to do something involving terms and cardinality, but I can't quite nail the syntax.

Upvotes: 0

Views: 427

Answers (1)

kgf3JfUtW
kgf3JfUtW

Reputation: 14908

How to get all possible values for field host?

curl -XGET  http://localhost:9200/articles/_search?pretty -d '
{
    "aggs" : {
        "whatever_you_like_here" : {
            "terms" : { "field" : "host", "size":10000 }
        }
    },
    "size" : 0
}'

Note

  • The result will contain a doc_count for each unique value

  • "size":10000 Get at most 10000 unique values. Default is 10.

  • "size":0 By default, "hits" contains 10 documents. We don't need them.

  • By default, the buckets are ordered by the doc_count in decreasing order.


Reference: bucket terms aggregation

Upvotes: 1

Related Questions