gpt4 book ai didi

elasticsearch - 将父_source字段包括在嵌套的热门匹配中

转载 作者:行者123 更新时间:2023-12-02 23:07:20 26 4
gpt4 key购买 nike

我试图在一个字段上聚合,并使用top_ hits获取最高记录,但我想在响应中包括嵌套属性映射中未包括的其他字段。当前,如果我指定_source:{"include":[]},那么我只能获得当前嵌套属性中的字段。
这是我的 map

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


预期响应
它为我提供了该交易中所有产品的名称,从而增加了存储桶的大小。我只想要具有最后销售日期的单个产品名称,以及每个产品的其他详细信息。
我的汇总与答案中乔的汇总相同
另外,我的 怀疑是,我是否也可以添加脚本以对_source中获得的字段执行操作。
,例如:-price-discount_offered =最终金额。

最佳答案

除非您使用 nested ,否则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
}
}
}
}
}
}
}
}
}
}
需要注意的是,您不会将 store.name放入 top_hits内-尽管我怀疑您可能已经在客户端进行了一些后期处理,可以合并这些条目:
"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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com