index alias

index alias

建立index

PUT twitter/_doc/1
{
  "user" : "双榆树-张三",
  "message" : "今儿天气不错啊,出去转转去",
  "uid" : 2,
  "age" : 20,
  "city" : "北京",
  "province" : "北京",
  "country" : "中国",
  "address" : "中国北京市海淀区",
  "location" : {
    "lat" : "39.970718",
    "lon" : "116.325747"
  }
}
 
PUT twitter/_doc/2
{
  "user" : "东城区-老刘",
  "message" : "出发,下一站云南!",
  "uid" : 3,
  "age" : 30,
  "city" : "北京",
  "province" : "北京",
  "country" : "中国",
  "address" : "中国北京市东城区台基厂三条3号",
  "location" : {
    "lat" : "39.904313",
    "lon" : "116.412754"
  }
}
 
PUT twitter/_doc/3
{
  "user" : "虹桥-老吴",
  "message" : "好友来了都今天我生日,好友来了,什么 birthday happy 就成!",
  "uid" : 7,
  "age" : 90,
  "city" : "上海",
  "province" : "上海",
  "country" : "中国",
  "address" : "中国上海市闵行区",
  "location" : {
    "lat" : "31.175927",
    "lon" : "121.383328"
  }
}

添加一个index alias

一个 index 别名就是一个用来引用一个或多个已经存在的索引的另外一个名字,可以用如下的方法来创建

PUT /twitter/_alias/alias1

请求格式

PUT /<index>/_alias/<alias>
POST /<index>/_alias/<alias>
PUT /<index>/_aliases/<alias>
POST /<index>/_aliases/<alias>

路径参数:

<index>: 要添加到别名的索引名称的逗号分隔列表或通配符表达式。
要将群集中的所有索引添加到别名,请使用_all值。

<alias>: (必需,字符串)要创建或更新的索引别名的名称。

创建索引别名后,通过索引来访问index

GET alias1/_search

显然这样做的好处是非常明显的,可以把想要的进行搜索的 index 取一个和我们搜索方法里一样的别名就可以了,这样可以不修改我们的搜索方法,就可以分别对不同的 index 进行搜索。

  • 创建一个基于城市的alias
PUT twitter/_alias/city_beijing
{
  "filter": {
    "term": {
      "city": "北京"
    }
  }
}
  • filter还可以是多个查询
PUT twitter/_alias/city_beijing
{
  "filter": [
    {
      "term": {
        "city": "北京"
      }
    },
    {
      "range": {
        "age": {
          "lte": 25
        }
      }
    }
  ]
}
  • alias 也可以在创建 index 时被创建
DELETE twitter
 
PUT twitter
{
     "mappings" : {
      "properties" : {
        "address" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "age" : {
          "type" : "long"
        },
        "city" : {
          "type" : "keyword",
          "copy_to" : [
            "region"
          ]
        },
        "country" : {
          "type" : "keyword",
          "copy_to" : [
            "region"
          ]
        },
        "explain" : {
          "type" : "boolean"
        },
        "location" : {
          "type" : "geo_point"
        },
        "message" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "province" : {
          "type" : "keyword",
          "copy_to" : [
            "region"
          ]
        },
        "region" : {
          "type" : "text"
        },
        "uid" : {
          "type" : "long"
        },
        "user" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    },
    "aliases": {
      "city_beijing": {
        "filter": {
          "term": {
            "city": "北京"
          }
        }
      }
    }
}

获取alias

GET /_alias
GET /_alias/<alias>
GET /<index>/_alias/<alias>

通过wild card方式获取alias

GET /twitter/_alias/*
GET /_alias/city_*

检查alias是否存在

HEAD /_alias/<alias>
HEAD /<index>/_alias/<alias>

更新alias

POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "twitter", "alias" : "alias2" } }
    ]
}

在 action 里,有如下的几种操作:

  • add: 添加一个别名
  • remove: 删除一个别名
  • remove_index: 删除一个index或它的别名

删除一个alias

POST /_aliases
{
    "actions" : [
        { "remove": { "index" : "twitter", "alias" : "alias2" } }
    ]
}

重命名一个alias

重命名别名是一个简单的删除然后在同一 API 中添加操作。 此操作是原子操作。

POST /_aliases
{
    "actions" : [
        { "remove" : { "index" : "twitter", "alias" : "alias1" } },
        { "add" : { "index" : "twitter", "alias" : "alias2" } }
    ]
}

可以把同一个 alias 在指向不同时期的 index,比如 log index 滚动下一个月,可以修改 alias 总是指向最新的index。

POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "blogs_v2",
        "alias": "blogs"
      }
    },
    {
      "remove": {
        "index": "blogs_v1",
        "alias": "blogs"
      }
    }
  ]
}

为多个索引添加同样一个alias

POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "test1", "alias" : "alias1" } },
        { "add" : { "index" : "test2", "alias" : "alias1" } }
    ]
}

或者

POST /_aliases
{
    "actions" : [
        { "add" : { "indices" : ["test1", "test2"], "alias" : "alias1" } }
    ]
}

或者

POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "test*", "alias" : "all_test_indices" } }
    ]
}

注意: 当 index 文档时,对一个指向多个 index 的别名进行索引是错误的。

案例

在一个操作中使用别名交换索引

PUT test     
PUT test_2
 
PUT test_2/_doc/1
{
  "text": "nice"
}
 
POST /_aliases
{
    "actions" : [
        { "add":  { "index": "test_2", "alias": "test" } },
        { "remove_index": { "index": "test" } }  
    ]
}

filtered alias

带有过滤器的别名提供了一种创建同一索引不同“视图”的简便方法。

可以使用 Query DSL 定义过滤器,并使用此别名将其应用于所有“搜索”,“计数”,“按查询删除”和“更多此类操作”。

要创建过滤后的别名,首先需要确保映射中已存在这些字段

PUT /test1
{
  "mappings": {
    "properties": {
      "user" : {
        "type": "keyword"
      }
    }
  }
}

现在可以利用 filter 来创建一个alias,是基于 user 字段

POST /_aliases
{
    "actions" : [
        {
            "add" : {
                 "index" : "test1",
                 "alias" : "alias2",
                 "filter" : { "term" : { "user" : "kimchy" } }
            }
        }
    ]
}

write index

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

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

POST /_aliases
{
    "actions" : [
        {
            "add" : {
                 "index" : "test",
                 "alias" : "alias1",
                 "is_write_index" : true
            }
        },
        {
            "add" : {
                 "index" : "test2",
                 "alias" : "alias1"
            }
        }
    ]
}

如下操作

PUT /alias1/_doc/1
{
    "foo": "bar"
}

相当于

PUT /test/_doc/1

也就是写入到 test 索引中,而不会写入到 test2 中。

  • 交换哪个索引是别名的写入索引,可以利用别名 API 进行原子交换。
POST /_aliases
{
    "actions" : [
        {
            "add" : {
                 "index" : "test",
                 "alias" : "alias1",
                 "is_write_index" : false
            }
        }, {
            "add" : {
                 "index" : "test2",
                 "alias" : "alias1",
                 "is_write_index" : true
            }
        }
    ]
}

创建索引时指定write index alias

PUT products
{
  "aliases": {
    "prod": {
      "is_write_index":  true
    },
    "prod1": {      
    }
  }
}

创建一个叫做 products 的索引,并且它是一个可以写入的索引。在创建这个 products 索引的同时,也创建了一个叫做 prod 的alias。

导入第一个文档

PUT products/_doc/1
{
  "color": "red",
  "weight": 10
}

可以使用如下方式进行搜索

GET prod/_search
GET prod1/_search
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值