栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何通过使用Elasticsearch聚合返回唯一文档的数量

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何通过使用Elasticsearch聚合返回唯一文档的数量

我认为您需要

reverse_nested
聚合,因为您希望基于嵌套值进行聚合,但实际上是在计算ROOT文档,而不是嵌套文档

{  "query": {    "bool": {      "must": [        {          "term": { "last_name": "smith"          }        }      ]    }  },  "aggs": {    "location": {      "nested": {        "path": "location"      },      "aggs": {        "state": {          "terms": { "field": "location.state", "size": 10          },          "aggs": { "top_reverse_nested": {   "reverse_nested": {} }          }        }      }    }  }}

结果,您将看到类似以下的内容:

"aggregations": {      "location": {         "doc_count": 6,         "state": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [    {       "key": "ny",       "doc_count": 4,       "top_reverse_nested": {          "doc_count": 2       }    },    {       "key": "ca",       "doc_count": 2,       "top_reverse_nested": {          "doc_count": 2       }    } ]         }      }   }

而您正在寻找的

top_reverse_nested
部分内容。这里要指出的一点是:如果我没有记错的
"doc_count":6
是NESTED文档计数,那么不要以为您正在计算根文档而对这些数字感到困惑,因为计数是嵌套的。因此,对于具有三个匹配的嵌套文档的文档,计数为3,而不是1。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/382914.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号