问题
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