Skip to content

Instantly share code, notes, and snippets.

@RishikeshDarandale
Last active November 16, 2018 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RishikeshDarandale/445aeae51f82fee75b5a373df0be97d3 to your computer and use it in GitHub Desktop.
Save RishikeshDarandale/445aeae51f82fee75b5a373df0be97d3 to your computer and use it in GitHub Desktop.
elasticsearch order by IDs provided in IDs query

elasticsearch 6.3

When you want to order the elaticsearch document in the order of Ids you have provided in Ids query, then following function score query can be used to get required result:

{  
   "query":{  
      "function_score":{  
         "query":{  
            "ids":{  
               "type":"_doc",
               "values":[  
                  "4",
                  "3",
                  "1",
                  "5",
                  "2",
                  "6"
               ]
            }
         },
         "script_score":{  
            "script":{  
               "params":{  
                  "ids":[  
                     "4",
                     "3",
                     "1",
                     "5",
                     "2",
                     "6"
                  ]
               },
               "source":"params.ids.size() - params.ids.indexOf(doc['_id'].getValue())"
            }
         }
      }
   }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment