当前位置:网站首页>ES中索引别名(alias)的到底有什么用
ES中索引别名(alias)的到底有什么用
2022-06-26 12:40:00 【Elastic开源社区】
文章目录
1、别名(alias)是什么
1.1 官方定义
按照我的习惯,先给出官方对索引别名的定义:索引别名是用于引用一个或多个现有索引的辅助名称。大多数 Elasticsearch API 接受索引别名来代替索引。
1.2 通俗解释
官方给的解释一般来说都很难让人理解,尤其是没接触或使用过的人尤其如此。网上很多解释说索引别名是为了保护索引,可以让索引相对于调用者隐藏起来。其实这样的解释只有懂别名是什么的人才能看懂,而懂的人又不屑于看了
其实索引别名是对索引绑定的另一个名字,一个别名可以绑定多个索引,一个索引也可以绑定多个别名。至于索引的作用和意义,我将用下面例子来解释。先记结论:索引别名非常常用、非常非常重要。
2、别名(alias)有啥用
2.1 类比域名的作用
在解释别名之前,先思考一下,我们常见的域名有什么用?
2.1.1 比 IP 好记
比如 www.baidu.com,比 110.242.68.4 好记的多。
2.1.2 可以绑定多个IP或者应用
比如:
2.1.3 DNS负载均衡
一般来说,网站服务都不可能只有一台服务器,而是多台服务器组成的高可用集群,而高可用集群往往是需要做负载均衡的,如 Nginx、LVS等。
但是随着用户请求的不断增多,负载均衡也有到性能极限的时候,这是就需要从源头解决问题,也就是DNS分发,或者叫DNS负载均衡。
那么 DNS 是如何转发请求的呢?先来看一下用户的一个请求是如何发送到服务器的
假如某个网站的服务器在北京,那么上海的用户想要获取服务器上的数据,就需要跨越很远的距离,由于传输速度和路由转发等因素,就会导致访问速度非常缓慢。
所以一般很多大型互联网公司都会在全国甚至全世界范围内部署多个数据中心,那么不同地区的用户只需要通过DNS转发用户请求,就近访问即可,这样速度就提升了很多。这个过程就是从用户请求源头
比如,当我从我所处的位置(北京)访问百度的域名的时候,DNS给我指向的IP地址是:110.242.68.4
而当我从一台远程主机访问百度域名的时候,返回的结果的IP地址是不一样的。
2.2 别名的作用
2.2.1 隐藏底层索引
理解了域名的作用,别名也就好解释了,在ES中,索引别名的作用和域名的作用非常类似,别名可以绑定多个索引,客户端对索引的调用可以通过调用别名来完成,这样如果底层索引的所有变动均对用户无感知。调用者并不知道索引别名指向了哪些索引,也不用关心。
2.2.2 封装过滤器
比如下面代码对索引test创建了两个别名,其中,当调用alias_2对索引执行查询时,会自动执行别命中的过滤器
PUT /test
{
"aliases": {
"alias_1": {
},
"alias_2": {
"filter": {
"term": {
"user.id": "kimchy" }
}
}
}
}
当某个索引经常需要执行某些特定查询的时候,可以通过此方式来定义。比如对person索引定义三个别名,不必添加任何查询条件就可以查询出不同年龄段的人群:
- person_lt_18
- person_lt_30
- person_gte_30
3、别名(alias)哪里用:使用场景
3.1 滚动索引

3.2 索引模板/组件模板
PUT _index_template/my_template
{
"index_patterns": ["test_ilm_index_*"],
"template": {
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0,
"index.lifecycle.name": "test_ilm"
}
}
}
3.3 数据流
把索引别名转换为数据流
POST /_data_stream/_migrate/<alias>
4、别名(alias)怎么用
4.1 语法
POST /_aliases
4.2 基本用法
4.2.1 给索引添加别名
给 product 添加别名 product_template
POST _aliases
{
"actions" : [
{
"add" : {
"index" : "product", "alias" : "product_template" } }
]
}
4.2.2 给索引更换别名
把 product 的别名重命名为 product_real (原子操作)
POST /_aliases
{
"actions" : [
{
"remove" : {
"index" : "product", "alias" : "product_template" } },
{
"add" : {
"index" : "product", "alias" : "product_real" } }
]
}
4.2.3 给索引解绑别名
删除 product 的别名 product_real
POST /_aliases
{
"actions" : [
{
"remove" : {
"index" : "product", "alias" : "product_real" } }
]
}
4.2.4 绑定多个别名
一个别名绑定多个索引
POST /_aliases
{
"actions" : [
{
"add" : {
"index" : "product", "alias" : "product_real2" } },
{
"add" : {
"index" : "product2", "alias" : "product_real2" } }
]
}
4.2.5 定义索引时绑定别名
定义索引映射时指定别名
PUT /test
{
"aliases": {
"alias_1": {
},
"alias_2": {
"filter": {
"term": {
"user.id": "kimchy" }
}
}
}
}
4.2.6 作为字段类型
PUT trips
{
"mappings": {
"properties": {
"distance": {
"type": "long"
},
"route_length_miles": {
"type": "alias",
"path": "distance"
},
"transit_mode": {
"type": "keyword"
}
}
}
}
GET _search
{
"query": {
"range" : {
"route_length_miles" : {
"gte" : 39
}
}
}
}
边栏推荐
- Here Document免交互及Expect自动化交互
- IDC报告:百度智能云AI Cloud市场份额连续六次第一
- code force Party Lemonade
- 8. [STM32] timer (TIM) -- interrupt, PWM, input capture experiment (proficient in timer)
- Script - crawl the customized storage path of the cartoon and download it to the local
- MySQL讲解(二)
- Electron official docs series: Processes in Electron
- G - Cow Bowling
- UVA5009 Error Curves三分
- To solve the difficulties of small and medium-sized enterprises, Baidu AI Cloud makes an example
猜你喜欢

享元模式(Flyweight)

Do you know the limitations of automated testing?

Machine learning notes - seasonality of time series

Bridge mode

Beifu realizes the control of time slice size and quantity through CTU and ton

12 SQL optimization schemes summarized by old drivers (very practical)

Basic methods for network diagnosis and hardware troubleshooting of Beifu EtherCAT module

Bifu divides EtherCAT module into multiple synchronization units for operation -- use of sync units

To solve the difficulties of small and medium-sized enterprises, Baidu AI Cloud makes an example

Analysis of state transition diagram of Beifu NC axis
随机推荐
Electron official docs series: Development
首批通过!百度智能云曦灵平台获信通院数字人能力评测权威认证
MySQL explanation (I)
橋接模式(Bridge)
A few lines of code can realize complex excel import and export. This tool class is really powerful!
Beifu PLC realizes zero point power-off hold of absolute value encoder -- use of bias
mysql讲解(一)
Custom encapsulation drop-down component
sed编辑器
Uva11582 [fast power]colossal Fibonacci numbers!
Composite mode
Mysql database explanation (6)
Electron official docs series: Examples
UVA5009 Error Curves三分
Echart stack histogram: add white spacing effect setting between color blocks
Beifu PLC model selection -- how to see whether the motor is a multi turn absolute value encoder or a single turn absolute value encoder
8. [STM32] timer (TIM) -- interrupt, PWM, input capture experiment (proficient in timer)
There are many contents in the widget, so it is a good scheme to support scrolling
Solutions to insufficient display permissions of find and Du -sh
外观模式(Facade)