Show certain documents first, then sort remaining documents

Is it possible to exclude some documents from sorting operation, show specific documents first then sort?

What I want to achieve is supplying set of ids to ES and showing matching documents first and then sort other remaining documents by some other field.

Example:

Document A - id: 1, views: 100

Document B - id: 2, views: 120

Document C - id: 3, views: 130

Document D - id: 4, views: 140

With current sorting option (views desc) the result I get is: D, C, B, A

Expected behaviour: Give ids (3, 5, 2), Result should be: C, B, D, A

Hi Khonsu,

You could use a script to score documents (https://www.elastic.co/guide/en/elasticsearch/reference/6.4/search-request-sort.html#_script_based_sorting) to customize the way that you return them, but that will likely be very expensive, is there a reason you can't do this on the client side since that would be much less query load on Elasticsearch?

Thank you for your response.
There are pre-calculated set of ids related to user.
Based on users' inputs, documents are filtered, then sorted by a numeric field.

First problem is whether those pre-calculated ids would be included in result documents.
Second problem is result documents are fetched with pagination.

To prepare which documents to show, client side should fetch all the result documents (to prevent duplicate results, user related documents should be shown on first page),
check if set of user related ids are contained in results documents, then customize order of documents.

I was wondering if Elasticsearch would be any help to those calculations.

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