[英]Elasticsearch having count(*)= n
I have a simple index (in elasticsearch) and need to execute a query that is available in SQL databases.我有一个简单的索引(在弹性搜索中),需要执行 SQL 数据库中可用的查询。
my-index structure:我的索引结构:
'{"id":"1","userid":"1"}'
'{"id":"2","userid":"1"}'
'{"id":"3","userid":"1"}'
'{"id":"4","userid":"2"}'
'{"id":"5","userid":"3"}'
now I need to execute this query:现在我需要执行这个查询:
select count(userid),userid from my-index group by userid having count(userid)=3;
Is it possible to execute such queries in elasticsearch?是否可以在 elasticsearch 中执行此类查询?
You can use terms aggregation along with bucket selector aggregation您可以将术语聚合与存储桶选择器聚合一起使用
Adding a working example with index mapping, search query, and search result添加具有索引映射、搜索查询和搜索结果的工作示例
Index Mapping:索引映射:
{
"mappings": {
"properties": {
"userid": {
"type": "keyword"
}
}
}
}
Search Query:搜索查询:
{
"size": 0,
"aggs": {
"userId": {
"terms": {
"field": "userid"
},
"aggs": {
"the_filter": {
"bucket_selector": {
"buckets_path": {
"the_doc_count": "_count"
},
"script": "params.the_doc_count == 3"
}
}
}
}
}
}
Search Result:搜索结果:
"aggregations": {
"userId": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "1", // this denotes the userid having count==3
"doc_count": 3
}
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.