别名

编辑

别名是一组数据流或索引的辅助名称。大多数 Elasticsearch API 接受别名来代替数据流或索引名称。

您可以随时更改别名的数据流或索引。如果在应用程序的 Elasticsearch 请求中使用别名,则可以重新索引数据,而不会停机或更改应用程序的代码。

别名类型

编辑

有两种类型的别名

  • 数据流别名 指向一个或多个数据流。
  • 索引别名 指向一个或多个索引。

一个别名不能同时指向数据流和索引。您也不能将数据流的后备索引添加到索引别名中。

添加别名

编辑

要将现有数据流或索引添加到别名,请使用别名 APIadd 操作。如果别名不存在,则请求会创建它。

resp = client.indices.update_aliases(
    actions=[
        {
            "add": {
                "index": "logs-nginx.access-prod",
                "alias": "logs"
            }
        }
    ],
)
print(resp)
response = client.indices.update_aliases(
  body: {
    actions: [
      {
        add: {
          index: 'logs-nginx.access-prod',
          alias: 'logs'
        }
      }
    ]
  }
)
puts response
const response = await client.indices.updateAliases({
  actions: [
    {
      add: {
        index: "logs-nginx.access-prod",
        alias: "logs",
      },
    },
  ],
});
console.log(response);
POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "logs-nginx.access-prod",
        "alias": "logs"
      }
    }
  ]
}

API 的 indexindices 参数支持通配符 (*)。匹配数据流和索引的通配符模式会返回错误。

resp = client.indices.update_aliases(
    actions=[
        {
            "add": {
                "index": "logs-*",
                "alias": "logs"
            }
        }
    ],
)
print(resp)
const response = await client.indices.updateAliases({
  actions: [
    {
      add: {
        index: "logs-*",
        alias: "logs",
      },
    },
  ],
});
console.log(response);
POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "logs-*",
        "alias": "logs"
      }
    }
  ]
}

删除别名

编辑

要删除别名,请使用别名 API 的 remove 操作。

resp = client.indices.update_aliases(
    actions=[
        {
            "remove": {
                "index": "logs-nginx.access-prod",
                "alias": "logs"
            }
        }
    ],
)
print(resp)
response = client.indices.update_aliases(
  body: {
    actions: [
      {
        remove: {
          index: 'logs-nginx.access-prod',
          alias: 'logs'
        }
      }
    ]
  }
)
puts response
const response = await client.indices.updateAliases({
  actions: [
    {
      remove: {
        index: "logs-nginx.access-prod",
        alias: "logs",
      },
    },
  ],
});
console.log(response);
POST _aliases
{
  "actions": [
    {
      "remove": {
        "index": "logs-nginx.access-prod",
        "alias": "logs"
      }
    }
  ]
}

多个操作

编辑

您可以使用别名 API 在单个原子操作中执行多个操作。

例如,logs 别名指向单个数据流。以下请求将别名的流交换。在此交换过程中,logs 别名不会停机,也永远不会同时指向两个流。

resp = client.indices.update_aliases(
    actions=[
        {
            "remove": {
                "index": "logs-nginx.access-prod",
                "alias": "logs"
            }
        },
        {
            "add": {
                "index": "logs-my_app-default",
                "alias": "logs"
            }
        }
    ],
)
print(resp)
response = client.indices.update_aliases(
  body: {
    actions: [
      {
        remove: {
          index: 'logs-nginx.access-prod',
          alias: 'logs'
        }
      },
      {
        add: {
          index: 'logs-my_app-default',
          alias: 'logs'
        }
      }
    ]
  }
)
puts response
const response = await client.indices.updateAliases({
  actions: [
    {
      remove: {
        index: "logs-nginx.access-prod",
        alias: "logs",
      },
    },
    {
      add: {
        index: "logs-my_app-default",
        alias: "logs",
      },
    },
  ],
});
console.log(response);
POST _aliases
{
  "actions": [
    {
      "remove": {
        "index": "logs-nginx.access-prod",
        "alias": "logs"
      }
    },
    {
      "add": {
        "index": "logs-my_app-default",
        "alias": "logs"
      }
    }
  ]
}

