【问题标题】:Include parent _source fields in nested top hits aggregation在嵌套的热门点击聚合中包含父 _source 字段
【发布时间】:2021-01-26 13:36:45
【问题描述】:

我正在尝试聚合一个字段并使用top_ hits 获取顶部记录,但我想在响应中包含嵌套属性映射中未包含的其他字段。目前,如果我指定_source:{"include":[]},我只能获取当前嵌套属性中的字段。

这是我的地图

{
  "my_cart":{
    "mappings":{
      "properties":{
        "store":{
          "properties":{
            "name":{
              "type":"keyword"
            }
          }
        },
        "sales":{
          "type":"nested",
          "properties":{
            "Price":{
              "type":"float"
            },
            "Time":{
              "type":"date"
            },
            "product":{
              "properties":{
                "name":{
                  "type":"text",
                  "fields":{
                    "keyword":{
                      "type":"keyword"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
  

更新

Joe 的回答解决了我的上述问题。

我目前的回应问题是,虽然我将产品名称作为“关键”和其他详细信息,但我在结算收据中作为该交易的一部分的点击中也获得了其他产品名称。我想汇总产品名称并查找每个产品的最后销售日期以及价格、数量等其他详细信息。

当前反应

"aggregations" : {
    "aggregate_by_most_sold_product" : {
      "doc_count" : 2878592,
      "all_products" : {
        "buckets" : [
          {
            "key" : "shampoo",
            "doc_count" : 1,
            "lastSold" : {
              "value" : 1.602569793E12,
              "value_as_string" : "2018-10-13T06:16:33.000Z"
            },
            "using_reverse_nested" : {
              "doc_count" : 1,
              "latest product" : {
                "hits" : {
                  "total" : {
                    "value" : 1,
                    "relation" : "eq"
                  },
                  "max_score" : 0.0,
                  "hits" : [
                    {
                      "_index" : "my_cart",
                      "_type" : "_doc",
                      "_id" : "36303258-9r7w-4b3e-ba3d-fhds7cfec7aa",
                      "_source" : {
                        "cashier" : {
                          "firstname" : "romeo",
                          "uuid" : "2828dhd-0911-7229-a4f8-8ab80dde86a6"
                        },
                       "product_price": {
                       "price":20,
                       "discount_offered":10
                        },

                        "sales" : [
                          {
                            "product" : {
                              "name" : "shampoo",
                               "time":"2018-10-13T04:44:26+00:00
                            },
                             "product" : {
                              "name" : "noodles",
                              "time":"2018-10-13T04:42:26+00:00
                            },
                              "product" : {
                              "name" : "biscuits",
                              "time":"2018-10-13T04:41:26+00:00
                            }
                            }
                            ]
                              }
                             }
                            ]
                             }
}
]


预期响应

它为我提供了该交易中的所有产品名称,这增加了存储桶的大小。我只想要单个产品名称以及最后销售日期以及每个产品的其他详细信息。

我的聚合与回答中 Joe 的聚合相同

另外我的疑问是我是否还可以添加脚本来对我在 _source 中获得的字段执行操作。

Ex:- price-discount_offered = 最终金额。

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation elasticsearch-dsl


    【解决方案1】:

    除非您使用reverse_nested,否则nested 上下文无权访问父级。但是,在这种情况下,您将失去仅检索适用的嵌套子文档的能力。不过幸好有办法sort a terms aggregation by the result of a different, numeric one

    GET my_cart/_search
    {
      "size": 0,
      "aggs": {
        "aggregate": {
          "nested": {
            "path": "sales"
          },
          "aggs": {
            "all_products": {
              "terms": {
                "field": "sales.product.name.keyword",
                "size": 6500,
                "order": {                                <--
                  "lowest_date": "asc"
                }
              },
              "aggs": {
                "lowest_date": {                          <--
                  "min": {
                    "field": "sales.Time"
                  }
                },
                "using_reverse_nested": {
                  "reverse_nested": {},                   <--
                  "aggs": {
                    "latest product": {
                      "top_hits": {
                        "_source": {
                          "includes": [
                            "store.name"
                          ]
                        },
                        "size": 1
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    

    需要注意的是,您不会在top_hits 中获得store.name——尽管我怀疑您可能已经在客户端进行了一些后处​​理,您可以在其中合并这些条目:

    "aggregate" : {
      ...
      "all_products" : {
        ...
        "buckets" : [
          {
            "key" : "myproduct",                     <--
            ...
            "using_reverse_nested" : {
              ...
              "latest product" : {
                "hits" : {
                  ...
                  "hits" : [
                    {
                      ...
                      "_source" : {
                        "store" : {
                          "name" : "mystore"         <--
                        }
                      }
                    }
                  ]
                }
              }
            },
            "lowest_date" : {
              "value" : 1.4200704E12,
              "value_as_string" : "2015/01/01"       <--
            }
          }
        ]
      }
    }
    

    【讨论】:

    • 非常感谢您抽出时间来回答,您的方法很完美,但我仍然得到不准确的数据作为回应。问题是我有所有账单的记录,我想找出每家商店的最新交易。在一张账单中可以有不同商店的多条记录。目前它的汇总账单是明智的最后一笔交易,而不是所有账单中某个商店的每笔最后一笔交易。
    • 没有问题。您能否更新您的问题并更清楚地描述该问题(通过示例)?
    • 我已经更新了问题摘要,请检查并告诉我。
    • 我认为您已将我的查询从答案中更改,因为我指定仅包含 store.name,但您的点击包含整个 _source
    • 是的,乔,我确实更改了它,因为我还需要响应中的其他字段,例如收银员姓名等。为了简短起见,我没有包含映射的其他属性。
    猜你喜欢
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多