Script based sorting is applied only after pagination #3309
When a query requests both sorting and pagination, the expected behavior is that sorting is executed before pagination. And indeed this is the actual behavior when the query requests sorting on a field level. However, when doing script based sorting, it seems that sorting is only executed after pagination. This is behavior is unexpected and especially surprising because the same sort may yield different results depending on whether it is specified on a field level or via a script.
Suppose we have 100 files indexed, named "file001", "file002", ..., "file100", with the file name being stored in a "filename" field. Then the following query
yields the expected ordering "file001", "file002", ..., "file010". The seemingly equivalent query
may return, for example, "file005", "file007", "file020", "file027", "file035", "file050", "file067", "file080", "file092", "file097". Which is pretty useless and certainly not what the client would expect.
This behavior was observed in ElasticSearch 0.90.2, but I didn't test previous versions.