多个操作结果

编辑

当使用多个操作时,如果某些操作成功而某些操作失败,将返回每个操作结果的列表。

考虑与之前示例类似的动作列表,但现在使用一个尚不存在的别名 log-non-existing。在这种情况下,remove 操作将失败,但 add 操作将成功。响应将包含列表 action_results,其中包含每个请求的操作的结果。

resp = client.indices.update_aliases(
    actions=[
        {
            "remove": {
                "index": "index1",
                "alias": "logs-non-existing"
            }
        },
        {
            "add": {
                "index": "index2",
                "alias": "logs-non-existing"
            }
        }
    ],
)
print(resp)
response = client.indices.update_aliases(
  body: {
    actions: [
      {
        remove: {
          index: 'index1',
          alias: 'logs-non-existing'
        }
      },
      {
        add: {
          index: 'index2',
          alias: 'logs-non-existing'
        }
      }
    ]
  }
)
puts response
const response = await client.indices.updateAliases({
  actions: [
    {
      remove: {
        index: "index1",
        alias: "logs-non-existing",
      },
    },
    {
      add: {
        index: "index2",
        alias: "logs-non-existing",
      },
    },
  ],
});
console.log(response);
POST _aliases
{
  "actions": [
    {
      "remove": {
        "index": "index1",
        "alias": "logs-non-existing"
      }
    },
    {
      "add": {
        "index": "index2",
        "alias": "logs-non-existing"
      }
    }
  ]
}

API 返回以下结果

{
  "acknowledged": true,
  "errors": true,
  "action_results": [
    {
      "action": {
        "type": "remove",
        "indices": [ "index1" ],
        "aliases": [ "logs-non-existing" ],
      },
      "status": 404,
      "error": {
        "type": "aliases_not_found_exception",
        "reason": "aliases [logs-non-existing] missing",
        "resource.type": "aliases",
        "resource.id": "logs-non-existing"
      }
    },
    {
      "action": {
        "type": "add",
        "indices": [ "index2" ],
        "aliases": [ "logs-non-existing" ],
      },
      "status": 200
    }
  ]
}

允许操作列表部分成功可能无法提供所需的结果。将 must_exist 设置为 true 可能更合适,这将导致如果单个操作失败,则整个操作列表都将失败。

在索引创建时添加别名

编辑

您还可以使用组件索引模板,以便在创建索引或数据流时添加索引或数据流别名。

resp = client.cluster.put_component_template(
    name="my-aliases",
    template={
        "aliases": {
            "my-alias": {}
        }
    },
)
print(resp)

resp1 = client.indices.put_index_template(
    name="my-index-template",
    index_patterns=[
        "my-index-*"
    ],
    composed_of=[
        "my-aliases",
        "my-mappings",
        "my-settings"
    ],
    template={
        "aliases": {
            "yet-another-alias": {}
        }
    },
)
print(resp1)
response = client.cluster.put_component_template(
  name: 'my-aliases',
  body: {
    template: {
      aliases: {
        "my-alias": {}
      }
    }
  }
)
puts response

response = client.indices.put_index_template(
  name: 'my-index-template',
  body: {
    index_patterns: [
      'my-index-*'
    ],
    composed_of: [
      'my-aliases',
      'my-mappings',
      'my-settings'
    ],
    template: {
      aliases: {
        "yet-another-alias": {}
      }
    }
  }
)
puts response
const response = await client.cluster.putComponentTemplate({
  name: "my-aliases",
  template: {
    aliases: {
      "my-alias": {},
    },
  },
});
console.log(response);

const response1 = await client.indices.putIndexTemplate({
  name: "my-index-template",
  index_patterns: ["my-index-*"],
  composed_of: ["my-aliases", "my-mappings", "my-settings"],
  template: {
    aliases: {
      "yet-another-alias": {},
    },
  },
});
console.log(response1);
# Component template with index aliases
PUT _component_template/my-aliases
{
  "template": {
    "aliases": {
      "my-alias": {}
    }
  }
}

