es the way of learning alias

Keywords: ElasticSearch

ES learning path alias

The API in elastic search accepts index names when targeting specific indexes, and multiple indexes when applicable. The index aliases API allows you to alias an index with a name. All APIs automatically convert the alias to the actual index name. Aliases can also be mapped to multiple indexes, and when an alias is specified, the alias will automatically expand to the alias index. Aliases can also be associated with filters that are automatically applied when searching for and routing values. An alias cannot have the same name as an index.

Sample simple API:

POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "test1", "alias" : "alias1" } }
    ]
}//Add alias alias1 to test1 index

POST /_aliases
{
    "actions" : [
        { "remove" : { "index" : "test1", "alias" : "alias1" } }
    ]
}//Remove alias alias1 of test1 index

POST /_aliases
{
    "actions" : [
        { "remove" : { "index" : "test1", "alias" : "alias1" } },
        { "add" : { "index" : "test2", "alias" : "alias1" } }
    ]
}//Remove alias alias1 from test1 index and add alias alias1 to test2 index

POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "test1", "alias" : "alias1" } },
        { "add" : { "index" : "test2", "alias" : "alias1" } }
    ]
}//Add index alias1 to index test1 and test2 respectively

POST /_aliases
{
    "actions" : [
        { "add" : { "indices" : ["test1", "test2"], "alias" : "alias1" } }
    ]
}//Add index alias1 to index test1 and test2 respectively

Filtered Aliases

Aliases with filters provide an easy way to create different "views" of the same index. The filter can be defined by query DSL and applied to all search, count, delete by query and other operations, more similar to the operation using this alias.

Such as:

PUT /test1
{
  "mappings": {
    "type1": {
      "properties": {
        "user" : {
          "type": "keyword"
        }
      }
    }
  }
}

POST /_aliases
{
    "actions" : [
        {
            "add" : {
                 "index" : "test1",
                 "alias" : "alias2",
                 "filter" : { "term" : { "user" : "kimchy" } }
            }
        }
    ]
}

Routing

You can associate route values with aliases. This function can be used to filter aliases together to avoid unnecessary fragmentation.

POST /_aliases
{
    "actions" : [
        {
            "add" : {
                 "index" : "test",
                 "alias" : "alias1",
                 "routing" : "1"
            }
        }
    ]
}

POST /_aliases
{
    "actions" : [
        {
            "add" : {
                 "index" : "test",
                 "alias" : "alias2",
                 "search_routing" : "1,2",
                 "index_routing" : "2"
            }
        }
    ]
}//Search route can have multiple values, but index routing can only have one value

You can create indexes in a simpler way, without having to remember too much syntax

PUT /users
{
    "mappings" : {
        "user" : {
            "properties" : {
                "user_id" : {"type" : "integer"}
            }
        }
    }
}

PUT /users/_alias/user_12
{
    "routing" : "12",
    "filter" : {
        "term" : {
            "user_id" : 12
        }
    }
}
//Define an alias for the users index as user

You can also create an alias when creating an index. For example:

PUT /logs_20162801
{
    "mappings" : {
        "type" : {
            "properties" : {
                "year" : {"type" : "integer"}
            }
        }
    },
    "aliases" : {
        "current_day" : {},
        "2016" : {
            "filter" : {
                "term" : {"year" : 2016 }
            }
        }
    }
}
//Create two aliases, current day and 2016, while creating index logs

Alias removal:

DELETE /logs_20162801/_alias/current_day
//Delete the alias current day of logs

GET /logs_20162801/_alias/*
//Delete all aliases of logs

Sample source:
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html

Posted by railanc4309 on Tue, 31 Mar 2020 18:27:38 -0700