ElasticSearch -查询嵌套字段为空数组的文档[]

4smxwvx5  于 5个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(91)

我试图通过嵌套字段的空数组进行过滤。我尝试了许多不同的命令,甚至脚本,和扁平化字段,但无法检索任何结果。有人有这方面的经验吗?有可能在ES中完成吗?我也想通过相同的空数组字段值聚合(计数结果)[]

  • Map
suitability:
        type: "nested"
        properties:
            group:
               type: "keyword"
            code:
               type: "keyword"

字符串
在索引中,我在每个文档中都有这个嵌套字段

"suitability": [
            {
                "group": "RG309",
                "code": 1
            },
            {
                "group": "RG318",
                "code": 1
            },
            {
                "group": "RG355",
                "code": 2
            }
]


此外,某些文档具有空的嵌套字段

"suitability": []

  • 查询空的适用性结果(不起作用-始终返回total_hits:0)
GET /_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "nested": {
                        "ignore_unmapped": [
                            true
                        ],
                        "path": "suitability",
                        "query": {
                            "bool": {
                                "must_not": [
                                    {
                                        "exists": {
                                            "field": "suitability"
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    },
    "track_total_hits": true
}

  • 非空适用性查询(THIS WORKS:返回所有结果)
{
    "query": {
        "bool": {
            "must": [
                {
                    "nested": {
                        "ignore_unmapped": [
                            true
                        ],
                        "path": "suitability",
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                         "terms": {
                                             "suitability.rule_result": [
                                                 "1",
                                                 "2",
                                                 "3"
                                             ]
                                         }
                                     }
                                 ]
                            }
                        }
                    }
                }
            ]
        }
    },
    "track_total_hits": true
}

kuarbcqp

kuarbcqp1#

当查询nested时-它返回nested的结果。你可以阅读更多关于in nested query description的内容。所以当每个嵌套对象返回0或更多的内部命中时,它返回所有结果。
要实际获取嵌套对象为空的位置-您需要重新构造查询以设置所有嵌套路径为空,而不仅仅是嵌套部分中的字段为空:

"must_not": [
            {
                "nested": {
                    "path": "nestedField",
                    "query": {
                        "exists": {
                            "field": "nestedField.id"
                        }
                    }
                }
            }

字符串

相关问题