I have a requirement in the terms query for a nested field in elastic-search where the nested field values should match exactly with the number of values provided in the terms query. For example, consider the below query where we have terms query on the nested field named Types.
GET assets/_search
{
"size": "10",
"from": 0,
"query": {
"bool": {
"must": [
{
"nested": {
"path": "Types",
"query": {
"bool": {
"must": [
{
"term": {
"Types.Label.keyword":
["VOD, AOP"]
}
}
]
}
}
}
}
]
}
}
}
Index Mapping
{
"media-assets": {
"mappings": {
"dynamic": "true",
"properties": {
"Types": {
"type": "nested",
"properties": {
"Label": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256,
"normalizer": "lowercase_normalizer"
},
"ngram": {
"type": "text",
"analyzer": "ngram_tokenizer_analyzer"
}
}
}
}
}
}
}
}
}
Sample Document:
{
"_source": {
"AssetId": 1657352,
"MaterialId": "XBV01001",
"AirDate": "1997-03-10T00:00:00Z",
"Types": [
{
"Type": "AOP"
},
{
"Type": "VOD"
}
]
}
}
The above query should return documents where the field Types has exactly 2 values i.e "VOD" & "AOP".If a document has these 2 values along with some other values in the field Types, then it should not return that document because the number of terms provided in the query should match with the number of values present in the Types field.