Terms Lookup + sort

Hi,

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

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"
            }
         }
      }
}

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 to your query which does the sorting:

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 (you can also read more here) your relationships in the document may not be preserved like you think they are.

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