Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count distinct on elastic search

How to achieve count distinct function on elastic search type using sql4es driver?

Select distinct inv_number , count(1) from invoices;

But it returns the total count of the particular invoice number.

like image 240
PravinKumar.R Avatar asked Mar 19 '17 10:03

PravinKumar.R


People also ask

How do I count distinct in Elasticsearch?

There's no support for distinct counting in ElasticSearch, although non-deterministic counting exists. Use "terms" aggregation and count buckets in result. See Count distinct on elastic search question.

How do you count unique values in Kibana?

You can use a Metric visualization and just use the "count" metric for this. There are many ways to do this, generally in most visualizations, you can: use "Unique Count" on the personId field as the metric. use a terms aggregation on the organizationId field for the X-Axis (or split rows in a table visualization).

What is cardinality in elastic search?

Cardinality aggregationedit. A single-value metrics aggregation that calculates an approximate count of distinct values.


1 Answers

  {
      "size": 0, 
      "aggs": {
        "total_invoices": {
          "terms": {
            "field": "inv_number" 

        },
        "aggs": {
          "unique_invoiceid": {
            "cardinality": {
              "field": "inv_number"
            }
          }
        }
      }
    }

This will give you the invoice number as key and distict value in unique_invoiceid

like image 176
prasad kp Avatar answered Sep 28 '22 06:09

prasad kp