When I am searching in elastic search by a routing key, it is also returning result of another routing key.
Path:Index_2022/_search?routing=10
{
"from": 0,
"size": 5000,
"timeout": "15m",
"query":
{
"bool":
{
"must":
[
{
"query_string":
{
"query": "*",
"fields":
[
"content^1.0"
]
}
}
]
}
}
}
I am searching result with routing key 10, but elastic gives result of routing key 3 also.
CodePudding user response:
The goal of the routing key is only to define in which shard the document is going to be stored. The value of the routing key is hashed and the result is a shard index.
If you have 1M documents and 4 shards then each of your shard is going to contain approximately ~250K documents. All those documents might have different routing keys, but their hash always resolves to the number of primary shards (i.e. 0, 1, 2 and 3).
So in your case, the documents having routing keys 10 and 3 have a hash that both resolve to the same primary shard index, which is why you're finding them in the same shard.
The idea behind searching with a routing key is to search inside a fewer amount of shards. Say you have 4 shards and you know that what you're searching for has a routing key of 10 (e.g. it could be a tenant id, a customer id, etc), so you specify routing=10
and then the search will only happen inside a single shard instead of searching through all 4 shards, which usually yields faster query response time.