博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Elasticsearch学习---Index alias(索引别名)
阅读量:2492 次
发布时间:2019-05-11

本文共 6086 字,大约阅读时间需要 20 分钟。

前言

ES中的索引别名是一种简单且非常实用的功能,当你为一个索引设置别名后,就可以通过这个别名来操作相关的API,ES会自动将别名映射到实际的索引名中,而这样做的好处是,为你在之后的扩展方面带来的极大的灵活性。

具体有哪些应用

1、替换原索引:很显然,如果你一直使用的是索引别名来操作,那么如果你想更换另一个索引,只需要修改索引别名引用到新的索引即可,程序代码不需要任何改变。

2、创建不同功能的索引:可以通过建立索引检索的数据窗口,来实现特定的功能,例如,如果你为某个数据每天建立一个索引,当你想查询一周的数据时,你可以创建一个一周的索引别名,然后把每天的索引添加到这个别名中,只保留7天的数据,超过7天则删除最早的。

索引别名操作

建立一个索引别名

建立索引别名之前,先插入3条数据

PUT user_info/_doc/1{
"name":"张三", "age":18, "address":"中国南京市鼓楼区", "tel":"13901234567"}PUT user_info/_doc/2{
"name":"李四", "age":19, "address":"中国南京市建邺区", "tel":"13901234568"}PUT user_info/_doc/3{
"name":"王五", "age":20, "address":"中国北京市朝阳区", "tel":"13901234567"}

建立索引别名

PUT /user_info/_alias/user_info_alias_1

格式为:原索引名/_alias/别名

使用别名查询

