Can someone please help me to get an aggregated count of the nested object in elastic search, let say if my elastic search object mapping as :
{
"employe": {
"dynamic": "strict",
"properties": {
"empId":{
"type": "keyword"
},
"entities": {
"type": "nested"
}
}
}
}
entities are the type of array with some other object. I wanted to get the count of entities of the filtered item. I have tried some elastic search query, but it does not work
{
"query": {
"bool": {
"filter": [
{
"terms": {
"empId": [12121,2121212]
}
}
]
}
},
"size": 0,
"aggs": {
"entities_agg": {
"sum": {
"field": "entities",
"script": {
"inline": "doc['entities'].values.size()"
}
}
}
}
}
You cannot access nested data via doc values, you need to access the source document instead, like this:
{
"query": {
"bool": {
"filter": [
{
"terms": {
"empId": [
12121,
2121212
]
}
}
]
}
},
"size": 0,
"aggs": {
"entities_agg": {
"sum": {
"script": {
"inline": "params._source.entities.size()"
}
}
}
}
}