es - elasticsearch 别名

203 阅读1分钟

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序不断追求完美的过程。

官网地址 : www.elastic.co/guide/en/el…

es别名主要有三个实用的功能 :
1. 通过别名可以实现索引的平滑切换 :
1. 创建索引
2. 为索引配置别名
3. 创建新索引
4. 将别名指向新索引
5. 使用别名搜索
2. 通过别名同时搜索多个索引的内容 :
1. 创建多个索引
2. 多个索引指向同一个别名
3. 使用别名搜索
3. 通过别名为某个索引建立视图 :
1. 创建索引
2. 为索引配置别名
3. 在配置别名时通过filter指定视图
4. 使用别名搜索
4. 通过别名路由到具体分片 :
1. 这个功能的实际用途目前还没想好

以下为操作展示 :

# 创建索引
PUT /test
{
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "title" : {"type": "text"},
      "content" : {"type": "text"}
      }
    }
  }
}

# 索引数据
POST /test/_doc
{
  "title" : "hello",
  "content" : "hello this is my content"
}

# 搜索数据
GET /test/_search
{
  "query": {
    "match": {
      "title": "hello"
    }
  }
}


# 创建别名
POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "test",
        "alias": "test_alias"
      }
    }
  ]
}

# 别名搜索
GET /test_alias/_search
{
  "query": {
    "match": {
      "title": "hello"
    }
  }
}

# 创建新的索引
PUT /test_new
{
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "title" : {"type": "text"},
      "content" : {"type": "text"}
    }
  }
}

# 索引数据
POST /test_new/_doc
{
  "title" : "good",
  "content" : "this is good"
}

# 查询新索引
GET /test_new/_search
{
  "query": {
    "match": {
      "title": "good"
    }
  }
}

# 将别名指向新索引
POST /_aliases
{
  "actions": [
    {
      "remove": {
        "index" : "test",
        "alias": "test_alias"
      }
    },
    {
      "add": {
        "index": "test_new",
        "alias": "test_alias"
      }
    }
  ]
}


# 别名搜素 - 搜索到的是新索引的数据,旧索引搜索不到
GET /test_alias/_search
{
  "query": {
    "match": {
      "title": "good"
    }
  }
}

# 删除别名
POST /_aliases
{
  "actions": [
    {
      "remove": {
        "index": "test_new",
        "alias": "test_alias"
      }
    }
  ]
}

# 配置一个别名对应多个索引
POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "test",
        "alias": "test_alias"
      }
    },
    {
      "add": {
        "index": "test_new",
        "alias": "test_alias"
      }
    }
  ]
}

# 别名搜索
GET /test_alias/_search
{
  "query": {
    "match": {
      "content": "this"
    }
  }
}

# test 增加文档
POST /test/_doc
{
  "title" : "me",
  "content" : "me"
}

# test 搜索
GET /test/_search

# 别名视图
POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "test",
        "alias": "view_alias",
        "filter": {
          "term": {
            "title": "hello"
          }
        }
      }
    }
  ]
}


# 视图搜索
GET /view_alias/_search