Elasticsearch Alias第三篇 在endpoint中操作

本文主要介绍在Elasticsearch 1.5版本中如何在Endpoint中操作别名,包括增加、在index创建期间创建、删除别名,使用_cat查看,以及检索存在的别名。示例展示了添加带有路由和过滤器的别名,以及使用不同命令进行别名管理的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要针对1.5版本,主要参考自官网资料,可以理解为一个翻译+实践+扩充的版本

在endpoint中操作

增加别名

能够通过如下的方式添加

PUT /{index}/_alias/{name}
  • index为别名指向的index,可以是 * | _all | glob pattern | name1, name2, …
  • name为别名的名字,这项必须
  • routing 可选,别名绑定的路由
  • filter 可选,别名绑定的过滤器

也可以使用复数个_aliases

例子:

curl -XPUT 'localhost:9200/logs_201305/_alias/2013'

有路由和过滤器的例子

首先创建一个有user_id字段的index

curl -XPUT 'localhost:9200/users' -d '{
    "mappings" : {
        "user" : {
            "properties" : {
                "user_id" : {"type" : "integer"}
            }
        }
    }
}'

然后添加带路由和过滤器的alias

curl -XPUT 'localhost:9200/users/_alias/user_12' -d '{
    "routing" : "12",
    "filter" : {
        "term" : {
            "user_id" : 12
        }
    }
}'

当然,路由也可以拆开为index_routing和search_routing

curl -XPUT 'localhost:9200/users/_alias/user_12' -d '{
    "index_routing" : "12",
    "search_routing" : "11",
    "filter" : {
        "term" : {
            "user_id" : 12
        }
    }
}'

在index创建期间创建别名

可以在创建index期间创建别名

curl -XPUT localhost:9200/logs_20142801 -d '{
    "mappings" : {
        "type" : {
            "properties" : {
                "year" : {"type" : "integer"}
            }
        }
    },
    "aliases" : {
        "current_day" : {},
        "2014" : {
            "filter" : {
                "term" : {"year" : 2014 }
            },
            "routing": "1"
        }
    }
}'

可以观测到创建了current_day,2014两个别名

~ % curl 'localhost:9200/_cat/aliases?v'
alias       index         filter routing.index routing.search
current_day logs_20142801 -      -             -
2014        logs_20142801 *      1             1

删除别名

形式如 /{index}/_alias/{name}

  • index * | _all | glob pattern | name1, name2
  • name * | _all | glob pattern | name1, name2

也可以使用附属各_aliases。例子如下

curl -XDELETE 'localhost:9200/users/_alias/user_12'

使用_cat查看alias

用 _cat查看alias

curl 'localhost:9200/_cat/aliases?v'

成功则返回别名相关信息

alias   index filter routing.index routing.search
alias11 test  -      -             -
alias11 test1 -      -             -

暴力删除别名和指向的index,慎用!

直接用-XDELETE可以删除别名,例如上述例子中alias11指向test和test1

~ % curl -XDELETE 'localhost:9200/alias*'
{"acknowledged":true}%
~ % curl 'localhost:9200/_cat/aliases?v'
alias index filter routing.index routing.search

用-XDELETE就直接删除掉了,不过test和test1也就不见了,所以要慎用!

同理,删除了某个index,也会把这个index和别名的关系也删除了,如果该别名只有指向该index,那么该别名就会被删除!,具体示例如下:

~ % curl -XPOST 'http://localhost:9200/_aliases' -d '
{
    "actions" : [
        {
            "add" : {
                 "index" : "test",
                 "alias" : "alias11"
            }
        }
    ]
}'
{"acknowledged":true}%                                                                                                                                                                                                                                                        ~ % curl -XPOST 'http://localhost:9200/_aliases' -d '
{
    "actions" : [
        {
            "add" : {
                 "index" : "test1",
                 "alias" : "alias11"
            }
        }
    ]
}'
{"acknowledged":true}%                                                                                                                                                                                                                                                        ~ % curl 'localhost:9200/_cat/aliases?v'
alias   index filter routing.index routing.search
alias11 test  *      1             1
alias11 test1 *      1             1
~ % curl -XDELETE 'localhost:9200/test'
{"acknowledged":true}%                                                                                                                                                                                                                                                        ~ % curl 'localhost:9200/_cat/aliases?v'
alias   index filter routing.index routing.search
alias11 test1 *      1             1
~ % curl -XDELETE 'localhost:9200/test1'
{"acknowledged":true}%                                                                                                                                                                                                                                                        ~ % curl 'localhost:9200/_cat/aliases?v'
alias index filter routing.index routing.search

检索存在的别名

获取index与别名的API允许按照别名的名字和索引的名字进行过滤。这个api重定向到主服务器并且获取请求的index别名,如果可能的话,这个api仅仅序列化找到的index的别名。

形式为

/{index}/_alias/{alias}
  • index 要获取别名的index名字。能够通过通配符支持部分名称写法,可以用逗号隔开使用多个index名字,也可以使用指向index的别名
  • alias 在返回的向应用的alias的名字,和index的选项相似,支持通配符和用逗号隔开多个别名
  • ignore_unavailable 如果一个指定名字的index不存在怎么办,如果true的话这些index将会被忽视。

