【发布时间】:2021-07-17 13:31:42
【问题描述】:
我的文档看起来像
{
"path": "/foo/bar",
"userId" : "33",
}
我想要拥有超过的 userId 的数量
100 个文档 50 到 100 个文档 少于 100 个文档 我尝试使用不同的聚合,但我不知道如何根据另一个聚合的计数进行范围聚合
感谢您的帮助,
【问题讨论】:
标签: jquery elasticsearch aggregation
我的文档看起来像
{
"path": "/foo/bar",
"userId" : "33",
}
我想要拥有超过的 userId 的数量
100 个文档 50 到 100 个文档 少于 100 个文档 我尝试使用不同的聚合,但我不知道如何根据另一个聚合的计数进行范围聚合
感谢您的帮助,
【问题讨论】:
标签: jquery elasticsearch aggregation
您需要使用terms aggregation 和bucket selector aggregation,根据他们的文档计数来隔离UserId
在下面的例子中,考虑"UserId":"33"的5个文档,“UserId”:“34”的3个文档和"UserId":"35"的1个文档。现在搜索查询将根据
添加一个包含索引数据、搜索查询和搜索结果的工作示例
索引数据:
{
"path": "/foo/bar",
"userId" : "34"
}
{
"path": "/foo/bar",
"userId" : "34"
}
{
"path": "/foo/bar",
"userId" : "34"
}
{
"path": "/foo/bar",
"userId" : "33"
}
{
"path": "/foo/bar",
"userId" : "33"
}
{
"path": "/foo/bar",
"userId" : "33"
}
{
"path": "/foo/bar",
"userId" : "33"
}
{
"path": "/foo/bar",
"userId" : "33"
}
{
"path": "/foo/bar",
"userId" : "35"
}
搜索查询:
{
"size": 0,
"aggs": {
"userId_lessThanEqualTo2": {
"terms": {
"field": "userId.keyword"
},
"aggs": {
"less_than_2": {
"bucket_selector": {
"buckets_path": {
"the_doc_count": "_count"
},
"script": "params.the_doc_count < 2"
}
}
}
},
"userId_btw2-4": {
"terms": {
"field": "userId.keyword"
},
"aggs": {
"less_than_4": {
"bucket_selector": {
"buckets_path": {
"the_doc_count": "_count"
},
"script": "params.the_doc_count >= 2 && params.the_doc_count < 4"
}
}
}
},
"userId_greaterThanEqualTo4": {
"terms": {
"field": "userId.keyword"
},
"aggs": {
"greater_than_4": {
"bucket_selector": {
"buckets_path": {
"the_doc_count": "_count"
},
"script": "params.the_doc_count >= 4"
}
}
}
}
}
}
搜索结果:
"aggregations": {
"userId_btw2-4": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "34",
"doc_count": 3
}
]
},
"userId_greaterThanEqualTo4": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "33",
"doc_count": 5
}
]
},
"userId_lessThanEqualTo2": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "35",
"doc_count": 1
}
]
}
}
【讨论】:
感谢您的帮助。 我试了一下,搜索结果是:
"aggregations": {
"userId_btw2-4": {
"doc_count_error_upper_bound" : 2125,
"sum_other_doc_count" : 2482161,
"buckets": []
},
"userId_greaterThanEqualTo4": {
"doc_count_error_upper_bound" : 2125,
"sum_other_doc_count" : 2482138,
"buckets" : [
{
"key" : "804ba398-e512-4e43-a289-3e63483baabd",
"doc_count" : 13914
},
{
"key" : "de0ce908-7172-4be7-8e3e-181d94fb0571",
"doc_count" : 8561
},
{
"key" : "03670858-cb0b-4ca5-ae5a-07e92b12905f",
"doc_count" : 4918
},
{
"key" : "25a17282-2eb0-4a1f-8fdd-b0eef083af25",
"doc_count" : 4847
},
{
"key" : "c096169d-0bb3-42d2-8c24-92fa15754339",
"doc_count" : 4265
},
{
"key" : "9ff15fdc-2aa3-49a6-8422-4d7137f434ae",
"doc_count" : 4159
},
{
"key" : "ce77b5c0-e858-47a6-8598-879d78124857",
"doc_count" : 3730
},
{
"key" : "171bf337-8c19-4180-ba21-2ec9e72e380f",
"doc_count" : 3513
},
{
"key" : "745dd8e0-a021-4c91-abd8-2193e1805fd0",
"doc_count" : 3245
},
{
"key" : "e6645d55-a411-44c3-82db-1a5be1534e5e",
"doc_count" : 3051
}
]
},
"userId_lessThanEqualTo2": {
"doc_count_error_upper_bound" : 2125,
"sum_other_doc_count" : 2482161,
"buckets": []
}
}
【讨论】: