【发布时间】: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