# How to get matched documents from ElasticSearch for a nested array fields matching specified values

**URL:** https://discuss.elastic.co/t/how-to-get-matched-documents-from-elasticsearch-for-a-nested-array-fields-matching-specified-values/342724
**Category:** Elasticsearch
**Created:** [September 11, 2023, 9:12am UTC](https://discuss.elastic.co/t/how-to-get-matched-documents-from-elasticsearch-for-a-nested-array-fields-matching-specified-values/342724 "2023-09-11T09:12:04Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Nid](https://avatars.discourse-cdn.com/v4/letter/n/b487fb/32.png) [@Nid](https://discuss.elastic.co/u/Nid)
#### Post date: [September 11, 2023, 9:12am UTC](https://discuss.elastic.co/t/how-to-get-matched-documents-from-elasticsearch-for-a-nested-array-fields-matching-specified-values/342724/1 "2023-09-11T09:12:04Z")

</div>

I am using elasticsearch-8.7.0. . I am a beginner, stuck on one thing. Please help. I have defined a mapping as below:  
mappings = {  
"properties": {  
"change\_id": {"type": "text", "analyzer": "english"},  
"changes": { "type": "nested",  
"properties":  
{  
"file": {"type": "text", "analyzer": "english"},  
"diff": {"type": "text", "analyzer": "standard"},  
}  
} }  
}

Diff is the Perforce diff extracted using python, spaces removed and dumped in ES.

Sample data:  
{ "change\_id": 1,  
"changes": [ {  
"file": "1A",  
"diff": "1A\_very\_large\_string" },  
{  
"file": "1B",  
"diff": "1B\_very\_large\_string\_1" }  
],  
"change\_id": 2,  
"changes": [ {  
"file": "2A",  
"diff": "2A\_very\_large\_string" },  
{  
"file": "1B",  
"diff": "1B\_very\_large\_string\_2" }  
], }`

I am trying to find documents in which changes array match the given file and diff both of them using below query: `  
query= { "bool": { "must": [{

"nested": {  
"path": "changes",  
"query": {  
"bool": {  
"must": [  
{  
"match": {  
"changes.diff": "1B\_very\_large\_string\_2"  
}  
},  
{  
"match": {  
"changes.file": "1B"  
}  
}  
]  
}  
}  
},  
}]  
}  
}  
For quering I using Python language.  
Expectation: change\_id : 2 only doc should be returned  
Result: Both the docs are returned as both contains same file "1B".

---

<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: [October 9, 2023, 9:12am UTC](https://discuss.elastic.co/t/how-to-get-matched-documents-from-elasticsearch-for-a-nested-array-fields-matching-specified-values/342724/2 "2023-10-09T09:12:09Z")

</div>

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