Elasticsearch模块功能之-路由(routing)

Elasticsearch模块功能之-路由(routing)

索引分片分配能够控制索引分片在节点上怎么分布,那对于具体的文档能否控制具体节点的分布呢?答案是可以,根据路由公式shard = hash(routing) % number_of_primary_shardsElasticsearch使用相同的routing参数来实现这个功能,但我们在创建索引时需如下进行配置:

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. "mappings":{  
  2.     "doc": {  
  3.         "_routing": {  
  4.             "required": true,  
  5.             "path":"_routing"  
  6.         },  
  7.         "properties": {  
  8.             "title": {  
  9.                 "type":"string"  
  10.             }  
  11.         }  
  12.     }  
  13. }  

 

如果我们想在建索引时将相关的文档存放到一个分片下就可以这样做:

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. curl- XPUT localhost: 9200 / documents / doc / 1 - d '  
  2. {  
  3.     "title": "Document No.1",  
  4.     "_routing":"A"  
  5. }'  
  6. curl- XPUT localhost: 9200 / documents / doc / 2 - d '  
  7. {  
  8.     "title": "Document No.1",  
  9.     "_routing":"A"  
  10. }'  
  11. curl- XPUT localhost: 9200 / documents / doc / 3 - d '  
  12. {  
  13.     "title": "Document No.1",  
  14.     "_routing":"B"  
  15. }'  

 

这样将id为1和2的文档将会存在统一个分片上去。

         既然索引文档的时候使用了路由,那么肯定得在查询的时候利用了,在查询的时候使用routing参数将使得查询更加高效,如下:

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. curl-XGET 'localhost:9200/documents/_search?pretty&q=*:*&routing=A'  

from  http://blog.csdn.net/changong28/article/details/38427311

猜你喜欢

转载自aoyouzi.iteye.com/blog/2144725