How to get array count of nested of nested object

2019-08-02 14:24发布

问题:

Can someone please help me to get an aggregated count of the nested sets object in elastic search, let say if my elastic search object as :

{
  "empId":12121,
  "entities": [
    {
      "sets": [
        {
          "setId": 1
        }
      ]
    }
  ]
}

entities are the type of array which contains an another array called sets. I wanted to get the count of sets of the filtered item.

{
"query": {
  "bool": {
    "filter": [
      {
        "terms": {
          "mediaItemId": [346754750,346745565]
        }
      }
    ]
  }
},
"size": 0,
"aggs": {
  "entities_agg": {
    "sum": {
      "script": {
        "inline": "params._source.entities.sets.size()"
      }
    }
  }
}
}