elasticsearch-aggregation - 在 `must` 中使用 `aggs` 的弹性查询
问题描述
有什么办法可以让我们aggs
在里面must
.?? 我有一个包含多个条件和一个聚合的查询,聚合必须是AND
子句的一部分(我的意思是must
)
查询看起来像这样......
{
"query": {
"bool": {
"must": [
{
"has_child": {
"type": "actualIndex",
"min_children": 1,
"query": {
"range": {
"actualIndex.datetime": {
"gte": "now-364d/d",
"lt": "now/d"
}
}
}
}
},
{
"bool": {
"must": [
{
"has_child": {
"type": "actualIndex",
"min_children": 3,
"query": {
"bool": {
"must": [
{
"term": {
"actualIndex.array1.cloumA": "abc"
}
},
{
"term": {
"actualIndex.array1.cloumB": "xyz"
}
}
]
}
}
}
}
]
}
},
{
"bool": {
"must": [
{
"has_child": {
"type": "actualIndex",
"min_children": 2,
"query": {
"terms": {
"actualIndex.array1.mno": [
"value1",
"value2",
"value3"
]
}
}
}
},
{
"query": {
"range": {
"actualIndex.datetime": {
"gte": "now-364d/d",
"lt": "now/d"
}
}
},
"aggs": {
"groupby": {
"composite": {
"sources": [
{
"ids": {
"terms": {
"field": "ids"
}
}
}
]
},
"aggs": {
"total": {
"children": {
"type": "actualIndex"
},
"aggs": {
"range_bucket": {
"date_range": {
"field": "actualIndex.datetime",
"keyed": true,
"ranges": {
"from": "now-364d/d",
"to": "now",
"key": "bucket"
}
},
"aggs": {
"BAS_TOTAL": {
"sum": {
"field": "actualIndex.amountField"
}
}
}
},
"sum_intermediate": {
"max_bucket": {
"buckets_path": "range_bucket>BAS_TOTAL"
}
}
}
},
"inventory_selector": {
"bucket_selector": {
"buckets_path": {
"total": "total > sum_intermediate"
},
"script": "params.total > 1000"
}
}
}
}
}
}
]
}
}
]
}
}
}
我只需要的结果也应该aggs
是的一部分must
任何人都可以在这里寻求帮助。
PS分数并不重要。
解决方案
我们可以使用多个aggs
,这个问题可以通过脚本解决。
{
"inventory_selector": {
"bucket_selector": {
"buckets_path": {
"total": "total > sum_intermediate",
"actualPrice" : "price>sum_intermediate"
},
"script": "params.total > 1000 && params.actualPrice <= 500"
}
}
}
&&
可以用于must
操作,||
也可以用于应该操作。
推荐阅读
- google-apps-script - 表格脚本:激活按钮,将数据从单元格复制到表格,然后清除数据范围
- python - 使用最少的代码将结果写入文件
- ios - iOS 自定义模态演示忽略supportedInterfaceOrientation
- java - 无法解析方法 getSupportFragmentManager()
- delphi - 在 Delphi 中实例化对象而不使用变量
- r - 对于“sort.list”,“x”必须是原子的,使用 dbFD()。FD包
- twilio - 当输入仅是finishOnKey时,如何防止Twilio Gather挂断?
- unity3d - 错误cs2001源文件找不到2018
- c# - 调用 HttpClient GetStreamAsync 时如何模拟 Stream 响应
- javascript - 匹配单个字符但不匹配多个