{"action":"created","issue":{"url":"https://api.github.com/repos/elastic/elasticsearch/issues/12336","labels_url":"https://api.github.com/repos/elastic/elasticsearch/issues/12336/labels{/name}","comments_url":"https://api.github.com/repos/elastic/elasticsearch/issues/12336/comments","events_url":"https://api.github.com/repos/elastic/elasticsearch/issues/12336/events","html_url":"https://github.com/elastic/elasticsearch/issues/12336","id":96035705,"number":12336,"title":"Ability to specify specific buckets in Aggregation Path syntax","user":{"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},"labels":[{"url":"https://api.github.com/repos/elastic/elasticsearch/labels/:Aggregations","name":":Aggregations","color":"009800"},{"url":"https://api.github.com/repos/elastic/elasticsearch/labels/adoptme","name":"adoptme","color":"207de5"},{"url":"https://api.github.com/repos/elastic/elasticsearch/labels/enhancement","name":"enhancement","color":"4a4ea8"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-07-20T10:31:45Z","updated_at":"2016-01-26T14:49:06Z","closed_at":"2016-01-26T14:49:06Z","body":"The syntax used in both the terms aggregation ordering and in the pipeline aggregations is at the moment limited to referencing numeric metric aggregations and single-bucket aggregations. We should extend the syntax so users are able to specify a specific bucket in a multi-bucket aggregation. So, for example you could do the following:\r\n\r\n```json\r\n{\r\n  \"aggs\": {\r\n    \"fruits\": {\r\n      \"terms\": {\r\n        \"field\": \"fruit\",\r\n        \"order\": {\r\n          \"colours.yellow\": \"asc\"\r\n        }\r\n      },\r\n      \"aggs\": {\r\n        \"colours\": {\r\n          \"terms\": {\r\n            \"field\": \"colour\"\r\n          }\r\n        }\r\n      }\r\n    }\r\n  }\r\n}\r\n```\r\n\r\nwhich would order the `fruits` terms by the number of `yellow` documents for each `fruit`.\r\n\r\nWe would need to decide what we would do if `yellow` didn't appear in the top 10 terms for a particular fruit. Do we just say the sort value is `0`?\r\n\r\nBonus Points:\r\n\r\nWe could also support very basic operators on the multi-bucket aggregation's buckets so you could do something like:\r\n\r\n```json\r\n\"order\": {\r\n    \"avg(colours)\": \"asc\"\r\n}\r\n```\r\n\r\nwhich would sort by the average (mean) document count value across all the colours for each fruit. and:\r\n\r\n```json\r\n\"order\": {\r\n    \"avg(colours>total_sales)\": \"asc\"\r\n}\r\n```\r\nwhich would sort by the average (mean) value of `total_sales` across all the colours for each fruit."},"comment":{"url":"https://api.github.com/repos/elastic/elasticsearch/issues/comments/175055498","html_url":"https://github.com/elastic/elasticsearch/issues/12336#issuecomment-175055498","issue_url":"https://api.github.com/repos/elastic/elasticsearch/issues/12336","id":175055498,"user":{"login":"clintongormley","id":56599,"avatar_url":"https://avatars.githubusercontent.com/u/56599?v=3","gravatar_id":"","url":"https://api.github.com/users/clintongormley","html_url":"https://github.com/clintongormley","followers_url":"https://api.github.com/users/clintongormley/followers","following_url":"https://api.github.com/users/clintongormley/following{/other_user}","gists_url":"https://api.github.com/users/clintongormley/gists{/gist_id}","starred_url":"https://api.github.com/users/clintongormley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/clintongormley/subscriptions","organizations_url":"https://api.github.com/users/clintongormley/orgs","repos_url":"https://api.github.com/users/clintongormley/repos","events_url":"https://api.github.com/users/clintongormley/events{/privacy}","received_events_url":"https://api.github.com/users/clintongormley/received_events","type":"User","site_admin":false},"created_at":"2016-01-26T14:49:06Z","updated_at":"2016-01-26T14:49:06Z","body":"There does't seem to be much call for this syntax, so I'm going to close this issue for now."}}