原始数据
PUT cars
POST _bulk
{"index":{"_index":"cars","_id":"1"}}
{"model":"A","color":"red"}
{"index":{"_index":"cars","_id":"2"}}
{"model":"A","color":"white"}
{"index":{"_index":"cars","_id":"3"}}
{"model":"A","color":"black"}
{"index":{"_index":"cars","_id":"4"}}
{"model":"A","color":"yellow"}
{"index":{"_index":"cars","_id":"5"}}
{"model":"B","color":"red"}
{"index":{"_index":"cars","_id":"6"}}
{"model":"B","color":"white"}
{"index":{"_index":"cars","_id":"7"}}
{"model":"C","color":"black"}
{"index":{"_index":"cars","_id":"8"}}
{"model":"C","color":"red"}
{"index":{"_index":"cars","_id":"9"}}
{"model":"C","color":"white"}
{"index":{"_index":"cars","_id":"10"}}
{"model":"C","color":"yellow"}
{"index":{"_index":"cars","_id":"11"}}
{"model":"C","color":"blue"}
{"index":{"_index":"cars","_id":"12"}}
{"model":"D","color":"red"}
{"index":{"_index":"cars","_id":"13"}}
{"model":"A","color":"red"}
实现mysql中的如下需求:
SELECT model,COUNT(DISTINCT color) color_count
FROM cars
GROUP BY model
HAVING color_count > 1
ORDER BY color_count desc LIMIT 2;
mysql与ES的对应关系如下:
GET cars/_search
{
"size": 0,
"aggs": {
"aggmodels": {
"terms": {
"field": "model.keyword"
},
"aggs": {
"color_count": {
"cardinality": {
"field": "color.keyword"
}
},
"color_count_filter": {
"bucket_selector": {
"buckets_path": {
"colorcount": "color_count"
},
"script": "params.colorcount>1"
}
},
"color_count_sort": {
"bucket_sort": {
"sort": {
"color_count": "desc"
},
"size": 2
}
}
}
}
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。