主要针对1.5版本,主要参考自官网资料,可以理解为一个翻译+实践+扩充的版本
Filtered Alias
带过滤器的别名提供了一个十分简单的方式来创建同一个index下的不同的“展示”。过滤器能够使用Query DSL来定义并且被应用到所有的搜索,计数,通过查询删除和其它类似的行为。
为了创建一个带过滤器的别名,首先需要确保所有的字段都存在于mapping中。
创建一个mapping
curl -XPUT 'http://localhost:9200/test2' -d '{
"mappings": {
"type1": {
"properties": {
"user" : {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}'
我们可以创建一个在user字段带过滤器的别名。
curl -XPOST 'http://localhost:9200/_aliases' -d '{
"actions" : [
{
"add" : {
"index" : "test2",
"alias" : "alias10",
"filter" : { "term" : { "user" : "kimchy" } }
}
}
]
}'
成功则返回
{"acknowledged":true}
我们向test2中间放入两个user,trump和kimchy,如下
~ % curl -XPOST 'localhost:9200/alias10/testtype' -d '{"user":"trump"}'
{"_index":"test2","_type":"testtype","_id":"AVhnwgnAu0MIai42Pwvm","_version":1,"created":true}%
~ % curl -XPOST 'localhost:9200/alias10/testtype' -d '{"user":"kimchy"}'
{"_index":"test2","_type":"testtype","_id":"AVhnwkRiu0MIai42Pwvn","_version":1,"created":true}%
可以看到,我们通过别名进行文档输入,虽然alias10为带过滤器,限制了user为kimchy,但是我们通过alias10的别名进行user非kimchy文件的输入还是成功。进行搜索,得到结果如下:
~ % curl 'localhost:9200/test2/_search' -d '{"query":{"match_all":{}}}'
{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":1.0,"hits":[{"_index":"test2","_type":"testtype","_id":"AVhnwkRiu0MIai42Pwvn","_score":1.0,"_source":{"user":"kimchy"}},{"_index":"test2","_type":"testtype","_id":"AVhnwgnAu0MIai42Pwvm","_score":1.0,"_source":{"user":"trump"}}]}}%
~ % curl 'localhost:9200/alias10/_search' -d '{"query":{"match_all":{}}}'
{"took":17,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"test2","_type":"testtype","_id":"AVhnwkRiu0MIai42Pwvn","_score":1.0,"_source":{"user":"kimchy"}}]}}%
可以看到,通过test2这个index直接进行搜索可以看到之前放入的两个文档,但是通过alias10这个别名就只能看到符合过滤器过滤后的结果了,只有一个user为kimchy的结果。
Routing路由
能够为别名绑上路由。这个特性可以和过滤器的特性一起使用,来避免不必要地分片操作。
如下的命令创建了一个新的别名alias2指向了index test。在alias2被创建之后,所有和这个别名相关的操作将会被自动修正为使用值为1的路由。
curl -XPOST 'http://localhost:9200/_aliases' -d '
{
"actions" : [
{
"add" : {
"index" : "test",
"alias" : "alias2",
"routing" : "1"
}
}
]
}'
也可以分开指定搜索和索引的路由
curl -XPOST 'http://localhost:9200/_aliases' -d '
{
"actions" : [
{
"add" : {
"index" : "test",
"alias" : "alias2",
"search_routing" : "1,2",
"index_routing" : "2"
}
}
]
}'
进行测试,同时使用路由和过滤器
curl -XPOST 'http://localhost:9200/_aliases' -d '
{
"actions" : [
{
"add" : {
"index" : "test",
"alias" : "alias2",
"search_routing" : "2,3",
"index_routing" : "1",
"filter": {"term": {"user": "kitty"}}
}
}
]
}'
注意啦,index_routing不能指向多个值,不然会报错
{"error":"ElasticsearchIllegalArgumentException[alias [alias2] has several index routing values associated with it]","status":400}%
再加一个alias3
curl -XPOST 'http://localhost:9200/_aliases' -d '
{
"actions" : [
{
"add" : {
"index" : "test",
"alias" : "alias3",
"search_routing" : "1",
"index_routing" : "2",
"filter": {"term": {"user": "hello"}}
}
}
]
}'
测试如下:
~ % curl -XPOST 'localhost:9200/alias2/testtest' -d '{"user": "trump"}'
{"_index":"test","_type":"testtest","_id":"AVhqrXnwYhUIwOtlirMh","_version":1,"created":true}%
~ % curl -XPOST 'localhost:9200/alias2/testtest' -d '{"user": "hello"}'
{"_index":"test","_type":"testtest","_id":"AVhqrcWRYhUIwOtlirMi","_version":1,"created":true}%
~ % curl -XPOST 'localhost:9200/alias2/testtest' -d '{"user": "world"}'
{"_index":"test","_type":"testtest","_id":"AVhqrdp7YhUIwOtlirMj","_version":1,"created":true}%
~ % curl -XPOST 'localhost:9200/alias3/testwhat' -d '{"user": "kitty"}'
{"_index":"test","_type":"testwhat","_id":"AVhqrhRyYhUIwOtlirMk","_version":1,"created":true}%
~ % curl -XPOST 'localhost:9200/alias3/testwhat' -d '{"user": "omg"}'
{"_index":"test","_type":"testwhat","_id":"AVhqriccYhUIwOtlirMl","_version":1,"created":true}% ~ % curl -XPOST 'localhost:9200/alias3/testwhat' -d '{"user": "haha"}'
{"_index":"test","_type":"testwhat","_id":"AVhqrjmCYhUIwOtlirMm","_version":1,"created":true}%
~ % curl -XPOST 'localhost:9200/alias2/_search'
{"took":29,"timed_out":false,"_shards":{"total":2,"successful":2,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"test","_type":"testwhat","_id":"AVhqrhRyYhUIwOtlirMk","_score":1.0,"_source":{"user": "kitty"}}]}}%
~ % curl -XPOST 'localhost:9200/alias3/_search'
{"took":3,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"test","_type":"testtest","_id":"AVhqrcWRYhUIwOtlirMi","_score":1.0,"_source":{"user": "hello"}}]}}%
~ % curl -XDELETE 'localhost:9200/alias3/testwhat/AVhqrhRyYhUIwOtlirMk'
{"found":true,"_index":"test","_type":"testwhat","_id":"AVhqrhRyYhUIwOtlirMk","_version":2}%
~ % curl -XPOST 'localhost:9200/alias2/testwhat' -d '{"user": "kitty"}'
{"_index":"test","_type":"testwhat","_id":"AVhqtv_HYhUIwOtlirMn","_version":1,"created":true}%
~ % curl -XPOST 'localhost:9200/alias2/_search'
{"took":1,"timed_out":false,"_shards":{"total":2,"successful":2,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}%
这个测试例子比较有意思,alias2 index的路由为1,搜索的路由为2,3,alias3 index的路由为2,搜索的路由为1,所以使用alias2增加的文档能够被使用alias3搜索到,而使用alias2搜索不到,使用alias3增加的文档能够被alias2搜索到,而使用alias3搜索不到。再加上过滤器,所以alis2搜索match_all只能够得到kitty,alias3搜索match_all只能够得到hello,如果我们通过alias3删除了kitty,通过alias2增加了kitty,可以看到,alias2也是搜索不到的。