Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch Sorted Index not working as expected with multiple shards

I have an elastic index with default sort mapping of price:

shop_prices_sort_index

    "sort" : {
      "field" : "enrich.price",
      "order" : "desc"
    },

If I insert 10 documents:

100, 98, 10230, 34, 1, 23, 777, 2323, 3, 109

And Fetch results using /_search. By default it returns documents in order of price descending.

10230, 2323...

But if I distribute my documents into 3 shards, Then the same query returns some other sequence of products:

100, 98, 34...

I am really stuck here, I am not sure if I am missing out something basic or Do I need any extra settings to make a Sorted Index behave correctly.

PS: I also tried 'routing' & 'preference'. but no luck. Any help much appreciated.

like image 203
informer Avatar asked Jun 09 '21 15:06

informer


1 Answers

When configuring index sorting, you're only making sure that each segment inside each shard is properly sorted. The goal of index sorting is to provide some more optimization during searches

Due to the distributed nature of ES, when your index has many shards, each shard will be properly sorted, but your search query will still need to use sorting explicitly.

So if your index settings contains the following to apply sorting at indexing time

"sort" : {
  "field" : "enrich.price",
  "order" : "desc"
}

your search queries will also need to contain the same sort specification at query time

"sort" : {
  "field" : "enrich.price",
  "order" : "desc"
}

By using index sorting you'll hit a little overhead at indexing time, but your search queries will be a bit faster in the end.

like image 107
Val Avatar answered Oct 22 '22 00:10

Val