首页 > 解决方案 > 弹性:嵌套查询或查询弹性文档的文档键,其值为键、值对的数组

问题描述

我正在尝试在弹性中形成查询,文档的存储方式如下-

{
    "data": [
        {
            "value": "Lorem ipsum, Lorem ipsum",
            "source": [
                "abc.com", "xyz.com"
            ]
        },
        {
            "value": "Lorem ipsum Lorem ipsum",
            "source": [
                "wxy.com", "osa.com"
            ]
        }
    ]
}

我想按某个来源过滤文档。

我从这里得到了一些想法,但它不起作用。它返回该字段的所有查询中的所有记录。

到目前为止,我已经用尽了我所知道的所有查询排列和组合。那么,知道如何实现这一目标吗?

映射:-

{
    "mappings": {
        "_document_name_": {
            "properties": {
                "data": {
                    "properties": {
                        "source": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "value": {
                            "type": "text",
                            "fields": {
                            "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        }
                    }
                }      
            }
        }
    }
}

询问:-

{
    "query": {
        "nested": {
            "path": "address",
            "query": {
                "bool": {
                    "must_not": [
                        { "match": {"data.source": "abc.com"}}
                    ]
                }
            }
        }
    }
}

标签: elasticsearchnested

解决方案


推荐阅读