【问题标题】:How do I query an Elasticsearch nested document within an inner object?如何在内部对象中查询 Elasticsearch 嵌套文档?
【发布时间】:2017-10-18 15:09:43
【问题描述】:

我正在尝试过滤 Elasticsearch 查询 (ES 5.6.2) 中 nested document 中的字段。嵌套文档本身就是主文档内部对象中的一个字段。映射如下所示:

{
    "mappings": {
        "container": {
            "properties": {
                "host": {
                    "properties": {
                        "tags_nested": {
                            "type": "nested",
                            "properties": {
                                "tag_key": {
                                    "type": "keyword"
                                },
                                "tag_val": {
                                    "type": "keyword"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

我想过滤host.tags_nested.tag_keys,但我无法找出正确的语法来访问host 内部对象中的嵌套tags_nested 文档。当我知道有一些应该匹配时,我尝试了以下查询,它不返回任何结果:

{
    "query": {
        "nested": {
            "path": "host.tags_nested",
            "query": {
                "bool": {
                    "filter": [
                        {
                            "term": {
                                "host.tags_nested.tag_key": "example_key"
                            }
                        }
                    ]
                }
            }
        }
    }
}

根据ES docs,您可以通过传递与嵌套文档的字段名称对应的path 来执行nested 查询以在嵌套文档中进行查询。但是,当path 位于内部对象中并且需要使用点符号访问时,这似乎不起作用。

有什么想法吗?

【问题讨论】:

    标签: elasticsearch elasticsearch-5


    【解决方案1】:

    试试这个。 Term Query 搜索我们指定的确切单词。因此,为此使用fieldname.keyword,因为关键字存储我们索引它们时的精确文本。

    {
        "query": {
            "nested": {
                "path": "host.tags_nested",
                "query": {
                    "bool": {
                        "filter": [
                            {
                                "term": {
                                    "host.tags_nested.tag_key.keyword": "example_key"
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-31
      • 2015-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多