You can do this in one go with a scripted_metric
aggregation but you'd have to write the percentiles function and then the avg function -- I wrote one here. But the script isn't going to be performant so I don't think it'd be worth the effort…
I'd instead suggest to first retrieve the percentile bounds:
POST myindex/_search
{
"size": 0,
"aggs": {
"boundaries": {
"percentiles": {
"field": "value",
"percents": [
10,
90
]
}
}
}
}
yielding [1.5, 9.5]
and then plug these numbers into a weighted average aggregation:
POST myindex/_search
{
"size": 0,
"aggs": {
"avg_without_outliers": {
"weighted_avg": {
"value": {
"field": "value"
},
"weight": {
"script": {
"source": "def v = doc.value.value; return v <= params.min || v >= params.max ? 0 : 1",
"params": {
"min": 1.5,
"max": 9.5
}
}
}
}
}
}
}
The weight
is either 0 or 1, depending on whether the particular doc that's being traversed is an outlier or not.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…