# Nested Query: Nested document returns unmatched documents

**URL:** <https://discuss.elastic.co/t/nested-query-nested-document-returns-unmatched-documents/272096>\
**Category:** Elasticsearch\
**Created:** [May 4, 2021, 5:17pm UTC](https://discuss.elastic.co/t/nested-query-nested-document-returns-unmatched-documents/272096 "2021-05-04T17:17:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tkumar](https://avatars.discourse-cdn.com/v4/letter/t/f04885/32.png) [@tkumar](https://discuss.elastic.co/u/tkumar)\
**Post date:** [May 4, 2021, 5:17pm UTC](https://discuss.elastic.co/t/nested-query-nested-document-returns-unmatched-documents/272096/1 "2021-05-04T17:17:23Z")

</div>

Hi -  
We have indexed the document using nested mapping and the document also created in nested format in the Elasticsearch. But while retrieving using nested query it also returns unmatched documents from both the datasets. We want only the documents which match all the 3 conditions.

Sharing the mapping, elastic data and nested query for reference.

## mapping

"services": {  
"type": "nested",  
"properties": {  
"id": {  
"type": "long"  
},  
"name": {  
"type": "text",  
"fields": {  
"keyword": {  
"type": "keyword",  
"ignore\_above": 256  
}  
}  
},  
"priorities": {  
"type": "nested",  
"properties": {  
"id": {  
"type": "long"  
},  
"name": {  
"type": "text",  
"fields": {  
"keyword": {  
"type": "keyword",  
"ignore\_above": 256  
}  
}  
},  
"phases": {  
"type": "nested",  
"properties": {  
"id": {  
"type": "long"  
},  
"name": {  
"type": "text",  
"fields": {  
"keyword": {  
"type": "keyword",  
"ignore\_above": 256  
}  
}  
}  
}  
}  
}

## Search Data

services:  
{  
"id": 1100,  
"name": "B2B ",  
"priorities": [  
{  
"id": 1125,  
"name": "Create Test",  
"phases": [  
{  
"id": 1128,  
"name": "Acc"  
}  
]  
},  
{  
"id": 1121,  
"name": "Demonstrate Marketing",  
"phases": [  
{  
"id": 1122,  
"name": "Def"  
},  
{  
"id": 1123,  
"name": "App"  
},  
{  
"id": 1124,  
"name": "Acc"  
}  
]  
}  
]  
}

## Nested Query

{  
"query" :{  
"bool" : {  
"must" : [  
{  
"nested" : {  
"query" : {

```
                    "match_phrase": {
                      "services.id" : {
                        "query" : "1100",
                        "slop" : 0,
                        "zero_terms_query" : "NONE",
                        "boost" : 1.0
                      }
                    }
            },
            "path" : "services",
            "ignore_unmapped" : false,
            "score_mode" : "max",
            "boost" : 1.0,
            "inner_hits" : {
              "name" : "servicesHits",
              "ignore_unmapped" : false,
              "from" : 0,
              "size" : 3,
              "version" : false,
              "seq_no_primary_term" : false,
              "explain" : false,
              "track_scores" : false
            }
          }
   
  },
  {
      "nested" : {
            "query" : {
                "match_phrase" : {
                      "services.priorities.id" : {
                        "query" : "1125",
                        "slop" : 0,
                        "zero_terms_query" : "NONE",
                        "boost" : 1.0
                      }
                    }
                },
            "path" : "services.priorities",
            "ignore_unmapped" : false,
            "score_mode" : "max",
            "boost" : 1.0
          }
  },
  {
      "nested" : {
            "query" : {
                "match_phrase": {
                      "services.priorities.phases.id" : {
                        "query" : "1128",
                        "slop" : 0,
                        "zero_terms_query" : "NONE",
                        "boost" : 1.0
                      }
                    }
            },
            "path" : "services.priorities.phases",
            "ignore_unmapped" : false,
            "score_mode" : "max",
            "boost" : 1.0
          }
   
  }
],
"adjust_pure_negative" : true,
"boost" : 1.0

```

}  
}  
}

---

<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:** [June 1, 2021, 5:18pm UTC](https://discuss.elastic.co/t/nested-query-nested-document-returns-unmatched-documents/272096/2 "2021-06-01T17:18:14Z")

</div>

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