{"action":"created","issue":{"url":"https://api.github.com/repos/elastic/elasticsearch/issues/17067","repository_url":"https://api.github.com/repos/elastic/elasticsearch","labels_url":"https://api.github.com/repos/elastic/elasticsearch/issues/17067/labels{/name}","comments_url":"https://api.github.com/repos/elastic/elasticsearch/issues/17067/comments","events_url":"https://api.github.com/repos/elastic/elasticsearch/issues/17067/events","html_url":"https://github.com/elastic/elasticsearch/issues/17067","id":140144476,"number":17067,"title":"Getting the nested objects in sorted order.","user":{"login":"himaniJoshi","id":15976885,"avatar_url":"https://avatars.githubusercontent.com/u/15976885?v=3","gravatar_id":"","url":"https://api.github.com/users/himaniJoshi","html_url":"https://github.com/himaniJoshi","followers_url":"https://api.github.com/users/himaniJoshi/followers","following_url":"https://api.github.com/users/himaniJoshi/following{/other_user}","gists_url":"https://api.github.com/users/himaniJoshi/gists{/gist_id}","starred_url":"https://api.github.com/users/himaniJoshi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/himaniJoshi/subscriptions","organizations_url":"https://api.github.com/users/himaniJoshi/orgs","repos_url":"https://api.github.com/users/himaniJoshi/repos","events_url":"https://api.github.com/users/himaniJoshi/events{/privacy}","received_events_url":"https://api.github.com/users/himaniJoshi/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-03-11T10:07:10Z","updated_at":"2016-03-11T11:39:48Z","closed_at":"2016-03-11T11:39:48Z","body":"Hello,\r\nI first want to declare that I have been introduced to  the world of elasticsearch just a week back, so there could be some obvious points I might be missing while addressing my problems.\r\n\r\nHowever, there is one particular problem, I just can't seem to find a solution to, after exhaustively trying to find and answer, so I would like some help here.\r\n\r\nI have created a dummy index to explain my problem.\r\n\r\nThe mapping is : \r\n\r\n  \r\n    {\"mappings\": {\r\n    \"person\" : {\r\n      \"_source\": {\r\n        \"enabled\": false\r\n      }, \r\n      \"properties\": {\r\n        \"name\" :  {\r\n          \"type\" : \"string\"\r\n        },\r\n        \"age\" :{\r\n          \"type\"  :\"long\"\r\n        },\r\n        \"city\" : {\r\n          \"type\" : \"string\"\r\n        },\r\n        \"attributes\" : {\r\n          \"type\": \"nested\",\r\n          \"include_in_all\": true,\r\n          \"properties\": {\r\n            \"attributeName\": {\r\n              \"type\" : \"string\"\r\n            },\r\n            \"id\"  :{\r\n              \"type\" : \"string\",\r\n              \"store\" :  true\r\n            },\r\n            \"rating\" : {\r\n              \"type\" : \"long\"\r\n            }\r\n          }\r\n        }\r\n      }\r\n    }}}\r\n\r\n'attributes' is nested, and we are storing the 'attributes.id'\r\n\r\nI populated this with following entries:\r\n  \r\n  \r\n    {\"name\" : \"abc1\",\r\n    \"age\" : 21,\r\n    \"city\" : \"Delhi\",\r\n    \"attributes\" : [\r\n      {\r\n        \"id\" : 1,\r\n        \"rating\"  : 5,\r\n        \"attributeName\"  : \"at1\"\r\n      },\r\n      {\"id\" : 2,\r\n        \"rating\" : 7,\r\n        \"attributeName\"  : \"at2\"\r\n      }\r\n    ]}\r\n\r\n\r\n     {\"name\" : \"abc2\",\r\n     \"age\" : 24,\r\n     \"city\" : \"Mumbai\",\r\n     \"attributes\" : [\r\n      {\r\n        \"id\" : 4,\r\n        \"rating\"  : 4,\r\n        \"attributeName\"  : \"at4\"\r\n      },\r\n      {\"id\" : 3,\r\n        \"rating\" : 8,\r\n        \"attributeName\"  : \"at3\"\r\n      }\r\n    ]}\r\n\r\n\r\n\r\n     {\"name\" : \"abc3\",\r\n     \"age\" : 26,\r\n     \"city\" : \"Delhi\",\r\n     \"attributes\" : [\r\n      {\r\n        \"id\" : 5,\r\n        \"rating\"  : 9,\r\n        \"attributeName\"  : \"at5\"\r\n      },\r\n      {\"id\" : 6,\r\n        \"rating\" : 6,\r\n        \"attributeName\"  : \"at6\"\r\n      }\r\n    ]}\r\n\r\n\r\n\r\nNow my problem required me to make a query that applies some filter on city, and some filter on attributes.rating, and gives me back just the attribute ids. There doesn't seem to be an exact solution for this, but I have been able to get it working by using inner_hits and adding id to the inner_hits' fields. Like this:\r\n\r\n \r\n    {\"query\" :{\r\n              \"bool\": {\r\n                \"must\": [\r\n                   {\r\n                    \"match\" : {\r\n                      \"city\" : \"Delhi\"\r\n                    }\r\n                  },\r\n                  {\r\n                    \"nested\": {\r\n                      \"path\": \"attributes\",\r\n                      \"query\": {\r\n                        \"bool\": {\r\n                          \"must\": [\r\n                           {\r\n                              \"range\": {\r\n                                \"attributes.rating\": {\r\n                                  \"gt\": 5\r\n                                }\r\n                              }\r\n                            }\r\n                          ]\r\n                        }\r\n                      },\r\n                      \"inner_hits\" : {\r\n                          \"fields\"  : [\"attributes.id\"]\r\n                      }\r\n                    }\r\n                  }\r\n                ]\r\n              }\r\n            }}\r\n\r\n\r\nThis gives me the following response:\r\n  \r\n    {\"took\": 9,\r\n    \"timed_out\": false,\r\n    \"_shards\": {\r\n    \"total\": 5,\r\n    \"successful\": 5,\r\n    \"failed\": 0\r\n    },\r\n    \"hits\": {\r\n    \"total\": 2,\r\n    \"max_score\": 1.724915,\r\n    \"hits\": [\r\n      {\r\n        \"_index\": \"test\",\r\n        \"_type\": \"person\",\r\n        \"_id\": \"1\",\r\n        \"_score\": 1.724915,\r\n        \"inner_hits\": {\r\n          \"attributes\": {\r\n            \"hits\": {\r\n              \"total\": 1,\r\n              \"max_score\": 1,\r\n              \"hits\": [\r\n                {\r\n                  \"_index\": \"test\",\r\n                  \"_type\": \"person\",\r\n                  \"_id\": \"1\",\r\n                  \"_nested\": {\r\n                    \"field\": \"attributes\",\r\n                    \"offset\": 1\r\n                  },\r\n                  \"_score\": 1,\r\n                  \"fields\": {\r\n                    \"attributes.id\": [\r\n                      \"2\"\r\n                    ]\r\n                  }\r\n                }\r\n              ]\r\n            }\r\n          }\r\n        }\r\n      },\r\n      {\r\n        \"_index\": \"test\",\r\n        \"_type\": \"person\",\r\n        \"_id\": \"3\",\r\n        \"_score\": 1.724915,\r\n        \"inner_hits\": {\r\n          \"attributes\": {\r\n            \"hits\": {\r\n              \"total\": 2,\r\n              \"max_score\": 1,\r\n              \"hits\": [\r\n                {\r\n                  \"_index\": \"test\",\r\n                  \"_type\": \"person\",\r\n                  \"_id\": \"3\",\r\n                  \"_nested\": {\r\n                    \"field\": \"attributes\",\r\n                    \"offset\": 1\r\n                  },\r\n                  \"_score\": 1,\r\n                  \"fields\": {\r\n                    \"attributes.id\": [\r\n                      \"6\"\r\n                    ]\r\n                  }\r\n                },\r\n                {\r\n                  \"_index\": \"test\",\r\n                  \"_type\": \"person\",\r\n                  \"_id\": \"3\",\r\n                  \"_nested\": {\r\n                    \"field\": \"attributes\",\r\n                    \"offset\": 0\r\n                  },\r\n                  \"_score\": 1,\r\n                  \"fields\": {\r\n                    \"attributes.id\": [\r\n                      \"5\"\r\n                    ]\r\n                  }\r\n                }\r\n              ]\r\n            }\r\n          }\r\n        }\r\n      }\r\n    ]}}\r\n\r\n\r\nThe second part of my problem requires me to return the attribute id in sorted order.\r\nSo from above query how can I get attribute.id, sorted by rating in ascending order. This means that i should get Ids in sequence 6, 2, 5. However, since in my response I get the parent, not the nested doc, and since 5 and 6 are in one parent, and 2 in another, does my problem even have a solution?"},"comment":{"url":"https://api.github.com/repos/elastic/elasticsearch/issues/comments/195333445","html_url":"https://github.com/elastic/elasticsearch/issues/17067#issuecomment-195333445","issue_url":"https://api.github.com/repos/elastic/elasticsearch/issues/17067","id":195333445,"user":{"login":"jasontedor","id":4744941,"avatar_url":"https://avatars.githubusercontent.com/u/4744941?v=3","gravatar_id":"","url":"https://api.github.com/users/jasontedor","html_url":"https://github.com/jasontedor","followers_url":"https://api.github.com/users/jasontedor/followers","following_url":"https://api.github.com/users/jasontedor/following{/other_user}","gists_url":"https://api.github.com/users/jasontedor/gists{/gist_id}","starred_url":"https://api.github.com/users/jasontedor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jasontedor/subscriptions","organizations_url":"https://api.github.com/users/jasontedor/orgs","repos_url":"https://api.github.com/users/jasontedor/repos","events_url":"https://api.github.com/users/jasontedor/events{/privacy}","received_events_url":"https://api.github.com/users/jasontedor/received_events","type":"User","site_admin":false},"created_at":"2016-03-11T11:39:48Z","updated_at":"2016-03-11T11:39:48Z","body":"GitHub is reserved for feature requests and bug reports; general questions are best addressed on the [Elastic Discourse forums](https://discuss.elastic.co)."}}