gpt4 book ai didi

elasticsearch - ElasticSearch中的嵌套聚合和过滤

转载 作者:行者123 更新时间:2023-12-02 22:25:04 25 4
gpt4 key购买 nike

我有一个嵌套的聚合和过滤器的问题,基本上没有过滤器它返回全局范围的总和,但嵌套的doc_count是可以的,但是总和总是0,这是我要运行的查询:

{
"query": {
"nested": {
"path": "skills.tree",
"query": {
"bool" : {
"must" : [
{"match": {"leaf0": "Management"}},
{"match": {"leaf1": "Financial"}}
]
}
}
}
},
"aggs": {
"by_org": {
"terms": {
"field": "org"
},
"aggs": {
"sum_weight0-filtered": {
"filter": {
"nested": {
"path": "skills.tree",
"query": {
"bool" : {
"must" : [
{"match": {"leaf0": "Management"}},
{"match": {"leaf1": "Financial"}}
]
}
}
}
},
"aggs":{
"sum0":{
"sum": {
"field": "skills.tree.weight0"
}
},
"sum1":{
"sum": {
"field": "skills.tree.weight1"
}
}
}
}
}
}
}
}

下面是示例输出:
{
"took": 978,
"timed_out": false,
"_shards": {
"total": 50,
"successful": 50,
"failed": 0
},
"hits": {
"total": 11337,
"max_score": 0,
"hits": []
},
"aggregations": {
"by_org": {
"buckets": [
{
"key": "Aetna",
"doc_count": 1888,
"sum_weight0-filtered": {
"doc_count": 1888,
"sum0": {
"value": 0
},
"sum1": {
"value": 0
}
}
},
{
"key": "AECOM",
"doc_count": 1085,
"sum_weight0-filtered": {
"doc_count": 1085,
"sum0": {
"value": 0
},
"sum1": {
"value": 0
}
}
}
....

这是部分架构:
'skills'        => array(
'properties' => array(
'tree' => array(
'type' => 'nested',
'properties' => array(
'leaf0' => array(
"type" => "multi_field",
"fields" => array(
"leaf0"=> array(
"type" => "string",
"index" => "not_analyzed"
),
"search" => array(
"type" => "string",
"index" => "analyzed"
)
)
),
'leaf1' => array(
"type" => "multi_field",
"fields" => array(
"leaf1"=> array(
"type" => "string",
"index" => "not_analyzed"
),
"search" => array(
"type" => "string",
"index" => "analyzed"
)
)
),
'leaf2' => array(
"type" => "multi_field",
"fields" => array(
"leaf2"=> array(
"type" => "string",
"index" => "not_analyzed"
),
"search" => array(
"type" => "string",
"index" => "analyzed"
)
)
),
'leaf3' => array(
"type" => "multi_field",
"fields" => array(
"leaf3"=> array(
"type" => "string",
"index" => "not_analyzed"
),
"search" => array(
"type" => "string",
"index" => "analyzed"
)
)
),
'leaf4' => array(
"type" => "multi_field",
"fields" => array(
"leaf4"=> array(
"type" => "string",
"index" => "not_analyzed"
),
"search" => array(
"type" => "string",
"index" => "analyzed"
)
)
),
'leaf5' => array(
"type" => "multi_field",
"fields" => array(
"leaf5"=> array(
"type" => "string",
"index" => "not_analyzed"
),
"search" => array(
"type" => "string",
"index" => "analyzed"
)
)
),
'weight1' => array(
'type' => 'integer',
),
'weight2' => array(
'type' => 'integer',
),
'weight3' => array(
'type' => 'integer',
),
'weight4' => array(
'type' => 'integer',
),
'weight5' => array(
'type' => 'integer',
)
)
)

问题在于关于sum0和sum1,尽管其中存在值,它们都返回0(它在更高的范围内起作用(没有过滤器))。我在这里做错了什么?

最佳答案

您应用的嵌套过滤器仅适用于条件,不适用于聚合将在后续聚合中查找值的位置。这意味着总和值存在于嵌套对象中,而不存在于父文档中,因此您得到0。
现在,如果您使用嵌套聚合来要求ES对嵌套对象进行聚合,则它应该可以工作-

{
"query": {
"nested": {
"path": "skills.tree",
"query": {
"bool": {
"must": [
{
"match": {
"leaf0": "Management"
}
},
{
"match": {
"leaf1": "Financial"
}
}
]
}
}
}
},
"aggs": {
"by_org": {
"terms": {
"field": "org"
},
"aggs": {
"sum_weight0-filtered": {
"filter": {
"nested": {
"path": "skills.tree",
"query": {
"bool": {
"must": [
{
"match": {
"leaf0": "Management"
}
},
{
"match": {
"leaf1": "Financial"
}
}
]
}
}
}
},
"aggs": {
"nestedAgg": {
"nested": {
"path": "skills.tree"
},
"aggs": {
"sum0": {
"sum": {
"field": "skills.tree.weight0"
}
},
"sum1": {
"sum": {
"field": "skills.tree.weight1"
}
}
}
}
}
}
}
}
}
}

关于elasticsearch - ElasticSearch中的嵌套聚合和过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28681068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com