首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何根据层次结构字段值(字段的一部分)创建子桶聚合

如何根据层次结构字段值(字段的一部分)创建子桶聚合
EN

Stack Overflow用户
提问于 2022-03-08 13:41:19
回答 1查看 47关注 0票数 0

假设我们有一个具有如下层次结构字段的文档:

代码语言:javascript
复制
POST subbuckets/_doc
{
  "hierarchy": "this/is/some/hierarchy"
}

POST subbuckets/_doc
{
  "hierarchy": "this/is/some/hierarchy2"
}

POST subbuckets/_doc
{
  "hierarchy": "this/is/another/hierarchy1"
}

我想计算属于的文档数量,每个层次结构级别为I.e。

  • "this"层次结构层有3个documents
  • "this/is"层次层、3个documents
  • "this/is/some"层次层、2个documents
  • "this/is/another"层次层、1个document
  • "this/is/another/hierarchy1"层次层、1个document
  • "this/is/some/hierarchy"层次层、1个document
  • "this/is/some/hierarchy2"层次层和1个文档

级别。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-08 14:01:52

我们不能在keyword上应用分析器,因此要解决这个问题,必须将字段定义为text类型,并在text字段上启用聚合并设置"fielddata": true。请检查下面的配置。

索引映射:

代码语言:javascript
复制
PUT index5
{
  "settings": {
    "analysis": {
      "analyzer": {
        "path-analyzer": {
          "tokenizer": "path-tokenizer"
        }
      },
      "tokenizer": {
        "path-tokenizer": {
          "type": "path_hierarchy",
          "delimiter": "/"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "hierarchy": {
        "type": "text",
        "analyzer": "path-analyzer",
        "search_analyzer": "keyword",
        "fielddata": true
      }
    }
  }
}

索引文档

代码语言:javascript
复制
POST index5/_doc
{
  "hierarchy": "this/is/some/hierarchy"
}

POST index5/_doc
{
  "hierarchy": "this/is/some/hierarchy2"
}

POST index5/_doc
{
  "hierarchy": "this/is/another/hierarchy1"
}

查询:

代码语言:javascript
复制
POST index5/_search
{
  "aggs": {
    "path": {
      "terms": {
        "field": "hierarchy"
      }
    }
  },
  "size": 0
}

响应:

代码语言:javascript
复制
{
 "aggregations" : {
    "path" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "this",
          "doc_count" : 3
        },
        {
          "key" : "this/is",
          "doc_count" : 3
        },
        {
          "key" : "this/is/some",
          "doc_count" : 2
        },
        {
          "key" : "this/is/another",
          "doc_count" : 1
        },
        {
          "key" : "this/is/another/hierarchy1",
          "doc_count" : 1
        },
        {
          "key" : "this/is/some/hierarchy",
          "doc_count" : 1
        },
        {
          "key" : "this/is/some/hierarchy2",
          "doc_count" : 1
        }
      ]
    }
  }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71396029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档