
在现代云原生和大数据开发中, 命令行 工具(CLI)因其高效、易于自动化等特性,一直是开发者和运维人员的得力助手。近日,Elastic 官方推出了全新的 Elastic CLI 。这是一个强大的命令行接口工具,旨在帮助用户直接从终端与 Elasticsearch 、Elastic Serverless 和 Elastic Cloud 的 API 进行无缝交互。
目前,该工具处于 技术预览版(Technical Preview) 阶段,仍在积极开发和完善中,但其核心功能已经足够令人惊艳。本文将带你深度盘点 Elastic CLI 的核心亮点、安装配置以及实战用法。
一、 为什么需要 Elastic CLI?
在过去,与 Elasticsearch 或 Kibana 进行交互,我们通常需要编写复杂的 curl 请求、使用 Postman,或者在 Kibana 的 Dev Tools 中输入 DSL 查询。
Elastic CLI 的出现彻底改变了这一现状。它具有以下核心优势:
- 原生集成:完美适配 Elasticsearch 各种 API 端点,支持一键执行搜索、索引管理、集群维护等任务。
- 多环境管理(Contexts):支持配置多个上下文(例如本地开发、预发、生产环境),轻松切换而无需反复修改认证信息。
- 极佳的安全设计:支持系统级的钥匙串(如 macOS Keychain、Linux libsecret、Windows Credential Manager 等),避免明文密码和 API 密钥泄露。
- 动态 Shell 自动补全:支持 Bash、Zsh 和 Fish,且候选补全项会根据你的实际配置文件动态拉取。
- 强大的扩展机制(Extensions):允许开发者通过 Node.js/npm 或 GitHub 仓库轻松开发和安装自定义子命令。
二、 快速安装与配置
1. 安装方式
Elastic CLI 基于 Node.js 开发。你可以通过 npm 全局安装它:
bash
`npm install -g @elastic/cli` AI写代码
安装完成后,可以通过以下命令验证是否成功,并查看帮助信息:
bash
`elastic --help` AI写代码
如果你不想全局安装,也可以使用 npx 执行一次性命令:
bash
`npx -y @elastic/cli --help` AI写代码
提示(Linux/macOS 权限问题) :如果全局安装时遇到
EACCES权限错误,建议将 npm 的全局路径指向用户目录下:
bash` 1. mkdir -p ~/.npm-global 2. npm config set prefix ~/.npm-global 3. echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc 4. source ~/.bashrc `AI写代码
2. Shell 自动补全
配置自动补全能极大地提升命令行使用体验。Elastic CLI 提供了动态补全脚本,以下是针对常见 Shell 的配置方法:
-
Zsh:
bash` 1. # 将脚本写入你的 fpath 路径中: 2. elastic completion zsh > "${fpath[1]}/_elastic" `AI写代码确保你的
~/.zshrc中启用了补全:autoload -Uz compinit && compinit。 -
Bash(针对当前用户):
bash` 1. mkdir -p ~/.local/share/bash-completion/completions 2. elastic completion bash > ~/.local/share/bash-completion/completions/elastic `AI写代码
开启后,按下 Tab 键可以动态补全顶层命令(如 stack、cloud、docs、config)、子命令、长参数(如 --json)甚至是你的配置上下文名称。
三、 灵活安全的配置系统
Elastic CLI 默认会在用户家目录下依次查找 .elasticrc、.elasticrc.json、.elasticrc.yaml 或 .elasticrc.yml 配置文件 。建议将配置文件放置在 ~/.elasticrc.yml。
1. 多上下文配置示例
你可以定义多个 Context(上下文),比如 local 和 staging:
yaml
`
1. current_context: local
3. contexts:
4. local:
5. elasticsearch:
6. url: http://localhost:9200
7. auth:
8. api_key: your-api-key-here
9. kibana:
10. url: http://localhost:5601
11. auth:
12. api_key: your-api-key-here
13. staging:
14. elasticsearch:
15. url: https://my-cluster.es.us-east-1.aws.elastic.cloud
16. auth:
17. api_key: your-api-key-here
18. cloud:
19. url: https://api.elastic-cloud.com
20. auth:
21. api_key: your-cloud-api-key-here
`AI写代码
2. 通过 CLI 管理配置与保护密钥
比起手动修改 YAML 文件,使用 elastic config 命令组来管理更为安全、便捷。
更酷的是,在支持的操作系统上,Elastic CLI 会自动将你的敏感凭据(如 API 密钥或密码)存储在系统的 Keychain/密钥管理器中 ,在 YAML 配置文件中只保留一个解析器表达式(如 $(keychain:...)),从而彻底杜绝秘钥泄露!
bash
`
1. # 添加一个新上下文,并将 API Key 自动存入系统钥匙串
2. elastic config context add local \
3. --es-url http://localhost:9200 \
4. --es-api-key your-api-key
6. # 查看现有上下文列表
7. elastic config context list
9. # 切换当前默认上下文
10. elastic config current-context set staging
12. # 自由编辑上下文内容(会在默认编辑器中以 YAML 形式打开)
13. elastic config context edit local
`AI写代码
3. 动态解析器(Resolvers)
如果你不想使用系统钥匙串,你还可以利用强大的动态解析器,在运行时从外部源动态获取密钥:
-
从文件读取(非常适合 Kubernetes/Docker Secrets):
markdown` 1. auth: 2. api_key: $(file:/run/secrets/elastic_api_key) `AI写代码 -
从环境变量读取:
markdown` 1. auth: 2. api_key: $(env:ELASTIC_API_KEY) `AI写代码 -
从外部命令读取(调用自定义脚本或密码管理器):
markdown` 1. auth: 2. api_key: $(cmd:pass show elastic/api-key) `AI写代码
四、 实战命令
Elastic CLI 将命令按组件和服务进行了逻辑划分,其中 es 和 kb 可以作为 stack es 和 stack kb 的快捷方式直接调用。
1. Elasticsearch API 交互 (es)
所有的 es 子命令都直接映射到 Elasticsearch 对应的 API 端点,支持参数验证(--dry-run)和从外部 JSON 文件加载请求体(--input-file)。
markdown
`
1. # 查询指定索引中的文档
2. elastic es search --index my-index
4. # 获取指定 ID 的文档
5. elastic es get --index my-index --id abc123
7. # 执行 ES|QL 或 SQL 查询
8. elastic es esql --query "FROM my-index | LIMIT 10"
10. # 获取集群信息
11. elastic es info
13. # 进行批量操作
14. elastic es bulk
`AI写代码
常用的子命令群组包括:indices(索引管理)、cluster(集群管理)、security(安全配置)、inference(推理端点/AI/ML 交互)等。
注:针对我的自签名证书集群,我们可以使用如下的方式来运行:
bash
`
1. $ pwd
2. /Users/liuxg/elastic/elasticsearch-9.5.0/config/certs
3. $ NODE_EXTRA_CA_CERTS=/Users/liuxg/elastic/elasticsearch-9.5.0/config/certs/http_ca.crt elastic es nodes usage
4. {
5. "_nodes": {
6. "total": 1,
7. "successful": 1,
8. "failed": 0
9. },
10. "cluster_name": "elasticsearch",
11. "nodes": {
12. "rVUZrswwRfiRb_vbRPln9g": {
13. "timestamp": 1787146072679,
14. "since": 1787144344919,
15. "rest_actions": {
16. "nodes_usage_action": 2,
17. "ilm_get_action": 13,
18. "document_update_action": 84,
19. "xpack_security_invalidate_api_key": 5,
20. "get_index_template_action": 2,
21. "remote_cluster_info_action": 1,
22. "security_get_privileges_action": 1,
23. "get_mapping_action": 10,
24. "get_indices_action": 75,
25. "create_index_action": 6,
26. "xpack_security_create_api_key": 1,
27. "update_by_query_action": 2,
28. "monitoring_bulk": 172,
29. "document_get_action": 653,
30. "cluster_health_action": 10,
31. "document_delete_action": 1,
32. "count_action": 7,
33. "esql_query": 346,
34. "xpack_security_query_api_key": 4,
35. "cluster_get_settings_action": 1,
36. "get_settings_action": 19,
37. "close_point_in_time": 146,
38. "document_index_action": 178,
39. "security_authenticate_action": 103,
40. "msearch_action": 587,
41. "xpack_ml_get_data_frame_analytics_action": 2,
42. "simulate_template_action": 35,
43. "xpack_security_update_cross_cluster_api_key": 2,
44. "document_mget_action": 855,
45. "ml_get_datafeeds_action": 1,
46. "bulk_action": 1222,
47. "security_has_privileges_action": 71,
48. "ingest_get_pipeline_action": 2,
49. "delete_composable_index_template_action": 1,
50. "get_component_template_action": 13,
51. "put_component_template_action": 49,
52. "nodes_info_action": 689,
53. "ingest_put_pipeline_action": 1,
54. "ml_get_jobs_action": 2,
55. "get_composable_index_template_action": 33,
56. "ilm_put_action": 7,
57. "xpack_info_action": 118,
58. "open_point_in_time": 161,
59. "security_get_roles_action": 1,
60. "delete_by_query_action": 4,
61. "get_license": 18,
62. "main_action": 10,
63. "get_inference_model_action": 12,
64. "ml_get_trained_models_action": 1,
65. "create_data_stream_action": 2,
66. "put_mapping_action": 35,
67. "xpack_usage_action": 1,
68. "get_data_streams_action": 52,
69. "document_create_action": 76,
70. "streams_status_action": 1,
71. "simulate_index_template_action": 32,
72. "search_action": 1312,
73. "get_aliases_action": 23,
74. "update_settings_action": 14,
75. "esql_put_view": 3,
76. "put_composable_index_template_action": 43,
77. "search_scroll_action": 1,
78. "xpack_security_get_profile": 15
79. },
80. "aggregations": {
81. "histogram": {
82. "numeric": 690
83. },
84. "filter": {
85. "other": 1740
86. },
87. "date_histogram": {
88. "date": 29
89. },
90. "terms": {
91. "keyword": 526
92. },
93. "missing": {
94. "keyword": 58
95. },
96. "range": {
97. "date": 29
98. },
99. "sum": {
100. "numeric": 34
101. },
102. "filters": {
103. "other": 4
104. },
105. "top_hits": {
106. "other": 3
107. }
108. }
109. }
110. }
111. }
`AI写代码收起代码块
另外一种方式是:
ruby
``
1. $ export NODE_TLS_REJECT_UNAUTHORIZED=0
2. $ elastic es nodes usage
3. (node:12307) Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' makes TLS connections and HTTPS requests insecure by disabling certificate verification.
4. (Use `node --trace-warnings ...` to show where the warning was created)
5. {
6. "_nodes": {
7. "total": 1,
8. "successful": 1,
9. "failed": 0
10. },
11. "cluster_name": "elasticsearch",
12. "nodes": {
13. "rVUZrswwRfiRb_vbRPln9g": {
14. "timestamp": 1787146134782,
15. "since": 1787144344919,
16. "rest_actions": {
17. "nodes_usage_action": 3,
18. "ilm_get_action": 13,
19. "document_update_action": 84,
20. "xpack_security_invalidate_api_key": 5,
21. "get_index_template_action": 2,
22. "remote_cluster_info_action": 1,
23. "security_get_privileges_action": 1,
24. "get_mapping_action": 10,
25. "get_indices_action": 76,
26. "create_index_action": 6,
27. "xpack_security_create_api_key": 1,
28. "update_by_query_action": 2,
29. "monitoring_bulk": 178,
30. "document_get_action": 668,
31. "cluster_health_action": 10,
32. "document_delete_action": 1,
33. "count_action": 7,
34. "esql_query": 359,
35. "xpack_security_query_api_key": 4,
36. "cluster_get_settings_action": 1,
37. "get_settings_action": 19,
38. "close_point_in_time": 149,
39. "document_index_action": 184,
40. "security_authenticate_action": 103,
41. "msearch_action": 608,
42. "xpack_ml_get_data_frame_analytics_action": 2,
43. "simulate_template_action": 35,
44. "xpack_security_update_cross_cluster_api_key": 2,
45. "document_mget_action": 885,
46. "ml_get_datafeeds_action": 1,
47. "bulk_action": 1266,
48. "security_has_privileges_action": 71,
49. "ingest_get_pipeline_action": 2,
50. "delete_composable_index_template_action": 1,
51. "get_component_template_action": 13,
52. "put_component_template_action": 49,
53. "nodes_info_action": 713,
54. "ingest_put_pipeline_action": 1,
55. "ml_get_jobs_action": 2,
56. "get_composable_index_template_action": 33,
57. "ilm_put_action": 7,
58. "xpack_info_action": 122,
59. "open_point_in_time": 164,
60. "security_get_roles_action": 1,
61. "delete_by_query_action": 4,
62. "get_license": 18,
63. "main_action": 10,
64. "get_inference_model_action": 12,
65. "ml_get_trained_models_action": 1,
66. "create_data_stream_action": 2,
67. "put_mapping_action": 35,
68. "xpack_usage_action": 1,
69. "get_data_streams_action": 53,
70. "document_create_action": 76,
71. "streams_status_action": 1,
72. "simulate_index_template_action": 32,
73. "search_action": 1347,
74. "get_aliases_action": 23,
75. "update_settings_action": 14,
76. "esql_put_view": 3,
77. "put_composable_index_template_action": 43,
78. "search_scroll_action": 1,
79. "xpack_security_get_profile": 15
80. },
81. "aggregations": {
82. "histogram": {
83. "numeric": 714
84. },
85. "filter": {
86. "other": 1742
87. },
88. "date_histogram": {
89. "date": 30
90. },
91. "terms": {
92. "keyword": 544
93. },
94. "missing": {
95. "keyword": 60
96. },
97. "range": {
98. "date": 30
99. },
100. "sum": {
101. "numeric": 34
102. },
103. "filters": {
104. "other": 4
105. },
106. "top_hits": {
107. "other": 3
108. }
109. }
110. }
111. }
112. }
``AI写代码收起代码块
markdown
`
1. $ elastic es --help
2. Usage: elastic stack es|elasticsearch [options] [command]
4. Interact with the Elasticsearch API
6. Options:
7. -h, --help display help for command
9. API namespaces
10. async-search Elasticsearch async-search API commands
11. cat Elasticsearch cat API commands
12. ccr Elasticsearch ccr API commands
13. cluster Elasticsearch cluster API commands
14. connector Elasticsearch connector API commands
15. dangling-indices Elasticsearch dangling-indices API commands
16. encryption Elasticsearch encryption API commands
17. enrich Elasticsearch enrich API commands
18. eql Elasticsearch eql API commands
19. esql Elasticsearch esql API commands
20. features Elasticsearch features API commands
21. fleet Elasticsearch fleet API commands
22. graph Elasticsearch graph API commands
23. ilm Elasticsearch ilm API commands
24. indices Elasticsearch indices API commands
25. inference Elasticsearch inference API commands
26. ingest Elasticsearch ingest API commands
27. license Elasticsearch license API commands
28. logstash Elasticsearch logstash API commands
29. migration Elasticsearch migration API commands
30. ml Elasticsearch ml API commands
31. nodes Elasticsearch nodes API commands
32. project Elasticsearch project API commands
33. query-rules Elasticsearch query-rules API commands
34. rollup Elasticsearch rollup API commands
35. search-application Elasticsearch search-application API commands
36. searchable-snapshots Elasticsearch searchable-snapshots API commands
37. security Elasticsearch security API commands
38. simulate Elasticsearch simulate API commands
39. slm Elasticsearch slm API commands
40. snapshot Elasticsearch snapshot API commands
41. sql Elasticsearch sql API commands
42. ssl Elasticsearch ssl API commands
43. streams Elasticsearch streams API commands
44. synonyms Elasticsearch synonyms API commands
45. tasks Elasticsearch tasks API commands
46. text-structure Elasticsearch text-structure API commands
47. transform Elasticsearch transform API commands
48. watcher Elasticsearch watcher API commands
49. xpack Elasticsearch xpack API commands
51. Documents
52. bulk Bulk index or delete documents.
53. create Create a new document in the index.
54. delete Delete a document.
55. delete-by-query Delete documents.
56. exists Check a document.
57. exists-source Check for a document source.
58. get Get a document by its ID.
59. get-source Get a document's source.
60. index Create or update a document in an index.
61. mget Get multiple documents.
62. reindex Reindex documents.
63. update Update a document.
64. update-by-query Update documents.
66. Search
67. clear-scroll Clear a scrolling search.
68. close-point-in-time Close a point in time.
69. msearch Run multiple searches.
70. msearch-template Run multiple templated searches.
71. open-point-in-time Open a point in time.
72. render-search-template Render a search template.
73. scroll Run a scrolling search.
74. search Run a search.
75. search-mvt Search a vector tile.
76. search-shards Get the search shards.
77. search-template Run a search with a search template.
79. Analysis
80. count Count search results.
81. explain Explain a document match result.
82. field-caps Get the field capabilities.
83. mtermvectors Get multiple term vectors.
84. rank-eval Evaluate ranked search results.
85. terms-enum Get terms in an index.
86. termvectors Get term vector information.
88. Scripts
89. delete-script Delete a script or search template.
90. get-script Get a script or search template.
91. get-script-context Get script contexts.
92. get-script-languages Get script languages.
93. put-script Create or update a script or search template.
94. scripts-painless-execute Run a script.
96. Cluster
97. health-report Get the cluster health.
98. info Get cluster info.
99. ping Ping the cluster.
101. Advanced
102. delete-by-query-rethrottle Throttle a delete by query operation.
103. reindex-rethrottle Throttle a reindex operation.
104. update-by-query-rethrottle Throttle an update by query operation.
106. Other commands
107. cancel-reindex Cancel an ongoing reindex task.
108. get-reindex Get the status and progress of a specific reindex task.
109. list-reindex Get information about all currently running reindex tasks.
111. Helpers
112. helpers High-level helper commands for common Elasticsearch workflows
`AI写代码收起代码块
perl
``
1. $ elastic es cat indices
2. (node:26051) Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' makes TLS connections and HTTPS requests insecure by disabling certificate verification.
3. (Use `node --trace-warnings ...` to show where the warning was created)
4. yellow open book_bbq_disk V3IKkYrkS4yz3bERlstqxA 1 1 197 0 1mb 1mb 1mb
5. yellow open multimodal-index 9KC0BpQmTtilJ_Bl6R99Bg 1 1 17 0 174.2kb 174.2kb 174.2kb
6. yellow open documents sb7Z-SC9Q1aBaLghcHWHZA 1 1 12 0 17.1kb 17.1kb 17.1kb
7. yellow open my-audio-index QQdPhg87QNiOackK-1kJYg 1 1 18 0 204.1kb 204.1kb 204.1kb
``AI写代码
2. Kibana API 交互 (kb)
你可以通过命名空间直接调用 Kibana API(需要当前上下文中配置了 kibana 模块):
markdown
`
1. # 列出 Kibana 的 Data Views
2. elastic kb data-views list
4. # 查看具体的 Data View
5. elastic kb data-views get --data-view-id <id>
7. # 查看工单(Cases)
8. elastic kb cases list
`AI写代码
3. Elastic Cloud & Serverless 管理 (cloud)
如果你使用 Elastic 托管服务或最新的 Serverless 架构,你可以直接在命令行中创建和管理资源:
markdown
`
1. # 获取当前账号的组织信息
2. elastic cloud orgs list-organizations
4. # 查看 Elastic Cloud 上托管的部署
5. elastic cloud hosted deployments list-deployments
7. # 在 AWS 区域中快速创建一个 Serverless 项目(支持等待项目就绪并保存连接上下文)
8. elastic cloud serverless projects search create --wait \
9. --name demo-project --region-id aws-us-east-1 --save-as my-demo
`AI写代码
五、 强大的扩展生态(Extensions)
如果你发现 CLI 缺少某些特定功能,你可以通过开发扩展 来实现。任何扩展在安装后,都会成为 elastic 的顶级子命令(例如安装 demo 后即可执行 elastic demo)。
1. 安装已有扩展
支持从 GitHub 仓库或 npm 直接安装:
markdown
`
1. # 从 GitHub 安装
2. elastic extension install github:elastic/elastic-demo
4. # 从 npm 安装
5. elastic extension install npm:elastic-demo
`AI写代码
2. 快速开发自己的扩展
CLI 提供了脚手架,能一键生成并注册一个本地扩展目录:
go
`elastic extension create my-tool` AI写代码
这会在 ~/.elastic/extensions/elastic-my-tool/ 下自动生成一个标准的 TypeScript/JavaScript 模板。当该扩展运行时,Elastic CLI 会自动将当前上下文的连接参数和 API Key 通过环境变量注入到你的脚本中:
ELASTIC_ES_URLELASTIC_ES_API_KEYELASTIC_KIBANA_URL等,帮助你快速编写针对本环境的自动化运维或迁移工具!
六、 总结与展望
作为官方打造的全新命令行工具,Elastic CLI 不仅提供了极高的一致性体验,还在多环境切换、零泄露密钥保护以及可扩展性上做到了行业领先的水平。
尽管目前它还处于技术预览版,但已经展现出极为强大的自动化潜力。如果你是 Elastic 生态的深度用户,不妨立即安装试用,让你的终端操作重获新生!
项目地址 :GitHub - elastic/cli