Elasticsearch之路由(routing)
Posted alexephor
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elasticsearch之路由(routing)相关的知识,希望对你有一定的参考价值。
路由
主要解决的是文档应该存储在哪个分片中
实际过程是根据公式计算出进行分配
1 # 路由哈希值 % 主分片的数量 2 shard = hash(routing) % number_of_primary_shards 3 # 路由默认值是文档的_id
自定义路由
1 PUT r1/doc/1?routing=user1 2 3 "title":"论母猪的产前保养" 4 5 PUT r1/doc/2?routing=user1 6 7 "title":"论母猪的产后护理" 8
这两篇文档都分配在一个分片上了
通过路由查询文档
GET r1/doc/1?routing=user1 # 结果如下 "_index" : "r1", "_type" : "doc", "_id" : "1", "_version" : 3, "_routing" : "user1", "found" : true, "_source" : "title" : "论母猪的产前保养"
通过路由值查找
1 GET r1/doc/_search 2 3 "query": 4 "terms": 5 "_routing":["user1"] 6 7 8 9 # 结果如下 10 11 "took" : 0, 12 "timed_out" : false, 13 "_shards" : 14 "total" : 5, 15 "successful" : 5, 16 "skipped" : 0, 17 "failed" : 0 18 , 19 "hits" : 20 "total" : 2, 21 "max_score" : 1.0, 22 "hits" : [ 23 24 "_index" : "r1", 25 "_type" : "doc", 26 "_id" : "2", 27 "_score" : 1.0, 28 "_routing" : "user1", 29 "_source" : 30 "title" : "论母猪的产后护理" 31 32 , 33 34 "_index" : "r1", 35 "_type" : "doc", 36 "_id" : "1", 37 "_score" : 1.0, 38 "_routing" : "user1", 39 "_source" : 40 "title" : "论母猪的产前保养" 41 42 43 ] 44 45
删除文档 就要带上路由值不然会找不到
DELETE r1/doc/1 # 结果如下 "_index" : "r1", "_type" : "doc", "_id" : "1", "_version" : 1, "result" : "not_found", "_shards" : "total" : 2, "successful" : 1, "failed" : 0 , "_seq_no" : 2, "_primary_term" : 1 DELETE r1/doc/1?routing=user1 # 结果如下 "_index" : "r1", "_type" : "doc", "_id" : "1", "_version" : 2, "result" : "deleted", "_shards" : "total" : 2, "successful" : 1, "failed" : 0 , "_seq_no" : 4, "_primary_term" : 1
查询多个路由
PUT r2/doc/1?routing=user1 "title":"母猪产前保养重点在多喂饲料,辅以人工按摩" PUT r2/doc/2?routing=user2 "title":"母猪产后护理重点在母子隔离喂养"
查找
GET r2/doc/_search?routing=user1,user2 "query": "match": "title": "母猪" # 结果如下 "took" : 0, "timed_out" : false, "_shards" : "total" : 2, "successful" : 2, "skipped" : 0, "failed" : 0 , "hits" : "total" : 2, "max_score" : 0.68324494, "hits" : [ "_index" : "r2", "_type" : "doc", "_id" : "2", "_score" : 0.68324494, "_routing" : "user2", "_source" : "title" : "母猪产后护理重点在母子隔离喂养" , "_index" : "r2", "_type" : "doc", "_id" : "1", "_score" : 0.5753642, "_routing" : "user1", "_source" : "title" : "母猪产前保养重点在多喂饲料,辅以人工按摩" ]
处理忘记路由(导致文档在多个分片建立索引)
PUT r3/doc/1?routing=u1 "title":"小猪仔非常可爱" PUT r3/doc/2 "title":"再可爱也是一盘菜"
查询
GET r3/doc/_search "query": "terms": "_routing":["u1"] # 结果如下 "took" : 1, "timed_out" : false, "_shards" : "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 , "hits" : "total" : 1, "max_score" : 1.0, "hits" : [ "_index" : "r3", "_type" : "doc", "_id" : "1", "_score" : 1.0, "_routing" : "u1", "_source" : "title" : "小猪仔非常可爱" ]
文档2可以根据普通查询,这样两篇文档都要被返回,避免这样子的情况
# 以下是6.5.4版本的写法 PUT r4 "mappings": "doc": "_routing": "required": true # 以下是7.0后的官方文档的的写法 PUT my_index2 "mappings": "_ usting": "required":true
在进行对文档操作的时候就必须带上路由参数
PUT r4/doc/1?routing参数 "title":"母猪不怀孕怎么办?"
以上是关于Elasticsearch之路由(routing)的主要内容,如果未能解决你的问题,请参考以下文章