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

如何获得具有多个字段的Elasticsearch聚合

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

如何获得具有多个字段的Elasticsearch聚合

从外观上看,您

tags
不是
nested
。为了使此聚合起作用,您需要它,
nested
以便an
id
和a 之间存在关联
name
。没有s
nested
的列表
id
只是一个数组,而
name
s
的列表是另一个数组:

    "item": {      "properties": {        "meta": {          "properties": { "tags": {   "type": "nested",<-- nested field   "include_in_parent": true,  <-- to, also, keep the flat array-like structure   "properties": {     "id": {       "type": "integer"     },     "name": {       "type": "string"     }   } }          }        }      }    }

另外,请注意,我已经在映射中添加了这一行

"include_in_parent":true
,这意味着您的
nested
标签也将像“平面”数组状结构一样工作。

因此,到目前为止,您在查询中拥有的所有内容仍然可以正常使用,而无需对查询进行任何更改。

但是,对于您的这个特定查询,聚合需要更改为以下内容:

{  "aggs": {    "baked_goods": {      "nested": {        "path": "item.meta.tags"      },      "aggs": {        "name": {          "terms": { "field": "item.meta.tags.id"          },          "aggs": { "name": {   "terms": {     "field": "item.meta.tags.name"   } }          }        }      }    }  }}

结果是这样的:

   "aggregations": {      "baked_goods": {         "doc_count": 9,         "name": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [    {       "key": 123,       "doc_count": 3,       "name": {          "doc_count_error_upper_bound": 0,          "sum_other_doc_count": 0,          "buckets": [  {     "key": "biscuits",     "doc_count": 3  }          ]       }    },    {       "key": 456,       "doc_count": 2,       "name": {          "doc_count_error_upper_bound": 0,          "sum_other_doc_count": 0,          "buckets": [  {     "key": "cakes",     "doc_count": 2  }          ]       }    },    .....


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

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

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