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

**URL:** https://discuss.elastic.co/t/query-string-with-and-operator-in-nested-query-not-working-any-idea/208630
**Category:** Elasticsearch
**Created:** [November 20, 2019, 6:53am UTC](https://discuss.elastic.co/t/query-string-with-and-operator-in-nested-query-not-working-any-idea/208630 "2019-11-20T06:53:01Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Devang\_Prajapati](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/devang_prajapati/32/57575_2.png) [@Devang\_Prajapati](https://discuss.elastic.co/u/Devang_Prajapati)
#### Post date: [November 20, 2019, 6:53am UTC](https://discuss.elastic.co/t/query-string-with-and-operator-in-nested-query-not-working-any-idea/208630/1 "2019-11-20T06:53:01Z")

</div>

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

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

---

<div class="post-metadata">

### Author: ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)
#### Post date: [November 21, 2019, 7:28pm UTC](https://discuss.elastic.co/t/query-string-with-and-operator-in-nested-query-not-working-any-idea/208630/2 "2019-11-21T19:28:10Z")

</div>

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:

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

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [December 19, 2019, 7:42pm UTC](https://discuss.elastic.co/t/query-string-with-and-operator-in-nested-query-not-working-any-idea/208630/3 "2019-12-19T19:42:36Z")

</div>

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