Is there a way to Order by ABS(SUM()) in ES 2.3.5?
Looks like I can only do ABS(value) as part of aggregation but not during the order part.
SQL Example
DECLARE @table TABLE
(
Name VARCHAR(50),
Value FLOAT
)
INSERT INTO @table
SELECT 'First', -15.1
UNION SELECT 'Third', 7.3
UNION SELECT 'Third', -7.3
UNION SELECT 'fourth', -2.4
UNION SELECT 'Second', 12.2
SELECT TOP 3 Name, SUM(Value) [Value] FROM @table
GROUP BY Name
ORDER BY ABS(SUM(Value)) DESC
Result
First,-15.1
Second,12.2
Fourth,-2.4
ES Example
POST /_bulk
{ "create": { "_index": "sorttest", "_type": "sortitem" }}
{ "name": "First", "value": -15.1 }
{ "index": { "_index": "sorttest", "_type": "sortitem" }}
{ "name": "Third", "value": 7.3 }
{ "index": { "_index": "sorttest", "_type": "sortitem" }}
{ "name": "Third", "value": -7.3 }
{ "index": { "_index": "sorttest", "_type": "sortitem" }}
{ "name": "fourth", "value": -2.4 }
{ "index": { "_index": "sorttest", "_type": "sortitem" }}
{ "name": "Second", "value": 12.2 }
ES Request
GET /sorttest/_search
{
"size": 0,
"aggs": {
"groupByName": {
"terms": {
"field": "name",
"size": 3,
"order": {
"sumabsvalue": "desc"
}
},
"aggs": {
"sumvalue": {
"sum": {
"field": "value"
}
},
"sumabsvalue": {
"sum": {
"field": "value",
"script": "abs(_value)"
}
}
}
}
}
}
Result
First,-15.1
Third,0
Second,12.2
Any help appreciated.
M