# Terms Lookup + sort

**URL:** <https://discuss.elastic.co/t/terms-lookup-sort/83084>\
**Category:** Elasticsearch\
**Created:** [April 20, 2017, 3:41pm UTC](https://discuss.elastic.co/t/terms-lookup-sort/83084 "2017-04-20T15:41:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sverdier](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@sverdier](https://discuss.elastic.co/u/sverdier)\
**Post date:** [April 20, 2017, 3:41pm UTC](https://discuss.elastic.co/t/terms-lookup-sort/83084/1 "2017-04-20T15:41:14Z")

</div>

Hi,

I want to sort users by created\_at of likers it's possible with terms Lookup ?

```auto
PUT /newsfeed/feed/1
{
 "likers" : [
   {
     "id" : "1",
     "created_at": "2017-04-16T19:02:26Z"
   },
   {
     "id" : "2",
     "created_at": "2017-04-16T19:02:26Z"
   }
 ]
}

GET /users/user/_search
{
      "query":{
         "terms":{
            "id":{
               "index":"newsfeed",
               "type":"feed",
               "id":"1",
               "path":"likers.id"
            }
         }
      }
}

```

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [April 28, 2017, 1:18pm UTC](https://discuss.elastic.co/t/terms-lookup-sort/83084/2 "2017-04-28T13:18:14Z")

</div>

Not through the terms lookup functionality. You can think of that as merely grabbing the list of terms to filter from a document, rather than from the query body supplied as part of the request.

But you can [add a sort element](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html) to your query which does the sorting:

```auto
GET /users/user/_search
{
      "query":{
         "terms":{
            "id":{
               "index":"newsfeed",
               "type":"feed",
               "id":"1",
               "path":"likers.id"
            }
         }
      },
      "sort" : [
          { "likers.id" : {"order" : "asc"}},
          "_score"
      ],
}

```

Note that unless that first document is using [Nested Documents](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html) (you can also [read more here](https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-objects.html)) your relationships in the document may not be preserved like you think they are.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 26, 2017, 1:28pm UTC](https://discuss.elastic.co/t/terms-lookup-sort/83084/3 "2017-05-26T13:28:16Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
