ElasticSearch解决去重精度问题 scripted_metric 去重计数脚本计算高效率解决问题(二)
{
"size": 0,
"query": {
"bool": {
"must": [
{
"range": {
"purchase_date": {
"from": "2019-08-12 00:00:00",
"to": "2019-08-18 23:59:59",
"include_lower": true,
"include_upper": true,
"format": "yyyy-MM-dd HH:mm:ss",
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
"aggregations": {
"total_purchase_amount": {
"sum": {
"field": "total_purchase_amount",
"format": "0.000"
}
},
"purchase_quantity": {
"sum": {
"field": "purchase_quantity",
"format": "0"
}
},
"spu": {
"scripted_metric": {
"init_script": {
"source": "params._agg.map= new HashMap()",
"lang": "painless"
},
"map_script": {
"source": "if(!params._agg.(doc[''].value)){params._agg.(doc[''].value,1)}",
"lang": "painless"
},
"combine_script": {
"source": "return params._agg.().size()",
"lang": "painless"
},
"reduce_script": {
"source": "double profit = 0; for (a in params._aggs) { profit += a } return profit",
"lang": "painless"
}
}
}
}
}