According to your question, you have to return only the matching object from the array based on the query.
On the basis of Elasticsearch Array docs
Arrays of objects do not work as you would expect: you cannot query
each object independently of the other objects in the array. If you
need to be able to do this then you should use the nested datatype
instead of the object datatype.
If you want to query on each object of the array and return only the specific object from the array, you need to use a nested query with inner_hits. But for using the nested query, you have to create a new index with nested mapping
Adding a working example with index mapping, index data, search query, and search result
Index Mapping:
PUT <index-name>
{
"mappings": {
"properties": {
"metadata":{
"type":"nested"
}
}
}
}
Index Data:
POST <index-name>/_doc/1
{
"metadata": [
{
"key": "type",
"value": "animal"
},
{
"key": "animal",
"value": "cat"
}
]
}
Search Query:
POST <index-name>/_search
{
"query": {
"nested": {
"path": "metadata",
"query": {
"match": {
"metadata.key": "animal"
}
},
"inner_hits": {}
}
}
}
Search Result:
"inner_hits" : {
"metadata" : {
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.6931471,
"hits" : [
{
"_index" : "70711376",
"_type" : "_doc",
"_id" : "1",
"_nested" : {
"field" : "metadata",
"offset" : 1
},
"_score" : 0.6931471,
"_source" : {
"value" : "cat",
"key" : "animal"
}
}
]
}
}
}