Hello,
I'm extremely new to the world of elasticsearch, but even after reading a bit of the material, I'm still quite unsure how elasticsearch actually works under the hood.
I understand that all of it essentially revolves around inverted indices, it must also probably involve some internal statistics.
If I have a query with two filters, the engine will have two inverted indices to choose from, it will likely pick the one with a lower number of matching docs, and it will simply "filter out" in software the other one.
I also understand that, even though "compound" inverted indices don't exist, we can kind of "make them up" using the "copy_to" feature, to create a compound field, and then filter out on that field.
Now what I really don't understand however, is the sorting.
From what I understand, the main way data is sorted is in memory, after the entries have actually been fetched.
This is fine if we're querying a fairly small set of data,
e.g. "get all apples ordered by price ascending" is good!
"get all products ordered by price ascending" is worse.
To solve this, there's the "sorted index" feature, which will (provided we don't care about number of matches) essentially only read as many entries as it returns (given a single shard). That is, it won't read all potential 10M entries to return a page of 10.
This also synergizes well with the "search_after" feature, given the index is sorted, it's easier for elasticsearch to actually lookup the start of the data it needs to return.
But what happens if I want to query sorting under a different key?
Should I create a second sorted index, and insert all my entries twice? Is this a common use-case?