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 Nested query | Elasticsearch Guide [8.11] | Elastic

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

hitting query

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

Your queries search for a vehicle that is both a Mifune and a Miller-Meteor at the same time. That's not the logic you're looking for. You seem to want to look for drivers that have one vehicle that is a Mifune and another vehicle that is a Miller-Meteor. You do that by moving the bool query one level higher and using it to query with two separate nested queries instead:

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

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.