嵌套查询中带有AND运算符的查询字符串不起作用。任何想法?

时间:2019-11-20 07:24:58

标签: elasticsearch elasticsearch-7

我想获取其中嵌套子代包含两个单词 Mifune Miller-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"]
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这是查询多个嵌套字段的方法。

在must子句中有两个Nested查询。

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

Nested DataType

希望这会有所帮助