我有嵌套聚合和过滤器的问题,基本上没有过滤器它返回全局范围的总和但是嵌套的doc_count是正常但是sum总是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 :(得分:1)
您已应用的嵌套过滤器仅适用于条件,而不适用于聚合将在后续聚合中查找值的位置。这意味着sum值存在于嵌套对象中而不是父文档中,因此得到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"
}
}
}
}
}
}
}
}
}
}
答案 1 :(得分:1)
问题可能只是您访问嵌套字段的方式,特别是您必须针对match
和search
的{{1}}子字段引导这些leaf0
语句 - 根据您的映射定义,子字段是实际分析的子字段。考虑到这一点,请尝试以下方法:
leaf1
我使用了一个非常人为的测试数据集 - 可能值得注意的是,我针对索引进行了查询,而不是针对特定的文档类型(因为在您最初发布的查询中,嵌套路径似乎是完全符合条件的。