{"action":"created","issue":{"url":"https://api.github.com/repos/elastic/elasticsearch/issues/8896","labels_url":"https://api.github.com/repos/elastic/elasticsearch/issues/8896/labels{/name}","comments_url":"https://api.github.com/repos/elastic/elasticsearch/issues/8896/comments","events_url":"https://api.github.com/repos/elastic/elasticsearch/issues/8896/events","html_url":"https://github.com/elastic/elasticsearch/issues/8896","id":51676285,"number":8896,"title":"Path hierarchy aggregation","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},"labels":[{"url":"https://api.github.com/repos/elastic/elasticsearch/labels/:Aggregations","name":":Aggregations","color":"009800"},{"url":"https://api.github.com/repos/elastic/elasticsearch/labels/discuss","name":"discuss","color":"fbca04"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2014-12-11T11:35:51Z","updated_at":"2015-12-08T07:08:57Z","closed_at":"2015-11-23T10:43:09Z","body":"A few users have used nested terms aggregations to try to visualise each level in a tree, such as a file system, eg:\r\n\r\n    {\r\n      \"aggs\": {\r\n        \"first_level\": {\r\n          \"terms\": {\r\n            \"field\": \"first_level\"\r\n          },\r\n          \"aggs\": {\r\n            \"second_level\": {\r\n              \"terms\": {\r\n                \"field\": \"second_level\"\r\n              },\r\n              \"aggs\": {\r\n                \"third_level\": {\r\n                  \"terms\": {\r\n                    \"field\": \"third_level\"\r\n                  }\r\n                }\r\n              }\r\n            }\r\n          }\r\n        }\r\n      }\r\n    }\r\n\r\nThis is very costly as it results in combinatorial explosion.  However, because this is a tree, it would be more efficient to store first_level+second_level+third_level in a single field, and to do a single pass over these \"leaf buckets\". Once we have the most popular leaves, we can backfill the branches (ie first_level+second_level  and first_level).\r\n\r\nThe results would obviously be different to the nested terms agg: instead of having the most popular first_levels, then the most popular second_levels in the most popular first_levels (etc), you'd have the most popular leaves, plus the first_level and second level that those leaves belong to.\r\n\r\nA complete example could look like this:\r\n\r\n    PUT /filesystem\r\n    {\r\n      \"mappings\": {\r\n        \"file\": {\r\n          \"properties\": {\r\n            \"path\": {\r\n              \"type\": \"string\",\r\n              \"index\": \"not_analyzed\",\r\n              \"doc_values\": true\r\n            }\r\n          }\r\n        }\r\n      }\r\n    }\r\n    \r\n    PUT /filesystem/file/1\r\n    {\r\n      \"path\": \"/My documents/Spreadsheets/Budget_2013.xls\",\r\n      \"views\": 10\r\n    }\r\n    \r\n    PUT /filesystem/file/2\r\n    {\r\n      \"path\": \"/My documents/Spreadsheets/Budget_2014.xls\",\r\n      \"views\": 6\r\n    }\r\n    \r\n    PUT /filesystem/file/3\r\n    {\r\n      \"path\": \"/My documents/Test.txt\",\r\n      \"views\": 1\r\n    }\r\n    \r\n    GET /filesystem/file/_search?search_type=count\r\n    {\r\n      \"aggs\": {\r\n        \"tree\": {\r\n          \"path_hierarchy\": {\r\n            \"field\": \"path\",\r\n            \"separator\": \"/\",\r\n            \"order\": \"total_views\"\r\n          },\r\n          \"aggs\": {\r\n            \"total_views\": {\r\n              \"sum\": {\r\n                \"field\": \"views\"\r\n              }\r\n            }\r\n          }\r\n        }\r\n      }\r\n    }\r\n\r\nAnd the result like this:\r\n\r\n    {\r\n      \"aggregations\": {\r\n        \"tree\": {\r\n          \"buckets\": [\r\n            {\r\n              \"key\": \"My documents\",\r\n              \"doc_count\": 3,\r\n              \"total_views\": {\r\n                \"value\": 18\r\n              },\r\n              \"tree\": {\r\n                \"buckets\": [\r\n                  {\r\n                    \"key\": \"Spreadsheets\",\r\n                    \"doc_count\": 2,\r\n                    \"total_views\": {\r\n                      \"value\": 17\r\n                    },\r\n                    \"tree\": {\r\n                      \"buckets\": [\r\n                        {\r\n                          \"key\": \"Budget_2013.xls\",\r\n                          \"doc_count\": 1,\r\n                          \"total_views\": {\r\n                            \"value\": 10\r\n                          }\r\n                        },\r\n                        {\r\n                          \"key\": \"Budget_2014.xls\",\r\n                          \"doc_count\": 1,\r\n                          \"total_views\": {\r\n                            \"value\": 7\r\n                          }\r\n                        }\r\n                      ]\r\n                    }\r\n                  },\r\n                  {\r\n                    \"key\": \"Test.txt\",\r\n                    \"doc_count\": 1,\r\n                    \"total_views\": {\r\n                      \"value\": 1\r\n                    }\r\n                  }\r\n                ]\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/162796821","html_url":"https://github.com/elastic/elasticsearch/issues/8896#issuecomment-162796821","issue_url":"https://api.github.com/repos/elastic/elasticsearch/issues/8896","id":162796821,"user":{"login":"deviantony","id":5485061,"avatar_url":"https://avatars.githubusercontent.com/u/5485061?v=3","gravatar_id":"","url":"https://api.github.com/users/deviantony","html_url":"https://github.com/deviantony","followers_url":"https://api.github.com/users/deviantony/followers","following_url":"https://api.github.com/users/deviantony/following{/other_user}","gists_url":"https://api.github.com/users/deviantony/gists{/gist_id}","starred_url":"https://api.github.com/users/deviantony/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/deviantony/subscriptions","organizations_url":"https://api.github.com/users/deviantony/orgs","repos_url":"https://api.github.com/users/deviantony/repos","events_url":"https://api.github.com/users/deviantony/events{/privacy}","received_events_url":"https://api.github.com/users/deviantony/received_events","type":"User","site_admin":false},"created_at":"2015-12-08T07:08:57Z","updated_at":"2015-12-08T07:08:57Z","body":"@clement-tourriere Does your plugin support ES 2.x ?"}}