How to get array count of nested object in elastic-search

删除回忆录丶 提交于 2019-12-11 14:38:25

问题


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()"
      }
    }
  }
 }
}

回答1:


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()"
        }
      }
    }
  }
}


来源:https://stackoverflow.com/questions/49250267/how-to-get-array-count-of-nested-object-in-elastic-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!