本文介绍了如何在弹性搜索中获取嵌套对象的数组数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我获取弹性搜索中嵌套对象的总计数吗,可以说我的弹性搜索对象映射为:

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

推荐答案

您无法通过doc值访问嵌套数据,而需要访问源文档,如下所示:

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

这篇关于如何在弹性搜索中获取嵌套对象的数组数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-18 08:01