【问题标题】:is there a way of showing documents after a sum aggregation?有没有办法在总和聚合后显示文档?
【发布时间】:2021-10-23 06:43:25
【问题描述】:

我最近一直在尝试检索有关 Kibana DSL 的销售信息。 我被告知要显示供应商信息以及他们的月销售额。 (我将在本示例中使用“Kibana_sample_data_ecommerce”)

我已经进行了此聚合,以便按所有客户的“customer_id”对他们进行分组:

#Aggregations (group by)
GET kibana_sample_data_ecommerce/_search
{
  "size": 0,  
    "aggs": {
      "by user_id": {
        "terms": {
          "field": "customer_id"
          },
          "aggs": {
            "add_field_to_bucket": {
              "top_hits": {"size": 1, "_source": {"includes": ["customer_full_name"]}}
            }
          }
      }
    }
 }

我在结果中包含了 customer_full_name:

"aggregations" : {
    "by user_id" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 2970,
      "buckets" : [
        {
          "key" : "27",
          "doc_count" : 348,
          "add_field_to_bucket" : {
            "hits" : {
              "total" : 348,
              "max_score" : 1.0,
              "hits" : [
                {
                  "_index" : "kibana_sample_data_ecommerce",
                  "_type" : "_doc",
                  "_id" : "fhwUR3sBpfDKGuVlpu8r",
                  "_score" : 1.0,
                  "_source" : {
                    "customer_full_name" : "Elyssa Underwood"
                  }
                }
              ]
            }
          }
        }

因此,在这个结果中,我知道带有“customerid”“27”的“Elyssa Underwood”有 348 次点击(或相关文档)。 我还需要知道“Elyssa”在这些产品上的总花费,使用“products.taxful_price”字段。 问题是我无法对 top_hits 执行子聚合(据我所知);我也尝试过 sum_aggregation,但它以相同的结果结束(我得到了我的总和,但当时我无法访问 top_hits 子聚合)。

在一天结束时,我希望得到这样的结果:

 "hits" : [
                {
                  "_index" : "kibana_sample_data_ecommerce",
                  "_type" : "_doc",
                  "_id" : "fhwUR3sBpfDKGuVlpu8r",
                  "_score" : 1.0,
                  "_source" : {
                    "customer_full_name" : "Elyssa Underwood",
                    "total_spent": 1234.5678 
                  }
                }
              ]

我可以做些什么来实现它吗?

PS:我使用的是 ElasticSearch 5.x,如果有解决方案,我可以访问 NEST 客户端。

提前致谢。

【问题讨论】:

  • fwiw Elasticsearch 5.X 已经停产多年,你真的应该升级 :)
  • 我知道,但这是安装在我正在工作的新公司上的环境。我正在尝试提出解决方案。

标签: elasticsearch dsl elasticsearch-5


【解决方案1】:

我使用下面的数据作为示例数据。

数据:

{
  "customer_id":2,
  "client-name":"b",
  "purchase": 2001
}

查询:

GET index/_search
{
  "size": 0, 
  "aggs": {
    "NAME": {
      "terms": {
        "field": "customer_id",
        "size": 10
      },
      "aggs": {
        "total_sales": {
          "sum": {
            "field": "purchase"
          }
        },
        "documents":{
          "top_hits": {
            "size": 10
          }
        }
      }
    }
  }  
}

结果:

{
          "key" : 2,
          "doc_count" : 1,
          "documents" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 1.0,
              "hits" : [
                {
                  "_index" : "index1",
                  "_type" : "_doc",
                  "_id" : "0HPzcHsBjw4ziwrzGzrq",
                  "_score" : 1.0,
                  "_source" : {
                    "customer_id" : 2,
                    "client-name" : "b",
                    "purchase" : 2001
                  }
                }
              ]
            }
          },
          "total_sales" : {
            "value" : 2001.0
          }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 2020-07-14
    • 2019-05-27
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2022-01-17
    相关资源
    最近更新 更多