警告:在未来版本的ElasticSearch中,默认的multiple indices选择可能会报错如果其中一个index不存在,这么做是为了和其它的index get API保持一致。

例子

获取index logs_20142801的所有别名

curl -XGET 'localhost:9200/logs_20142801/_alias/*?pretty'

返回的值为

{
  "logs_20142801" : {
    "aliases" : {
      "current_day" : { },
      "2014" : {
        "filter" : {
          "term" : { "year" : 2014 } },
        "index_routing" : "1",
        "search_routing" : "1"
      }
    }
  }
}

所有和别名alias2相关的index

curl -XGET 'localhost:9200/_alias/alias2?pretty'

返回值为

{
  "test" : {
    "aliases" : {
      "alias2" : {
        "filter" : {
          "term" : { "user" : "kitty" } },
        "index_routing" : "1",
        "search_routing" : "2,3"
      }
    }
  },
  "test1" : {
    "aliases" : {
      "alias2" : {
        "filter" : {
          "term" : { "year" : 2014 } },
        "index_routing" : "1",
        "search_routing" : "1"
      }
    }
  }
}

所有以alias开后的别名相关的index

curl -XGET 'localhost:9200/_alias/alias*?pretty'

返回的值为

{
  "test" : {
    "aliases" : {
      "alias2" : {
        "filter" : {
          "term" : { "user" : "kitty" } },
        "index_routing" : "1",
        "search_routing" : "2,3"
      },
      "alias3" : {
        "filter" : {
          "term" : { "user" : "hello" } },
        "index_routing" : "2",
        "search_routing" : "1"
      }
    }
  },
  "test1" : {
    "aliases" : {
      "alias2" : {
        "filter" : {
          "term" : { "year" : 2014 } },
        "index_routing" : "1",
        "search_routing" : "1"
      },
      "alias4" : { }
    }
  }
}

还有一个HEAD变量的获取index与别名的api来检查一个index的别名是否存在,这个index别名是否存在的api也支持和获取index与别名api相似。例如

curl -XHEAD -i 'localhost:9200/_alias/alias2'
HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Content-Length: 0
curl -XHEAD -i 'localhost:9200/_alias/alias*'
HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Content-Length: 0
curl -XHEAD -i 'localhost:9200/test/_alias/*'
HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Content-Length: 0
curl -XHEAD -i 'localhost:9200/not-exist/_alias/*'
HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=UTF-8
Content-Length: 0
### 解决方案 在 Vue 3.0 和 Vite 使用 TypeScript 配置 Axios 时,如果遇到 `'default' is not exported by qs` 的错误,通常是因为 Rollup 或其他构建工具无法正确解析 `qs` 库的默认导出。以下是详细的解决方案: #### 1. 修改依赖导入方式 尝试更改 `qs` 的导入方式以匹配 CommonJS 格式的模块。可以显式地通过解构语法访问其默认导出。 ```typescript import * as QS from 'qs'; const axiosInstance = axios.create({ baseURL: '/api', paramsSerializer: (params) => QS.stringify(params, { arrayFormat: 'brackets' }), }); ``` 这种方式能够避免因默认导出不兼容而导致的问题[^1]。 #### 2. 更新 `vite.config.ts` 为了更好地支持第三方库的兼容性,在 Vite 配置文件中添加 `resolve.alias` 来指定如何处理特定模块。 ```javascript import { defineConfig } from 'vite'; export default defineConfig({ resolve: { alias: { qs: 'qs/dist/esm', // 确保使用 ESM 版本 }, }, }); ``` 此配置会强制加载 `qs` 的 ES Module 版本而不是传统的 CommonJS 文件[^3]。 #### 3. 安装最新版本的 `qs` 有时旧版的 `qs` 可能存在一些未修复的问题或者与现代框架不够适配的情况。因此建议升级到最新的稳定版本。 ```bash npm install qs@latest --save ``` 更新之后重新运行项目并观察是否有改善[^4]。 #### 4. 调整 TypeScript 类型定义 如果你仍然看到类似的类型错误提示,则可能需要手动调整 `.d.ts` 中关于 `qs` 的声明部分来适应当前环境下的需求。 创建一个新的全局扩展文件(如 `global.d.ts`),加入如下内容: ```typescript declare module 'qs' { const content: any; export default content; } ``` 这一步是为了告诉编译器忽略潜在的类型冲突问题。 --- ### 总结 上述方法涵盖了从修改代码逻辑、优化构建流程以及调整外部包等多个角度解决问题的可能性。具体采用哪种取决于实际开发场景和个人偏好。 ```typescript // Example of final implementation after fixes. import axios, { AxiosRequestConfig } from 'axios'; import * as QS from 'qs'; const instance = axios.create({ headers: {'Content-Type': 'application/x-www-form-urlencoded'}, transformRequest: [(data: object): string => QS.stringify(data)] }); instance.post('/example-endpoint', { key: 'value' }) .then(response => console.log(response.data)) .catch(error => console.error('Error:', error)); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值