# Index template with index aliases
PUT _index_template/my-index-template
{
  "index_patterns": [
    "my-index-*"
  ],
  "composed_of": [
    "my-aliases",
    "my-mappings",
    "my-settings"
  ],
  "template": {
    "aliases": {
      "yet-another-alias": {}
    }
  }
}

您还可以在创建索引 API 请求中指定索引别名。

resp = client.indices.create(
    index="<my-index-{now/d}-000001>",
    aliases={
        "my-alias": {}
    },
)
print(resp)
response = client.indices.create(
  index: '<my-index-{now/d}-000001>',
  body: {
    aliases: {
      "my-alias": {}
    }
  }
)
puts response
const response = await client.indices.create({
  index: "<my-index-{now/d}-000001>",
  aliases: {
    "my-alias": {},
  },
});
console.log(response);
# PUT <my-index-{now/d}-000001>
PUT %3Cmy-index-%7Bnow%2Fd%7D-000001%3E
{
  "aliases": {
    "my-alias": {}
  }
}

查看别名

编辑

要获取集群别名列表,请使用没有参数的获取别名 API

resp = client.indices.get_alias()
print(resp)
response = client.indices.get_alias
puts response
const response = await client.indices.getAlias();
console.log(response);
GET _alias

_alias 之前指定数据流或索引以查看其别名。

resp = client.indices.get_alias(
    index="my-data-stream",
)
print(resp)
response = client.indices.get_alias(
  index: 'my-data-stream'
)
puts response
const response = await client.indices.getAlias({
  index: "my-data-stream",
});
console.log(response);
GET my-data-stream/_alias

_alias 之后指定别名以查看其数据流或索引。

resp = client.indices.get_alias(
    name="logs",
)
print(resp)
response = client.indices.get_alias(
  name: 'logs'
)
puts response
const response = await client.indices.getAlias({
  name: "logs",
});
console.log(response);
GET _alias/logs

写入索引

编辑

您可以使用 is_write_index 为别名指定写入索引或数据流。Elasticsearch 将别名的任何写入请求路由到此索引或数据流。

resp = client.indices.update_aliases(
    actions=[
        {
            "add": {
                "index": "logs-nginx.access-prod",
                "alias": "logs"
            }
        },
        {
            "add": {
                "index": "logs-my_app-default",
                "alias": "logs",
                "is_write_index": True
            }
        }
    ],
)
print(resp)
response = client.indices.update_aliases(
  body: {
    actions: [
      {
        add: {
          index: 'logs-nginx.access-prod',
          alias: 'logs'
        }
      },
      {
        add: {
          index: 'logs-my_app-default',
          alias: 'logs',
          is_write_index: true
        }
      }
    ]
  }
)
puts response
const response = await client.indices.updateAliases({
  actions: [
    {
      add: {
        index: "logs-nginx.access-prod",
        alias: "logs",
      },
    },
    {
      add: {
        index: "logs-my_app-default",
        alias: "logs",
        is_write_index: true,
      },
    },
  ],
});
console.log(response);
POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "logs-nginx.access-prod",
        "alias": "logs"
      }
    },
    {
      "add": {
        "index": "logs-my_app-default",
        "alias": "logs",
        "is_write_index": true
      }
    }
  ]
}

如果别名指向多个索引或数据流并且未设置 is_write_index,则别名会拒绝写入请求。如果索引别名指向一个索引并且未设置 is_write_index,则该索引会自动充当写入索引。数据流别名不会自动设置写入数据流,即使别名指向一个数据流也是如此。

我们建议使用数据流来存储仅追加的时间序列数据。如果需要更新或删除现有时间序列数据,可以直接在数据流后备索引上执行更新或删除操作。如果您经常使用相同的 _id 发送多个文档,并期望最后的写入获胜,您可能需要使用带有写入索引的索引别名。请参阅在没有数据流的情况下管理时间序列数据

过滤别名

编辑

filter 选项使用查询 DSL 来限制别名可以访问的文档。

