Sunday 22 January 2017

Elastic search - Index Routing

The purpose of routing is to inform elastic search to route the query to the shard which contains the data, to get the result of the query quickly. It boost up the performance of the system significantly


For instance when you run a query to get all the post which has a text "custom", by default the http request will be routed to all the nodes in the cluster to look for the data which is quite inefficient. This can be fixed with routing. 

Default query execution
Defining the routing in schema

{
"mappings": {
"post": {
"_routing": {
"required":true//query must use the routing parameter when searching
"path":"user_id" //The field which we wanna route with
}, 
"properties": {
"user_id": {
"type": "integer",
"store": true
},
"post_text": {
"type": "string"
},
"post_date": {
"type":"date",
"format":"YYYY-MM-DD"
}
}
}
}
}

The routing mapping will cause, elastic search to place all user data related to that user_id in one shard that helps to narrow down our search significantly. 

GET my_blog/post/_search?routing=2&q=post_text:custom

In the above query, the routing parameter specify to route the query to the shard which owns all the data of user_id=2. This is much more efficient.


No comments:

Post a Comment