Count distinct on elastic search

早过忘川 提交于 2019-12-24 08:26:02

问题


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.


回答1:


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




回答2:


  {
      "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




回答3:


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.




回答4:


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"
      }
    }
}

}'



来源:https://stackoverflow.com/questions/42885532/count-distinct-on-elastic-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!