当前位置:网站首页>What is the use of index aliases in ES

What is the use of index aliases in ES

2022-06-26 13:32:00 Elastic open source community

1、 Alias (alias) What is it?

1.1 The official definition of

According to my habit , First give the official answer The index alias The definition of : An index alias is a secondary name used to reference one or more existing indexes . majority Elasticsearch API Accept index aliases instead of indexes .

1.2 Popular explanation

Official explanations are generally difficult to understand , This is especially true for people who have not touched or used it . Many online explanations say that the index alias is to protect the index , You can hide the index relative to the caller . In fact, this explanation can only be understood by people who understand what alias is , And those who understand it disdain to read it

In fact, the index alias is another name bound to the index , An alias can bind multiple indexes , An index can also be bound to multiple aliases . As for the function and significance of the index , I will use the following example to explain . Remember the conclusion first : Index aliases are very common 、 Very, very important .

2、 Alias (alias) What's the use

2.1 Analogy to the role of domain names

Before explaining the alias , Think about it first , What is the use of our common domain names ?

2.1.1 Than IP Good record

such as www.baidu.com, Than 110.242.68.4 Much easier to remember .

2.1.2 You can bind multiple IP Or applications

such as :
 Insert picture description here

2.1.3 DNS Load balancing

Generally speaking , It is impossible for a web service to have only one server , It is a highly available cluster composed of multiple servers , High availability clusters often need to be load balanced , Such as Nginx、LVS etc. .
 Insert picture description here
But with the increasing number of user requests , Load balancing also reaches the performance limit , It is necessary to solve the problem from the source , That is to say DNS distribution , Or call it DNS Load balancing .

that DNS How to forward the request ? Let's take a look at how a user's request is sent to the server
 Insert picture description here
Suppose the server of a website is in Beijing , So users in Shanghai want to get the data on the server , It takes a long distance , Due to transmission speed, routing and forwarding, etc , It will cause the access speed to be very slow .

Therefore, many large Internet companies will deploy multiple data centers nationwide or even worldwide , Users in different regions only need to DNS Forward user request , Visit nearby , In this way, the speed increases a lot . This process is from the source of the user request
 Insert picture description here
such as , When I am from where I am ( Beijing ) When visiting Baidu's domain name ,DNS Point to me IP The address is :110.242.68.4
 Insert picture description here
When I access Baidu domain name from a remote host , Of the returned result IP The address is different .
 Insert picture description here

2.2 The function of aliases

2.2.1 Hide the underlying index

Understand the role of domain name , Alias is easy to explain , stay ES in , The function of index alias is very similar to that of domain name , Aliases can be bound to multiple indexes , The client can call the index by calling the alias , In this way, if all changes in the underlying index are imperceptible to the user . The caller does not know which indexes the index alias refers to , And don't care .
 Insert picture description here

2.2.2 Encapsulated filter

For example, the following code pairs the index test Created two aliases , among , When calling alias_2 When executing a query on an index , The filter that will automatically execute the do not hit

PUT /test
{
    
  "aliases": {
    
    "alias_1": {
    },
    "alias_2": {
    
      "filter": {
    
        "term": {
     "user.id": "kimchy" }
      }
    }
  }
}

When an index often needs to execute certain specific queries , You can define... In this way . For example, yes. person The index defines three aliases , People of different ages can be queried without adding any query criteria :

  • person_lt_18
  • person_lt_30
  • person_gte_30

3、 Alias (alias) Where to use : Use scenarios

3.1 Scroll index

 Insert picture description here

3.2 Index template / Component template

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 Data flow

Convert index aliases to data streams

POST /_data_stream/_migrate/<alias>

4、 Alias (alias) How to use it?

4.1 grammar

POST /_aliases

4.2 Basic usage

4.2.1 Add an alias to the index

to product Add alias product_template

POST _aliases
{
    
  "actions" : [
    {
     "add" : {
     "index" : "product", "alias" : "product_template" } }
  ]
}

4.2.2 Alias the index

hold product Rename the alias of to product_real ( Atomic manipulation )

POST /_aliases
{
    
  "actions" : [
    {
     "remove" : {
     "index" : "product", "alias" : "product_template" } },
    {
     "add" : {
     "index" : "product", "alias" : "product_real" } }
  ]
}

4.2.3 Unbind alias for index

Delete product Another name for product_real

POST /_aliases
{
    
  "actions" : [
    {
     "remove" : {
     "index" : "product", "alias" : "product_real" } }
  ]
}

4.2.4 Bind multiple aliases

One alias binds to multiple indexes

POST /_aliases
{
    
  "actions" : [
    {
     "add" : {
     "index" : "product", "alias" : "product_real2" } },
    {
     "add" : {
     "index" : "product2", "alias" : "product_real2" } }
  ]
}

4.2.5 Bind alias when defining index

Specify alias when defining index mapping

PUT /test
{
    
  "aliases": {
    
    "alias_1": {
    },
    "alias_2": {
    
      "filter": {
    
        "term": {
     "user.id": "kimchy" }
      }
    }
  }
}

4.2.6 As field type

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
      }
    }
  }
}
原网站

版权声明
本文为[Elastic open source community]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261229445181.html