{"action":"created","issue":{"url":"https://api.github.com/repos/elastic/elasticsearch/issues/8486","labels_url":"https://api.github.com/repos/elastic/elasticsearch/issues/8486/labels{/name}","comments_url":"https://api.github.com/repos/elastic/elasticsearch/issues/8486/comments","events_url":"https://api.github.com/repos/elastic/elasticsearch/issues/8486/events","html_url":"https://github.com/elastic/elasticsearch/issues/8486","id":48807850,"number":8486,"title":"Order by scripted_metric sub aggregation","user":{"login":"eryabitskiy","id":994670,"avatar_url":"https://avatars.githubusercontent.com/u/994670?v=3","gravatar_id":"","url":"https://api.github.com/users/eryabitskiy","html_url":"https://github.com/eryabitskiy","followers_url":"https://api.github.com/users/eryabitskiy/followers","following_url":"https://api.github.com/users/eryabitskiy/following{/other_user}","gists_url":"https://api.github.com/users/eryabitskiy/gists{/gist_id}","starred_url":"https://api.github.com/users/eryabitskiy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eryabitskiy/subscriptions","organizations_url":"https://api.github.com/users/eryabitskiy/orgs","repos_url":"https://api.github.com/users/eryabitskiy/repos","events_url":"https://api.github.com/users/eryabitskiy/events{/privacy}","received_events_url":"https://api.github.com/users/eryabitskiy/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/elastic/elasticsearch/labels/:Aggregations","name":":Aggregations","color":"009800"},{"url":"https://api.github.com/repos/elastic/elasticsearch/labels/high%20hanging%20fruit","name":"high hanging fruit","color":"009800"}],"state":"open","locked":false,"assignee":{"login":"colings86","id":236731,"avatar_url":"https://avatars.githubusercontent.com/u/236731?v=3","gravatar_id":"","url":"https://api.github.com/users/colings86","html_url":"https://github.com/colings86","followers_url":"https://api.github.com/users/colings86/followers","following_url":"https://api.github.com/users/colings86/following{/other_user}","gists_url":"https://api.github.com/users/colings86/gists{/gist_id}","starred_url":"https://api.github.com/users/colings86/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/colings86/subscriptions","organizations_url":"https://api.github.com/users/colings86/orgs","repos_url":"https://api.github.com/users/colings86/repos","events_url":"https://api.github.com/users/colings86/events{/privacy}","received_events_url":"https://api.github.com/users/colings86/received_events","type":"User","site_admin":false},"milestone":null,"comments":8,"created_at":"2014-11-14T16:18:46Z","updated_at":"2015-05-18T19:27:07Z","closed_at":null,"body":"Since there is a new Scripted metric aggregation (scripted_metric) in 1.4, it is possible to do a lot of amazing stuff. \r\nFor example it is possible to implement Weighted Average aggregation, which we were missing before.\r\n\r\nNow we are really missing a **possibility to sort by scripted_metric results**.\r\n\r\n**Live example**:\r\n\r\nWe calculate **weightedAvgVis** with scripted_metric and want to get ids with **TOP 5 values of weightedAvgVis**. Since script returns double, it looks logically possible. \r\n\r\n```json\r\n{  \r\n   \"from\":0,\r\n   \"size\":0,\r\n   \"query\":{  \r\n      \"match_all\":{   }\r\n   },\r\n   \"aggregations\":{  \r\n      \"idNodes\":{  \r\n         \"terms\":{  \r\n            \"field\":\"id\",\r\n            \"size\":5,\r\n            \"order\":{  \r\n               \"weightedAvgVis\":\"asc\"\r\n            }\r\n         },\r\n         \"aggregations\":{  \r\n            \"weightedAvgVis\":{  \r\n               \"scripted_metric\":{  \r\n                  \"init_script\":\"_agg['weightedSum'] = 0d; _agg['countSum'] = 0L;\",\r\n                  \"map_script\":\"_agg['weightedSum'] = _agg.weightedSum + _source['avgVis'] * _source['count']; _agg['countSum'] = _agg.countSum + _source['count'];\",\r\n                  \"reduce_script\":\"weightedSum = 0d; countSum = 0L; for(a in _aggs) {weightedSum += a.weightedSum; countSum += a.countSum;};if(countSum == 0L) {return 0d;} else {return weightedSum / countSum}\"\r\n               }\r\n            }\r\n         }\r\n      }\r\n   }\r\n}\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/elastic/elasticsearch/issues/comments/103182369","html_url":"https://github.com/elastic/elasticsearch/issues/8486#issuecomment-103182369","issue_url":"https://api.github.com/repos/elastic/elasticsearch/issues/8486","id":103182369,"user":{"login":"aaneja","id":1797669,"avatar_url":"https://avatars.githubusercontent.com/u/1797669?v=3","gravatar_id":"","url":"https://api.github.com/users/aaneja","html_url":"https://github.com/aaneja","followers_url":"https://api.github.com/users/aaneja/followers","following_url":"https://api.github.com/users/aaneja/following{/other_user}","gists_url":"https://api.github.com/users/aaneja/gists{/gist_id}","starred_url":"https://api.github.com/users/aaneja/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aaneja/subscriptions","organizations_url":"https://api.github.com/users/aaneja/orgs","repos_url":"https://api.github.com/users/aaneja/repos","events_url":"https://api.github.com/users/aaneja/events{/privacy}","received_events_url":"https://api.github.com/users/aaneja/received_events","type":"User","site_admin":false},"created_at":"2015-05-18T19:27:07Z","updated_at":"2015-05-18T19:27:07Z","body":"+1"}}