【Elasticsearch专栏 08】深入探索:Elasticsearch中的Routing机制详解
    		       		warning:
    		            这篇文章距离上次修改已过442天,其中的内容可能已经有所变动。
    		        
        		                
                
PUT /my_index
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  }
}
 
# 使用自定义路由
POST /my_index/_doc?routing=user_id_1
{
  "title": "Document 1",
  "user_id": "user_id_1"
}
 
POST /my_index/_doc?routing=user_id_2
{
  "title": "Document 2",
  "user_id": "user_id_2"
}
 
# 使用文档中的字段作为路由
POST /my_index/_doc
{
  "title": "Document 3",
  "user_id": "user_id_3"
}
 
# 查询时指定路由
GET /my_index/_search?routing=user_id_1
{
  "query": {
    "term": {
      "user_id": "user_id_1"
    }
  }
}这个代码示例展示了如何在Elasticsearch中使用自定义路由和文档字段来控制文档的分片路由。通过指定路由参数,可以将对特定文档的操作定向到相同的分片,这在需要保持数据局部性的场景中非常有用。
评论已关闭