世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。
问 :_routing有什么特点?
答 :
问 :_routing如何使用?
答 :
# _routing
PUT /routing_test
{
"mappings" : {
"_routing" : {
"required" : true
},
"properties" : {
"name" : {"type" : "text"}
}
}
}
# 索引
POST /routing_test/_doc/1?routing=hello
{
"name" : "hello good"
}
# 索引,当_routing设置为required=true时,没有routing则报错
POST /routing_test/_doc/2
{
"name" : "tea"
}
# 结果
{
"error" : {
"root_cause" : [
{
"type" : "routing_missing_exception",
"reason" : "routing is required for [routing_test]/[_doc]/[2]",
"index_uuid" : "_na_",
"index" : "routing_test"
}
],
"type" : "routing_missing_exception",
"reason" : "routing is required for [routing_test]/[_doc]/[2]",
"index_uuid" : "_na_",
"index" : "routing_test"
},
"status" : 400
}
# 搜索
GET /routing_test/_search
{
"query" : {
"terms" : {
"_routing" : ["hello"]
}
}
}
# 结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "routing_test",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_routing" : "hello",
"_source" : {
"name" : "hello good"
}
}
]
}
}