Count distinct on elastic search

后端 未结 4 1309
面向向阳花
面向向阳花 2020-12-11 07:07

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

Select distinct inv_number , count(1) from invoices;

But

相关标签:
4条回答
  • 2020-12-11 07:19

    Elasticsearch doesn't support deterministic DISTINCT counts (source). It supports only approximate distinct counters like "cardinality". One way to count distincts in a deterministic way is to aggregate them using "terms" and count buckets from result.

    0 讨论(0)
  • 2020-12-11 07:20

    Since, the OP is using sql4es jdbc driver, he is asking for a sql query for his use-case :

    SELECT COUNT(DISTINCT inv_number) from invoices;
    

    it returns the number of distinct values of the specified column

    0 讨论(0)
  • 2020-12-11 07:28
      {
          "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

    0 讨论(0)
  • 2020-12-11 07:40

    This should work to count exact distinct values:

    curl -X POST "localhost:9200/invoices/_search?size=0&pretty" -H 'Content-Type: application/json' -d '{
    "aggs" : {
        "types_count" : {
          "value_count" : { "field" : "inv_number" }
        },
        "group_by_status": {
          "terms": {
            "field": "inv_number"
          }
        }
    }
    

    }'

    0 讨论(0)
提交回复
热议问题