Search code examples
elasticsearchelasticsearch-7

Query string with AND operator in nested query not working. Any idea?


I want to get the document in which nested child contains both words Mifune AND Miller-Meteor.

For more detail of nested, I've gone through https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html

here are the mappings

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

i've two documents in the index

{
  "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 as below

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

I tried the above query but it did not work also tried with query_string AND operator but it also not worked

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

Solution

  • This is how you should query multiple nested fields.

    There are two Nested queries inside your must clause.

    The bool->must operator should be outside of your internal nested fields.

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

    Results:

    "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

    Hope this helps