当前位置:网站首页>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
}
}
}
}
边栏推荐
- Processsing function random
- H - Sumsets POJ 2229
- What are the common categories of software testing?
- Word document export (using fixed template)
- Electron official docs series: Get Started
- Oplg: new generation cloud native observable best practices
- HDU 3709 Balanced Number
- MariaDB study notes
- sed编辑器
- Analysis of state transition diagram of Beifu NC axis
猜你喜欢

适配器模式(Adapter)

Processsing function random

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

Log in to the server using SSH key pair

mariadb学习笔记

Use the script to crawl the beautiful sentences of the sentence fan website and store them locally (blessed are those who like to excerpt!)

Beifu PLC realizes zero point power-off hold of absolute value encoder -- use of bias

8、【STM32】定时器(TIM)——中断、PWM、输入捕获实验(一文精通定时器)

ES6 module
![[how to connect the network] Chapter 1: the browser generates messages](/img/6b/e85f29ba97c261e01e177b5e77c423.png)
[how to connect the network] Chapter 1: the browser generates messages
随机推荐
Solutions to insufficient display permissions of find and Du -sh
Processsing mouse interactive learning
MySQL讲解(二)
Analysis and protection of heart blood dripping vulnerability (cve-2014-0160)
[how to connect the network] Chapter 2 (middle): sending a network packet
Mysql database explanation (IV)
Bigint: handles large numbers (integers of any length)
MySQL数据库讲解(四)
MySQL数据库常见故障——遗忘数据库密码
What features are added to Photoshop 2022 23.4.1? Do you know anything
[how to connect the network] Chapter 2 (Part 1): establish a connection, transmit data, and disconnect
LeetCode_ Stack_ Medium_ 150. evaluation of inverse Polish expression
May product upgrade observation station
Word document export (using fixed template)
I - Dollar Dayz
Reflect the technical depth (unable to speed up)
shell脚本详细介绍(四)
Beifu PLC model selection -- how to see whether the motor is a multi turn absolute value encoder or a single turn absolute value encoder
There are many contents in the widget, so it is a good scheme to support scrolling
KITTI Tracking dataset whose format is letf_ top_ right_ bottom to JDE normalied xc_ yc_ w_ h