Elasticsearch中路径层次令牌的层次聚合

xytpbqjk  于 2023-08-03  发布在  ElasticSearch
关注(0)|答案(1)|浏览(81)

我有一个分层数据的索引。以下是我的索引locations_hierarchy中的示例文档:

{
    "_index" : "locations_hierarchy",
    "_type" : "_doc",
    "_id" : "1",
    "_score" : 1.0,
    "_source" : {
      "category_path" : "Art Gallery, Paintings, Case 1"
    }
  },
  {
    "_index" : "locations_hierarchy",
    "_type" : "_doc",
    "_id" : "2",
    "_score" : 1.0,
    "_source" : {
      "category_path" : "Display Gallery, Small Worlds, Case 1, Shelf 2"
    }
}

字符串
下面是我想要实现的聚合:

{
          "key" : "Art Gallery",
          "doc_count" : 4,
          "category_sub" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "Paintings",
                "doc_count" : 2,
                "category_sub_sub" : {
                  "doc_count_error_upper_bound" : 0,
                  "sum_other_doc_count" : 0,
                  "buckets" : [
                    {
                      "key" : "Case 1",
                      "doc_count" : 1
                    },
                    {
                      "key" : "Case 3",
                      "doc_count" : 1
                    }
                  ]
                }
}


我曾尝试在聚合查询中使用include和exclude来聚合它们,但这是一个相当手动的过程,并且对于我不知道层次结构中的级别数量的情况不起作用。
在Elasticsearch中,有没有可能解决路径层次令牌字段的层次聚合?

yc0p9oo0

yc0p9oo01#

首先,我建议您的数据中包含hierarchical category fields。您可以通过在摄取期间解析数据来实现它。

我发现了一种方法来实现与当前数据的结果,你有,这不是你想要的,但希望它有帮助!

POST /test_aggs_hierarchy/_bulk
{"index": {"_id": "1"}}
{"category_path": "Art Gallery, Paintings, Case 1"}
{"index": {"_id": "2"}}
{"category_path": "Art Gallery, Sculptures, Case 1"}
{"index": {"_id": "3"}}
{"category_path": "Display Gallery, Small Worlds, Case 1, Shelf 2"}
{"index": {"_id": "4"}}
{"category_path": "Display Gallery, Small Worlds, Case 2, Shelf 1"}

个字符

结果:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 4,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "category_top": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "Case 1",
          "doc_count": 3,
          "category_sub": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "Art Gallery, Paintings, Case 1",
                "doc_count": 1
              },
              {
                "key": "Art Gallery, Sculptures, Case 1",
                "doc_count": 1
              },
              {
                "key": "Display Gallery, Small Worlds, Case 1, Shelf 2",
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "Art Gallery",
          "doc_count": 2,
          "category_sub": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "Art Gallery, Paintings, Case 1",
                "doc_count": 1
              },
              {
                "key": "Art Gallery, Sculptures, Case 1",
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "Display Gallery",
          "doc_count": 2,
          "category_sub": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "Display Gallery, Small Worlds, Case 1, Shelf 2",
                "doc_count": 1
              },
              {
                "key": "Display Gallery, Small Worlds, Case 2, Shelf 1",
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "Small Worlds",
          "doc_count": 2,
          "category_sub": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "Display Gallery, Small Worlds, Case 1, Shelf 2",
                "doc_count": 1
              },
              {
                "key": "Display Gallery, Small Worlds, Case 2, Shelf 1",
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "Case 2",
          "doc_count": 1,
          "category_sub": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "Display Gallery, Small Worlds, Case 2, Shelf 1",
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "Paintings",
          "doc_count": 1,
          "category_sub": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "Art Gallery, Paintings, Case 1",
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "Sculptures",
          "doc_count": 1,
          "category_sub": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "Art Gallery, Sculptures, Case 1",
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "Shelf 1",
          "doc_count": 1,
          "category_sub": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "Display Gallery, Small Worlds, Case 2, Shelf 1",
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "Shelf 2",
          "doc_count": 1,
          "category_sub": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "Display Gallery, Small Worlds, Case 1, Shelf 2",
                "doc_count": 1
              }
            ]
          }
        }
      ]
    }
  }
}

相关问题