Would it be possible to script the sort order of aggregations? Let me
explain with a contrived example:
{
"aggs": {
"agg1": {
"terms": {
"field": "somefield",
"order": {
"avg1": "desc"
},
"size" : 100
},
"aggs": {
"avg1": {
"avg": {
"field": "someotherfield"
}
}
}
}
}
The above example will return an aggregation where the buckets are
sorted based on the average of some other field of the bucketized
documents. Pretty easy. What I would actually want is to do some more
complex math such as the average of the bucketized documents times the
number of documents in the bucket.
The metric aggregation script, as far as I can tell, does not have
access to information of the bucket it is in.
How to arrange something like this
"order": {
"_script": {
"script": "params.listing.indexOf((int)doc['id'].value)",
"type": "number",
"order": "desc",
"params": [1,2,3]
}
}