提问:布和纸怕什么?

es 7.10 嵌套文档的聚合对被嵌套的文档筛选不生效的问题。

Elasticsearch | 作者 Jea | 发布于2021年12月21日 | 阅读数:314

我有一个索引,名字叫stat 是嵌套文档mapping如下
{
"stat": {
"mappings": {
"properties": {
"orderParent": {
"properties": {
"createTime": {
"type": "date"
},
"orderAmount": {
"type": "float"
},
"sendTime": {
"type": "date"
}
}
},
"orderRefund": {
"properties": {
"refundTime": {
"type": "date"
},
"refundMoney": {
"type": "float"
},
"goodsId": {
"type": "long"
},
"brandId": {
"type": "long"
}
}
}
}
}
}
}

可插入数据如下:
{
"orderParent": {
"createTime": 1640054394,
"orderAmount": 1893.62,
"sendTime": 0
},
"orderRefund": [
{
"refundTime": 1640054394,
"refundMoney": 66.88,
"brandId": 33322,
"goodsId": 2323
},
{
"refundTime": 0,
"refundMoney": 122.88,
"brandId": 9988,
"goodsId": 2111
}
]
}

那么现在有个问题。
如果按照date_histogram来按照日期计算退款完成的(refundtime不是0的)那么dsl如下:
{
"size": 0,
"aggs": {
"filter_done": {
"filter": {
"bool": {
"must_not": [
{
"term": {
"orderRefund.refundTime": 0
}
}
]
}
},
"aggs": {
"histogram_orderRefund_refundTime": {
"date_histogram": {
"field": "orderRefund.refundTime",
"calendar_interval": "year",
"time_zone": "Asia/Shanghai"
},
"aggs": {
"sub_sum_orderRefund_refundMoney": {
"sum": {
"field": "orderRefund.refundMoney"
}
}
}
}
}
}
}
}
即使改成aggs中写上filter出来的结果还是orderRefund中两组数据的 refundMoney相加的~。
问题:
我要如何处理这种情况的统计,需要怎么写或者怎么去改变mapping结构
尝试过filter,post_filter,nested 貌似都不行~
感谢!
已邀请:

Ombres

赞同来自: Jea

nested是可以的吧?以下是我的试验语句
PUT /mmt

PUT /mmt/_mapping
{
"properties": {
"orderParent": {
"type":"nested",
"properties": {
"createTime": {
"type": "date"
},
"orderAmount": {
"type": "float"
},
"sendTime": {
"type": "date"
}
}
},
"orderRefund": {
"type":"nested",
"properties": {
"refundTime": {
"type": "date"
},
"refundMoney": {
"type": "float"
},
"goodsId": {
"type": "long"
},
"brandId": {
"type": "long"
}
}
}
}
}


POST /mmt/_doc
{
"orderParent": {
"createTime": 1640054394,
"orderAmount": 1893.62,
"sendTime": 0
},
"orderRefund": [
{
"refundTime": 1643233393000,
"refundMoney": 66.88,
"brandId": 33322,
"goodsId": 2323
},
{
"refundTime": 0,
"refundMoney": 122.88,
"brandId": 9988,
"goodsId": 2111
}
]
}


GET /mmt/_search
{
"size": 0,
"aggs": {
"orderRefunds": {
"nested": {
"path": "orderRefund"
},
"aggs": {
"timeFilter": {
"filter": {
"bool": {
"must_not": [
{
"term": {
"orderRefund.refundTime": 0
}
}
]
}
},
"aggs": {
"histogram_orderRefund_refundTime": {
"date_histogram": {
"field": "orderRefund.refundTime",
"calendar_interval": "year",
"time_zone": "Asia/Shanghai"
},
"aggs": {
"sub_sum_orderRefund_refundMoney": {
"sum": {
"field": "orderRefund.refundMoney"
}
}
}
}
}
}
}
}
}
}

God_lockin

赞同来自:

你这个是object型的数据,ES会把里面的内容打平,如果你需要对嵌套里面的数据做精细化筛选需要用nested型

Jea - 一只猿

赞同来自:

暂时没有找到合适的办法,还是沿用了之前的:nested嵌套内容,在入数据的时候把不符合统计指标的删除掉。

要回复问题请先登录注册