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

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".

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