Mapping is given below:-
"properties": {
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"accounts": {
"type": "nested",
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"createdOn": {
"type": "date"
},
"updatedOn": {
"type": "date"
}
}
}
}
Scripted Query is :-
{
"query": {
"bool": {
"must":[
{
"nested": {
"path": "accounts",
"query": {
"bool": {
"filter" : {
"script" : {
"script" : "if( doc.containsKey('accounts') && doc['accounts'].size() == 1 ) { return true } else {return false}"
}
}
}
}
}
}
]
}
},
"size":1
}
doc.containsKey('accounts') always returns false even if there are data is present.
Elasticsearch Version : 5.5.2