0

我想获取其中嵌套子项同时包含MifuneMiller-Meteor的文档。

有关嵌套的更多详细信息,我已经浏览了https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html

这是映射

{
    "mappings" : {
        "properties" : {
            "driver" : {
                "type" : "nested",
                "properties" : {
                    "last_name" : {
                        "type" : "text"
                    },
                    "vehicle" : {
                        "type" : "nested",
                        "properties" : {
                            "make" : {
                                "type" : "text"
                            },
                            "model" : {
                                "type" : "text"
                            }
                        }
                    }
                }
            }
        }
    }
}

我在索引中有两个文档

{
  "driver" : {
        "last_name" : "McQueen",
        "vehicle" : [
            {
                "make" : "Powell Motors",
                "model" : "Canyonero"
            },
            {
                "make" : "Miller-Meteor",
                "model" : "Ecto-1"
            }
        ]
    }
},{
  "driver" : {
        "last_name" : "Hudson",
        "vehicle" : [
            {
                "make" : "Mifune",
                "model" : "Mach Five"
            },
            {
                "make" : "Miller-Meteor",
                "model" : "Ecto-1"
            }
        ]
    }
}

查询如下

{
    "query" : {
        "nested" : {
            "path" : "driver",
            "query" : {
                "nested" : {
                    "path" :  "driver.vehicle",
                    "query" :  {
                        "bool" : {
                            "must" : [
                                { "match" : { "driver.vehicle.make" : "Mifune" } },
                                { "match" : { "driver.vehicle.make" : "Miller-Meteor" } }
                            ]
                        }
                    }
                }
            }
        }
    }
}

我尝试了上面的查询,但它不起作用也尝试使用 query_string AND 运算符,但它也不起作用

{
    "query": {
        "nested": {
            "path": "driver",
            "query": {
                "nested": {
                    "path": "driver.vehicle",
                    "query": {
                        "bool": {
                            "must": [
                                {
                                    "query_string": {
                                        "query": "Mifune AND Miller-Meteor",
                                        "fields": ["driver.vehicle.make"]
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        }
    }
}
4

1 回答 1

0

这就是您应该查询多个嵌套字段的方式。

Nested你的 must 子句中有两个查询。

bool->must运算符应该在您的内部嵌套字段之外。

GET my_index/_search
{
  "query": {
    "nested": {
      "path": "driver",
      "query": {
        "bool": {
          "must": [
            {
              "nested": {
                "path": "driver.vehicle",
                "query": {
                  "match": {
                    "driver.vehicle.make": "Mifune"
                  }
                }
              }
            },
            {
              "nested": {
                "path": "driver.vehicle",
                "query": {
                  "match": {
                    "driver.vehicle.make": "Miller-Meteor"
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

结果:

"timed_out" : false,
 "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 2.769686,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 2.769686,
        "_source" : {
          "driver" : {
            "last_name" : "Hudson",
            "vehicle" : [
              {
                "make" : "Mifune",
                "model" : "Mach Five"
              },
              {
                "make" : "Miller-Meteor",
                "model" : "Ecto-1"
              }
            ]
          }
        }
      }
    ]
  }
}

嵌套数据类型

希望这可以帮助

于 2019-11-20T08:01:11.210 回答