es 别名

发布时间 2023-04-13 12:04:53作者: 川流不息&

一 、es 别名介绍

顾名思义 就是为索引起一个 外号,一个别名 可以对应 多个索引。

场景很多

  1. 比如 order_202201,order_202202,order_202203 索引 起个别名 order,这样好处就是 可以关闭历史索引,加快查询数据。

  2. 比如 需求变更 导致 索引 mappings 调整,mappings 变更 添加还好,如果 更新 就必须删除索引重来,同步数据 (数据量较大) 耗时,如果删除 索引重建 会导致 上一个版本 服务暂时不可用。这个时候 如果使用 别名 就可以 无感知切换,索引重建到另一个索引上面,索引重建完成后 发布版本完成 在通过别名切换。这样就可以省去 同步 es 时候的 服务不可用问题。

二、别名操作

1. 创建 mapping

curl --location --request PUT 'http://host:9200/support_cinema_v4' \
--header 'Content-Type: application/json' \
--data '{
    "settings" : {
          "number_of_shards":5,
          "number_of_replicas" : 1
     },
    "mappings": {
        "properties": {
            "id": {
                "type": "keyword"
            },
            "cinemaId": {
                "type": "long"
            },
			"filmId":{
			 "type": "long"
			},
            "cinemaName": {
                "type": "text",
                "analyzer":"ik_max_word",
                "fields": {
                	"keyword": {
                		"type": "keyword",
                		"ignore_above": 256
                    }
                }
            },
			"address": {
                "type": "keyword"
            },
			"location": {
                "type": "geo_point"
            },
            "channelCode": {
                "type": "keyword"
            },
            "channelCityCode": {
                "type": "keyword"
            },
			"acceptSoonOrder": {
                "type": "short"
            },
			"regionName": {
                "type": "keyword"
            },
			"price":{
			 "type": "double"
			},
			"date":{
                "type": "date",
                 "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
            }
        }
    }
}'
2. 别名关联 索引
curl --location --request POST 'http://host:9200/_aliases' \
--header 'Content-Type: application/json' \
--data '{ 
  "actions": [ 
    { 
      "add": {  
        "index": "support_cinema_v4", 
        "alias": "support_cinema" 
      } 
    }
  ] 
}'
3 别名删除
curl --location --request POST 'http://host:9200/_aliases' \
--header 'Content-Type: application/json' \
--data '{
	"actions": [{
		"remove": {
			"alias": "support_cinema",
			"index": "support_cinema_v4"
		}
	}]
}'
4. 无缝切换
curl --location --request POST 'http://host:9200/_aliases' \
--header 'Content-Type: application/json' \
--data '{
	"actions": [{
			"remove": {
				"alias": "support_cinema",
				"index": "support_cinema_v1"
			}
		},
		{
			"add": {
				"alias": "support_cinema",
				"index": "support_cinema_v2"
			}
		}
	]
}'
5. 查看别名 关联索引
curl --location --request GET 'http://host:9200/_cat/aliases/support_cinema'
6. 为别名指定写索引

因为别名是不能写操作的,别名只能读,使用这个操作 后,写别名操作 会转发到 对应的 索引。

curl --location --request POST 'http://host:9200/_aliases' \
--header 'Content-Type: application/json' \
--data '{ 
  "actions": [ 
    { 
      "add": {  
        "index": "support_cinema_v3", 
        "alias": "support_cinema", 
         "is_write_index":true 
      } 
    } 
  ] 
}'

三、spring-data 操作 ElasticsearchRestTemplate

// 保存指定别名 
elasticsearchRestTemplate.save(list,IndexCoordinates.of("support_cinema"));
// 搜索指定别名
elasticsearchRestTemplate.search(nativeSearchQuery, SupportCinema.class, IndexCoordinates.of("support_cinema"));