当前位置:网站首页>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
}
}
}
}
边栏推荐
- 8、【STM32】定时器(TIM)——中断、PWM、输入捕获实验(一文精通定时器)
- Bridge mode
- 【Spark】. Explanation of several icons of scala file in idea
- I have a good word to say, and I admire myself
- Beifu PLC realizes zero point power-off hold of absolute value encoder -- use of bias
- 橋接模式(Bridge)
- Vivado error code [drc pdcn-2721] resolution
- What features are added to Photoshop 2022 23.4.1? Do you know anything
- Explain C language 11 in detail (C language series)
- Use the script to crawl the beautiful sentences of the sentence fan website and store them locally (blessed are those who like to excerpt!)
猜你喜欢

Chapter 01_ Installation and use of MySQL under Linux

Beifu cx5130 card replacement and transfer of existing authorization files

Beifu PLC based on NT_ Shutdown to realize automatic shutdown and restart of controller

IDC报告:百度智能云AI Cloud市场份额连续六次第一

桥接模式(Bridge)

Explain C language 11 in detail (C language series)

ES基于Snapshot(快照)的数据备份和还原

C language: Exercise 2

awk工具

Processsing function random
随机推荐
code force Party Lemonade
KITTI Tracking dataset whose format is letf_ top_ right_ bottom to JDE normalied xc_ yc_ w_ h
Mysql database explanation (III)
Common creation and usage of singletons
MySQL数据库讲解(三)
Es6: iterator
Processsing mouse interactive learning
12 SQL optimization schemes summarized by old drivers (very practical)
MySQL数据库讲解(六)
Generate JDE dot train
code force Party Lemonade
Arcpy -- use of insertlayer() function: adding layers to map documents
Oplg: new generation cloud native observable best practices
Here Document免交互及Expect自动化交互
Nlp-d60-nlp competition D29
Typescript
Digital signal processing -- Design of linear phase type (Ⅰ, Ⅲ) FIR filter (1)
Learn how to develop owl components by hand (7): practical use of owl projects
解中小企业之困,百度智能云打个样
8. [STM32] timer (TIM) -- interrupt, PWM, input capture experiment (proficient in timer)