GET /user_info_alias_1/_search
{
"took": 3, "timed_out": false, "_shards": {
"total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": {
"total": 3, "max_score": 1, "hits": [ {
"_index": "user_info", "_type": "_doc", "_id": "2", "_score": 1, "_source": {
"name": "李四", "age": 19, "address": "中国南京市建邺区", "tel": "13901234568" } }, {
"_index": "user_info", "_type": "_doc", "_id": "1", "_score": 1, "_source": {
"name": "张三", "age": 18, "address": "中国南京市鼓楼区", "tel": "13901234567" } }, {
"_index": "user_info", "_type": "_doc", "_id": "3", "_score": 1, "_source": {
"name": "王五", "age": 20, "address": "中国北京市朝阳区", "tel": "13901234567" } } ] }}

创建一个年龄小于等于19的索引

PUT /user_info/_alias/user_info_alias_age{
"filter": {
"range": {
"age": {
"lte": 19 } } }}

查询结果

GET /user_info_alias_age/_search
{
"took": 6, "timed_out": false, "_shards": {
"total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": {
"total": 2, "max_score": 1, "hits": [ {
"_index": "user_info", "_type": "_doc", "_id": "2", "_score": 1, "_source": {
"name": "李四", "age": 19, "address": "中国南京市建邺区", "tel": "13901234568" } }, {
"_index": "user_info", "_type": "_doc", "_id": "1", "_score": 1, "_source": {
"name": "张三", "age": 18, "address": "中国南京市鼓楼区", "tel": "13901234567" } } ] }}

删除索引别名

POST /_aliases{
"actions": [ {
"remove": {
"index": "user_info", "alias": "user_info_alias_age" } } ]}

也可以通过这个方式建立一个索引

POST /_aliases{
"actions": [ {
"add": {
"index": "user_info", "alias": "user_info_alias_age", "filter": {
"range": {
"age": {
"gte": 10, "lt": 20 } } } } } ]}

查询是否存在

HEAD /_alias/user_info_alias_age

更新索引

让索引别名指向一个新的索引:user_info_2

新的索引中插入3条数据,年龄分别加了10岁

PUT user_info_2/_doc/1{
"name":"张三", "age":28, "address":"中国南京市鼓楼区", "tel":"13901234567"}PUT user_info_2/_doc/2{
"name":"李四", "age":29, "address":"中国南京市建邺区", "tel":"13901234568"}PUT user_info_2/_doc/3{
"name":"王五", "age":30, "address":"中国北京市朝阳区", "tel":"13901234567"}

更新索引别名的指向

POST /_aliases{
"actions": [ {
"add": {
"index": "user_info_2", "alias": "user_info_alias_age" } }, {
"remove": {
"index": "user_info", "alias": "user_info_alias_age" } } ]}

再次查询

GET /user_info_alias_age/_search
{
"took": 3, "timed_out": false, "_shards": {
"total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": {
"total": 3, "max_score": 1, "hits": [ {
"_index": "user_info_2", "_type": "_doc", "_id": "2", "_score": 1, "_source": {
"name": "李四", "age": 29, "address": "中国南京市建邺区", "tel": "13901234568" } }, {
"_index": "user_info_2", "_type": "_doc", "_id": "1", "_score": 1, "_source": {
"name": "张三", "age": 28, "address": "中国南京市鼓楼区", "tel": "13901234567" } }, {
"_index": "user_info_2", "_type": "_doc", "_id": "3", "_score": 1, "_source": {
"name": "王五", "age": 30, "address": "中国北京市朝阳区", "tel": "13901234567" } } ] }}

write index

可以将别名指向的索引关联为写入索引。指定后,针对指向多个索引的别名的所有索引和更新请求将尝试解析为写入索引的一个索引。每个别名一次只能指定一个索引作为写入索引。如果未指定写入索引并且别名引用了多个索引,则将不允许写入。

可以使用别名 API 和索引创建 API 将与别名关联的索引指定为写入索引。

指定只能向user_info写入数据,user_info_2中并不会添加。

POST /_aliases{
"actions": [ {
"add": {
"index": "user_info", "alias": "user_info_alias_2", "is_write_index":true } }, {
"add": {
"index": "user_info_2", "alias": "user_info_alias_2" } } ]}
PUT /user_info_alias_2/_doc/5{
"name":"赵六", "age":31, "address":"中国南京市雨花区", "tel":"13901234569"}

相当于执行如下语句

PUT user_info/_doc/5{
"name":"赵六", "age":31, "address":"中国南京市雨花区", "tel":"13901234569"}

如果指定多个is_write_index为true

POST /_aliases{
"actions": [ {
"add": {
"index": "user_info", "alias": "user_info_alias_2", "is_write_index":true } }, {
"add": {
"index": "user_info_2", "alias": "user_info_alias_2", "is_write_index":true } } ]}

报错提示只能有一个is_write_index为true

{
"error": {
"root_cause": [ {
"type": "illegal_state_exception", "reason": "alias [user_info_alias_2] has more than one write index [user_info,user_info_2]" } ], "type": "illegal_state_exception", "reason": "alias [user_info_alias_2] has more than one write index [user_info,user_info_2]" }, "status": 500}

转载地址:http://yelrb.baihongyu.com/

你可能感兴趣的文章
iOS开发的一些奇巧淫技
查看>>
常浏览的博客和网站
查看>>
Xcode 工程文件打开不出来, cannot be opened because the project file cannot be parsed.
查看>>
iOS在Xcode6中怎么创建OC category文件
查看>>
5、JavaWeb学习之基础篇—标签(自定义&JSTL)
查看>>
8、JavaWEB学习之基础篇—文件上传&下载
查看>>
reRender属性的使用
查看>>
href="javascript:void(0)"
查看>>
h:panelGrid、h:panelGroup标签学习
查看>>
f:facet标签 的用法
查看>>
<h:panelgroup>相当于span元素
查看>>
java中append()的方法
查看>>
必学高级SQL语句
查看>>
经典SQL语句大全
查看>>
log日志记录是什么
查看>>
<rich:modelPanel>标签的使用
查看>>
<h:commandLink>和<h:inputLink>的区别
查看>>
<a4j:keeyAlive>的英文介绍
查看>>
关于list对象的转化问题
查看>>
VOPO对象介绍
查看>>