# Problem with nested query

**URL:** <https://forum.opensearch.org/t/problem-with-nested-query/22641>\
**Category:** OpenSearch\
**Created:** [December 2, 2024, 8:53pm UTC](https://forum.opensearch.org/t/problem-with-nested-query/22641 "2024-12-02T20:53:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ehsankf](https://avatars.discourse-cdn.com/v4/letter/e/d78d45/32.png) [@ehsankf](https://forum.opensearch.org/u/ehsankf)\
**Post date:** [December 2, 2024, 8:53pm UTC](https://forum.opensearch.org/t/problem-with-nested-query/22641/1 "2024-12-02T20:53:32Z")

</div>

**Versions** (relevant - OpenSearch/Dashboard/Server OS/Browser):

**Describe the issue** :

I am indexing the following doc

```auto
{ 
  "patients": [ 
    {"name" : "John Doe", "age" : 56, "smoker" : true},
    {"name" : "Mary Major", "age" : 85, "smoker" : false}
  ] 
}

```

with the following mapping

```auto
mapping = {
  "mappings" : {
    "properties": {
      "patients": { 
        "type" : "nested"
      }
    }
  }
}

```

for this query

```auto
{
  "query": {
    "nested": {
      "path": "patients",
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "patients.smoker": false
              }
            },
            {
              "range": {
                "patients.age": {
                  "gte": 75
                }
              }
            }
          ]
        }
      }
    }
  }
}

```

```auto
response = client.search(index=index_name, body=query)

```

returns

```auto
{'took': 116, 'timed_out':
 False, '_shards': 
{'total': 3, 'successful': 3, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 1, 'relation': 'eq'}, 'max_score': 1.6931472, '
hits': [{'_index': 'test', '_id': 'U2j6iJ', 
'_score': 1.6931472, 
'_source': 
{'patients': [{'name': 'John Doe', 'age': 56, 'smoker': True}, 
{'name': 'Mary Major', 'age': 85, 'smoker': False}]}}]}}

```

I expect this query only returns

```auto
Mary Major

```

---

<div class="post-metadata">

**Author:** ![pablo](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/pablo/32/10363_2.png) [@pablo](https://forum.opensearch.org/u/pablo)\
**Post date:** [December 3, 2024, 7:44pm UTC](https://forum.opensearch.org/t/problem-with-nested-query/22641/2 "2024-12-03T19:44:24Z")

</div>

@ehsankf This is expected. It returns a single document.

> [@ehsankf](#):
>
> ```auto
> { 
> "patients": [ 
> {"name" : "John Doe", "age" : 56, "smoker" : true},
> {"name" : "Mary Major", "age" : 85, "smoker" : false}
> ] 
> }
> 
> ```

This is a single document in the index. If you create a test index and ingest the above, the document count will be one. Therefore if the match is successful on this document you will see both John and Mary in the result.
