elasticsearch - 在嵌套热门点击聚合中包含父 _source 字段

标签 elasticsearch elasticsearch-aggregation elasticsearch-dsl

我正在尝试聚合一个字段并使用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"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
  

更新

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

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

当前响应

"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
                            }
                            }
                            ]
                              }
                             }
                            ]
                             }
}
]


预期响应

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

我的聚合与乔在回答中的聚合相同

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

例如:-price-discount_offered = 最终金额。

最佳答案

除非您使用reverse_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"       <--
        }
      }
    ]
  }
}

关于elasticsearch - 在嵌套热门点击聚合中包含父 _source 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64314470/

相关文章:

elasticsearch - 如何让 Elastic Search 聚合来镜像查询过滤逻辑?

javascript - 尝试在ElasticSearch中进行查询和汇总,但无法正常工作-Elasticsearch.js客户端

elasticsearch - 数组中重复值的value_count而不是doc_count

elasticsearch - Elasticsearch何时确定文档的索引?

python - 如何在 python 中使用 elasticsearch 编写一个简单的正则表达式查询?

elasticsearch - Logstash-FileBeat多行设置将太多消息分组

elasticsearch - analyzed v not_analyzed 还是...?

elasticsearch - 使用elasticsearch在两个值之间搜索价格

elasticsearch - 按嵌套字段匹配的百分比对 ElasticSearch 结果进行排序

elasticsearch - 多键多前缀搜索