resp = client.indices.update_aliases(
    actions=[
        {
            "add": {
                "index": "my-index-2099.05.06-000001",
                "alias": "my-alias",
                "filter": {
                    "bool": {
                        "filter": [
                            {
                                "range": {
                                    "@timestamp": {
                                        "gte": "now-1d/d",
                                        "lt": "now/d"
                                    }
                                }
                            },
                            {
                                "term": {
                                    "user.id": "kimchy"
                                }
                            }
                        ]
                    }
                }
            }
        }
    ],
)
print(resp)
response = client.indices.update_aliases(
  body: {
    actions: [
      {
        add: {
          index: 'my-index-2099.05.06-000001',
          alias: 'my-alias',
          filter: {
            bool: {
              filter: [
                {
                  range: {
                    "@timestamp": {
                      gte: 'now-1d/d',
                      lt: 'now/d'
                    }
                  }
                },
                {
                  term: {
                    'user.id' => 'kimchy'
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
)
puts response
const response = await client.indices.updateAliases({
  actions: [
    {
      add: {
        index: "my-index-2099.05.06-000001",
        alias: "my-alias",
        filter: {
          bool: {
            filter: [
              {
                range: {
                  "@timestamp": {
                    gte: "now-1d/d",
                    lt: "now/d",
                  },
                },
              },
              {
                term: {
                  "user.id": "kimchy",
                },
              },
            ],
          },
        },
      },
    },
  ],
});
console.log(response);
POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "my-index-2099.05.06-000001",
        "alias": "my-alias",
        "filter": {
          "bool": {
            "filter": [
              {
                "range": {
                  "@timestamp": {
                    "gte": "now-1d/d",
                    "lt": "now/d"
                  }
                }
              },
              {
                "term": {
                  "user.id": "kimchy"
                }
              }
            ]
          }
        }
      }
    }
  ]
}

过滤器仅在使用查询 DSL 时应用,并且在按 ID 检索文档时不会应用。

路由

编辑

使用 routing 选项将别名的请求路由到特定分片。这使您可以利用分片缓存来加快搜索速度。数据流别名不支持路由选项。

resp = client.indices.update_aliases(
    actions=[
        {
            "add": {
                "index": "my-index-2099.05.06-000001",
                "alias": "my-alias",
                "routing": "1"
            }
        }
    ],
)
print(resp)
response = client.indices.update_aliases(
  body: {
    actions: [
      {
        add: {
          index: 'my-index-2099.05.06-000001',
          alias: 'my-alias',
          routing: '1'
        }
      }
    ]
  }
)
puts response
const response = await client.indices.updateAliases({
  actions: [
    {
      add: {
        index: "my-index-2099.05.06-000001",
        alias: "my-alias",
        routing: "1",
      },
    },
  ],
});
console.log(response);
POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "my-index-2099.05.06-000001",
        "alias": "my-alias",
        "routing": "1"
      }
    }
  ]
}

使用 index_routingsearch_routing 为索引和搜索指定不同的路由值。如果指定,这些选项将覆盖其各自操作的 routing 值。

resp = client.indices.update_aliases(
    actions=[
        {
            "add": {
                "index": "my-index-2099.05.06-000001",
                "alias": "my-alias",
                "search_routing": "1",
                "index_routing": "2"
            }
        }
    ],
)
print(resp)
response = client.indices.update_aliases(
  body: {
    actions: [
      {
        add: {
          index: 'my-index-2099.05.06-000001',
          alias: 'my-alias',
          search_routing: '1',
          index_routing: '2'
        }
      }
    ]
  }
)
puts response
const response = await client.indices.updateAliases({
  actions: [
    {
      add: {
        index: "my-index-2099.05.06-000001",
        alias: "my-alias",
        search_routing: "1",
        index_routing: "2",
      },
    },
  ],
});
console.log(response);
POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "my-index-2099.05.06-000001",
        "alias": "my-alias",
        "search_routing": "1",
        "index_routing": "2"
      }
    }
  ]
}