简体   繁体   English

ElasticSearch:多级嵌套查询“AND”语法

[英]ElasticSearch: Multi-level nested query "AND" syntax

I've got an index similar to the example index shown in the elasticsearch doc multi-level nested query example: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html#multi-level-nested-query-ex我有一个类似于 elasticsearch doc 多级嵌套查询示例中显示的示例索引的索引: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query .html#multi-level-nested-query-ex

Given these example docs:鉴于这些示例文档:

{
  "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"
            }
        ]
    }
}

I need to be able to find documents where the driver.vehicle.make matches two values, ie the driver who has both vehicle makes "Powell Motors" and "Miller-meteor" should match McQueen but not Hudson.我需要能够找到 driver.vehicle.make 匹配两个值的文档,即同时拥有车辆制造“Powell Motors”和“Miller-meteor”的司机应该匹配 McQueen 而不是 Hudson。 I've tried a query similar to the doc example's make and model query, but it returns 0 docs:我尝试了类似于文档示例的 make 和 model 查询的查询,但它返回 0 个文档:

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

Changing the "must" to a "should" returns both docs.将“必须”更改为“应该”会返回两个文档。 I can't seem to find a query that will query the vehicle array for multiple value matches in the same doc.我似乎找不到在同一个文档中查询车辆数组的多个值匹配的查询。

The above query returns 0 documents, as there is no single document (inside driver.vehicle ) in your example that has driver.vehicle.make value as "Powell Motors" and "Miller-meteor".上面的查询返回 0 个文档,因为您的示例中没有单个文档(在driver.vehicle内)的driver.vehicle.make值为“Powell Motors”和“Miller-meteor”。

Note: Here single documents refer to each individual documents (or objects) inside driver.vehicle .注意:这里的单个文档是指driver.vehicle中的每个单独的文档(或对象)。

Therefore, when you are changing the must clause to should clause it returns those documents (in this case both the doc), that have driver.vehicle.make value as "Powell Motors" OR "Miller-meteor" .因此,当您将must子句更改为should子句时,它会返回那些具有driver.vehicle.make值为"Powell Motors" OR "Miller-meteor"的文档(在本例中为文档)。 must works as logical AND operator, and should works as logical OR operator. must用作逻辑 AND 运算符,并且should用作逻辑 OR 运算符。

According to your requirement, I believe you want those documents where driver who has vehicle makes "Powell Motors" OR "Miller-meteor" should match McQueen but not Hudson .根据您的要求,我相信您想要那些拥有车辆的司机制造“Powell Motors”或“Miller-meteor”的文件应该匹配 McQueen 而不是 Hudson

In this case, you need to combine a nested query with a normal query, which can be done using a bool query.在这种情况下,您需要将嵌套查询与普通查询结合起来,这可以使用 bool 查询来完成。

Your modified query will be您修改后的查询将是

{
  "query": {
    "nested": {
      "path": "driver",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "driver.last_name": "McQueen"
              }
            },
            {
              "nested": {
                "path": "driver.vehicle",
                "query": {
                  "bool": {
                    "should": [
                      {
                        "match": {
                          "driver.vehicle.make": "Powell Motors"
                        }
                      },
                      {
                        "match": {
                          "driver.vehicle.make": "Miller-Meteor"
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

And the search result will be搜索结果将是

 "hits" : [
      {
        "_index" : "soidx1",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 3.105637,
        "_source" : {
          "driver" : {
            "last_name" : "McQueen",
            "vehicle" : [
              {
                "make" : "Powell Motors",
                "model" : "Canyonero"
              },
              {
                "make" : "Miller-Meteor",
                "model" : "Ecto-1"
              }
            ]
          }
        }
      }
    ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM