查找在任何外部或嵌套字段中找到相同字段值的文档

问题描述 投票:0回答:1

Elasticsearch 有一个索引,其中 _source 属于模式

{
  ...
  "color" : "white",
  "items" : [
    {
      ...
      "color": "blue",
      "items": [...]
    },
    {
      ...
      "color": "red",
      "items": [...]
    },
    {
      ...
      "color": "green",
      "items": [...]
    }
  ]
},

查询需要查找任何嵌套字段中

{"color" : "red"}
的文档,鉴于“color”仅出现在“items”中,因此它not,它可以存在于文档的任何外部或深层嵌套字段中。 所以基本上查询需要在文档中的任何位置找到
{"color" : "red"}

查询我可以想出使用文档中的“嵌套”

{
  "query": {
    "nested": {
      "path": "*",
      "query" : {
        "bool": {
          "must": [
            {"match": {"*.color": "red"}}
          ]
        }
      }
    }
  }  

}

并使用query_string

{
  "query": {
    "nested": {
      "path": "*",
      "query": {
        "query_stirng": {
          "query": "red",
          "default_field": "color"
        }
      }
    }
  }
}

但是这些查询正在产生所需的结果

elasticsearch
1个回答
0
投票

方法#1。手动显式查询

用于示例映射

PUT /nested_colors
{
    "mappings": {
        "properties": {
            "color": {
                "type": "text"
            },
            "level1": {
                "type": "nested",
                "properties": {
                    "color": {
                        "type": "text"
                    },
                    "level2": {
                        "type": "nested",
                        "properties": {
                            "color": {
                                "type": "text"
                            }
                        }
                    }
                }
            }
        }
    }
}

和文件

PUT /nested_colors/_bulk
{"create":{"_id":1}}
{"color":"red"}
{"create":{"_id":2}}
{"level1":[{"color":"red"}]}
{"create":{"_id":3}}
{"level1":[{"level2":[{"color":"red"}]}]}

写这样的查询或类似的

GET /nested_colors/_search
{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "color": "red"
                    }
                },
                {
                    "nested": {
                        "path": "level1",
                        "query": {
                            "match": {
                                "level1.color": "red"
                            }
                        }
                    }
                },
                {
                    "nested": {
                        "path": "level1",
                        "query": {
                            "nested": {
                                "path": "level1.level2",
                                "query": {
                                    "match": {
                                        "level1.level2.color": "red"
                                    }
                                }
                            }
                        }
                    }
                }
            ]
        }
    }
}

回应

{
    "took" : 1,
    "timed_out" : false,
    "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
    },
    "hits" : {
        "total" : {
            "value" : 3,
            "relation" : "eq"
        },
        "max_score" : 0.2876821,
        "hits" : [
            {
                "_index" : "nested_colors",
                "_type" : "_doc",
                "_id" : "1",
                "_score" : 0.2876821,
                "_source" : {
                    "color" : "red"
                }
            },
            {
                "_index" : "nested_colors",
                "_type" : "_doc",
                "_id" : "2",
                "_score" : 0.2876821,
                "_source" : {
                    "level1" : [
                        {
                            "color" : "red"
                        }
                    ]
                }
            },
            {
                "_index" : "nested_colors",
                "_type" : "_doc",
                "_id" : "3",
                "_score" : 0.2876821,
                "_source" : {
                    "level1" : [
                        {
                            "level2" : [
                                {
                                    "color" : "red"
                                }
                            ]
                        }
                    ]
                }
            }
        ]
    }
}

方法#2。自动脚本

您可以编写一个脚本来遍历所有字段并比较值

方式#3。重新组织您的数据

对我来说很奇怪的是,有同名但不同深度的字段。考虑重新组织您的数据。它使您的查询更简单

© www.soinside.com 2019 - 2024. All rights reserved.