elasticsearch研习(八)

#通过在索引创建时提供索引级别设置index.routing_partition_size来完成的。随着分区大小的增加,数据分布越均匀,代价是每个请求必须搜索更多分片。

PUT r6
{
  "mappings": {
    "doc":{
      "_routing":{
        "required": true
      }
    }
  },
  "settings": {
    "index.routing_partition_size": 3 
  }
}
#也就是说,_routing字段用于计算索引中的一组分片,然后_id用于选择该集合中的分片。

#要启用此功能,index.routing_partition_size应具有大于1且小于index.number_of_shards的值。


DELETE /r6

#准备数据,创建别名
PUT l1/doc/1
{
  "title":"我想要睡你"
}

PUT l2/doc/1
{
  "title":"你却拿我当兄弟"
}

PUT l3/doc/1
{
  "title":"不过,我不介意"
}
# 创建别名
POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "l1",
        "alias": "a1"
      }
    }
  ]
}
#我们使用add为索引l1添加一个别名a1

GET a1/doc/1

#查看别名
GET l1/_alias

#删除别名
POST /_aliases
{
  "actions": [
    {
      "remove": {
        "index": "l1",
        "alias": "a1"
      }
    }
  ]
}
#重命名别名是一个简单的remove操作,然后执行add操作,无需担心短时间内别名不指向索引,因为这个操作原子性的:

POST /_aliases
{
  "actions": [
    {"remove": {"index": "l1", "alias": "a1"}},
    {"add": {"index": "l2", "alias": "a1"}}
  ]
}

#为多个索引指向同样的别名
POST /_aliases
{
  "actions": [
    {"add": {"index": "l1", "alias": "a1"}},
    {"add": {"index": "l2", "alias": "a1"}},
    {"add": {"index": "l3", "alias": "a1"}}
  ]
}
#使用indices数组语法在一个操作中为多个索引指向同一个别名

POST /_aliases
{
  "actions": [
    {"add": {"indices": ["l1", "l2", "l3"], "alias": "a2"}}
  ]
}

#这个套路同样适用于在一个操作中为一个索引指向多个别名:
POST /_aliases
{
  "actions": [
    {"add": {"index": "l1", "aliases": ["a1", "a2", "a3"]}}
  ]
}
POST /_aliases
{
  "actions": [
    {"add": {"index": "l*", "alias": "f1"}}
  ]
}
#别名交换
POST /_aliases
{
  "actions": [
    {"add": {"index": "l1", "alias": "a1"}},
    {"remove_index":{"index":"a1"}}
  ]
}

DELETE l1,l2,l3

#过滤器别名
PUT l4
{
  "mappings": {
    "doc":{
      "properties":{
        "year":{
          "type":"integer"
        },
        "method":{
          "type":"keyword"
        }
      }
    }
  }
}
PUT l4/doc/1
{
  "year":2019,
  "method":"GET"
}
PUT l4/doc/2
{
  "year":2018,
  "method":"POST"
}
PUT l4/doc/3
{
  "year":2019,
  "method":"POST"
}


#过滤器别名
#创建别名过滤器
POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "l4",
        "alias": "a4",
        "filter": {"term": {
          "year": 2019
        }}
      }
    }
  ]
}

GET l4/doc/_search

GET a4/doc/_search


DELETE l4



POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "l1",
        "alias": "a1",
        "routing": "1"
      }
    }
  ]
}
POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "l4",
        "alias": "a4",
        "search_routing": "1,2",
        "index_routing": "1"
      }
    }
  ]
}
#如上例所示,搜索路由(search_routing)可能包含多个(以英文逗号分隔)值,但索引路由(index_routing)就只能包含一个值。如果使用路由别名的搜索也有路由参数,则使用在该参数中指定的搜索别名路由和路由的交集

GET /a4/_search?q=year:2019&routing=2,3


POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "l1",
        "alias": "a1",
        "is_write_index": true
      }
    },
    {
      "add": {
        "index": "l2",
        "alias": "a1"
      }
    }
  ]
}

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "l1",
        "alias": "a1",
        "is_write_index": true
      }
    },
    {
      "add": {
        "index": "l2",
        "alias": "a1"
      }
    }
  ]
}

PUT a1/doc/2
{
  "title": "hi gay"
}

#上例中,PUT a1/doc/2相当于向l1/doc/2写入。

GET l1/doc/_search
GET l1/doc/2


#那么如果有些文档的写入要从l1切换到l2该怎么办呢?要交换哪个索引是别名的写入操作,可以利用别名API进行原子交换,交换不依赖于操作的顺序。

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "l1",
        "alias": "a1",
        "is_write_index": false
      }
    },
    {
      "add": {
        "index": "l2",
        "alias": "a1",
        "is_write_index": true
      }
    }
  ]
}

#添加单个索引别名
#PUT /{index}/_alias/{name}
#PUT /{index}/_alias/{name}?routing=user1



#在索引创建期间指定别名
PUT l6
{
  "mappings": {
    "doc":{
      "properties":{
        "year":{
          "type":"integer"
        }
      }
    }
  },
  "aliases": {
    "current_day": {},
    "2019":{
      "filter": {
        "term": {
          "year": 2019
        }
      }
    }
  }
}

#删除别名
#DELETE /l1/_alias/a1
#DELETE /l2/_aliases/a*


#检索现有别名
GET /{index}/_alias/{alias}
GET l1/_alias/a*    # 查询索引l1指向以a开头的所有别名
GET l1/_alias/*     # 查询索引l1所有的别名

GET /_alias/a1
GET /_alias/2019

#通过HEAD检测某个别名是否存在:
HEAD /_alias/2019
HEAD /_alias/20*
HEAD /l2/_alias/*

版权声明:除特别注明外,本站所有文章均为王晨曦个人站点原创

转载请注明:出处来自王晨曦个人站点 » elasticsearch研习(八)

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注