$npx -y skills add chaterm/terminal-skills --skill elasticsearchElasticsearch 集群管理
| 1 | # Elasticsearch 集群管理 |
| 2 | |
| 3 | ## 概述 |
| 4 | Elasticsearch 索引管理、查询 DSL、集群运维等技能。 |
| 5 | |
| 6 | ## 集群管理 |
| 7 | |
| 8 | ### 集群状态 |
| 9 | ```bash |
| 10 | # 集群健康 |
| 11 | curl -X GET "localhost:9200/_cluster/health?pretty" |
| 12 | |
| 13 | # 集群状态 |
| 14 | curl -X GET "localhost:9200/_cluster/state?pretty" |
| 15 | |
| 16 | # 集群统计 |
| 17 | curl -X GET "localhost:9200/_cluster/stats?pretty" |
| 18 | |
| 19 | # 节点信息 |
| 20 | curl -X GET "localhost:9200/_nodes?pretty" |
| 21 | curl -X GET "localhost:9200/_nodes/stats?pretty" |
| 22 | |
| 23 | # 分片分配 |
| 24 | curl -X GET "localhost:9200/_cat/shards?v" |
| 25 | curl -X GET "localhost:9200/_cat/allocation?v" |
| 26 | ``` |
| 27 | |
| 28 | ### Cat API |
| 29 | ```bash |
| 30 | # 常用 cat 命令 |
| 31 | curl -X GET "localhost:9200/_cat/health?v" |
| 32 | curl -X GET "localhost:9200/_cat/nodes?v" |
| 33 | curl -X GET "localhost:9200/_cat/indices?v" |
| 34 | curl -X GET "localhost:9200/_cat/shards?v" |
| 35 | curl -X GET "localhost:9200/_cat/segments?v" |
| 36 | curl -X GET "localhost:9200/_cat/count?v" |
| 37 | curl -X GET "localhost:9200/_cat/recovery?v" |
| 38 | curl -X GET "localhost:9200/_cat/thread_pool?v" |
| 39 | ``` |
| 40 | |
| 41 | ## 索引管理 |
| 42 | |
| 43 | ### 索引操作 |
| 44 | ```bash |
| 45 | # 创建索引 |
| 46 | curl -X PUT "localhost:9200/my_index" -H 'Content-Type: application/json' -d' |
| 47 | { |
| 48 | "settings": { |
| 49 | "number_of_shards": 3, |
| 50 | "number_of_replicas": 1 |
| 51 | }, |
| 52 | "mappings": { |
| 53 | "properties": { |
| 54 | "title": { "type": "text" }, |
| 55 | "content": { "type": "text" }, |
| 56 | "timestamp": { "type": "date" }, |
| 57 | "status": { "type": "keyword" } |
| 58 | } |
| 59 | } |
| 60 | }' |
| 61 | |
| 62 | # 删除索引 |
| 63 | curl -X DELETE "localhost:9200/my_index" |
| 64 | |
| 65 | # 查看索引 |
| 66 | curl -X GET "localhost:9200/my_index?pretty" |
| 67 | curl -X GET "localhost:9200/my_index/_mapping?pretty" |
| 68 | curl -X GET "localhost:9200/my_index/_settings?pretty" |
| 69 | |
| 70 | # 索引别名 |
| 71 | curl -X POST "localhost:9200/_aliases" -H 'Content-Type: application/json' -d' |
| 72 | { |
| 73 | "actions": [ |
| 74 | { "add": { "index": "my_index_v2", "alias": "my_index" } }, |
| 75 | { "remove": { "index": "my_index_v1", "alias": "my_index" } } |
| 76 | ] |
| 77 | }' |
| 78 | ``` |
| 79 | |
| 80 | ### 索引设置 |
| 81 | ```bash |
| 82 | # 修改设置 |
| 83 | curl -X PUT "localhost:9200/my_index/_settings" -H 'Content-Type: application/json' -d' |
| 84 | { |
| 85 | "index": { |
| 86 | "number_of_replicas": 2 |
| 87 | } |
| 88 | }' |
| 89 | |
| 90 | # 关闭/打开索引 |
| 91 | curl -X POST "localhost:9200/my_index/_close" |
| 92 | curl -X POST "localhost:9200/my_index/_open" |
| 93 | |
| 94 | # 刷新索引 |
| 95 | curl -X POST "localhost:9200/my_index/_refresh" |
| 96 | |
| 97 | # 强制合并 |
| 98 | curl -X POST "localhost:9200/my_index/_forcemerge?max_num_segments=1" |
| 99 | ``` |
| 100 | |
| 101 | ## 文档操作 |
| 102 | |
| 103 | ### CRUD |
| 104 | ```bash |
| 105 | # 创建文档 |
| 106 | curl -X POST "localhost:9200/my_index/_doc" -H 'Content-Type: application/json' -d' |
| 107 | { |
| 108 | "title": "Hello World", |
| 109 | "content": "This is a test document", |
| 110 | "timestamp": "2024-01-15T10:00:00" |
| 111 | }' |
| 112 | |
| 113 | # 指定 ID 创建 |
| 114 | curl -X PUT "localhost:9200/my_index/_doc/1" -H 'Content-Type: application/json' -d' |
| 115 | { |
| 116 | "title": "Document 1" |
| 117 | }' |
| 118 | |
| 119 | # 获取文档 |
| 120 | curl -X GET "localhost:9200/my_index/_doc/1?pretty" |
| 121 | |
| 122 | # 更新文档 |
| 123 | curl -X POST "localhost:9200/my_index/_update/1" -H 'Content-Type: application/json' -d' |
| 124 | { |
| 125 | "doc": { |
| 126 | "title": "Updated Title" |
| 127 | } |
| 128 | }' |
| 129 | |
| 130 | # 删除文档 |
| 131 | curl -X DELETE "localhost:9200/my_index/_doc/1" |
| 132 | |
| 133 | # 批量操作 |
| 134 | curl -X POST "localhost:9200/_bulk" -H 'Content-Type: application/json' -d' |
| 135 | {"index":{"_index":"my_index","_id":"1"}} |
| 136 | {"title":"Doc 1"} |
| 137 | {"index":{"_index":"my_index","_id":"2"}} |
| 138 | {"title":"Doc 2"} |
| 139 | ' |
| 140 | ``` |
| 141 | |
| 142 | ## 查询 DSL |
| 143 | |
| 144 | ### 基础查询 |
| 145 | ```bash |
| 146 | # 匹配所有 |
| 147 | curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d' |
| 148 | { |
| 149 | "query": { "match_all": {} } |
| 150 | }' |
| 151 | |
| 152 | # 全文搜索 |
| 153 | curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d' |
| 154 | { |
| 155 | "query": { |
| 156 | "match": { |
| 157 | "content": "search text" |
| 158 | } |
| 159 | } |
| 160 | }' |
| 161 | |
| 162 | # 精确匹配 |
| 163 | curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d' |
| 164 | { |
| 165 | "query": { |
| 166 | "term": { |
| 167 | "status": "published" |
| 168 | } |
| 169 | } |
| 170 | }' |
| 171 | |
| 172 | # 范围查询 |
| 173 | curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d' |
| 174 | { |
| 175 | "query": { |
| 176 | "range": { |
| 177 | "timestamp": { |
| 178 | "gte": "2024-01-01", |
| 179 | "lte": "2024-01-31" |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | }' |
| 184 | ``` |
| 185 | |
| 186 | ### 复合查询 |
| 187 | ```bash |
| 188 | # Bool 查询 |
| 189 | curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d' |
| 190 | { |
| 191 | "query": { |
| 192 | "bool": { |
| 193 | "must": [ |
| 194 | { "match": { "title": "elasticsearch" } } |
| 195 | ], |
| 196 | "filter": [ |
| 197 | { "term": { "status": "published" } }, |
| 198 | { "range": { "timestamp": { "gte": "2024-01-01" } } } |
| 199 | ], |
| 200 | "should": [ |
| 201 | { "match": { "content": "tutorial" } } |
| 202 | ], |
| 203 | "must_not": [ |
| 204 | { "term": { "status": "draft" } } |
| 205 | ] |
| 206 | } |
| 207 | } |
| 208 | }' |
| 209 | ``` |
| 210 | |
| 211 | ### 聚合查询 |
| 212 | ```bash |
| 213 | # 聚合 |
| 214 | curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d' |
| 215 | { |
| 216 | "size": 0, |
| 217 | "aggs": { |
| 218 | "status_count": { |
| 219 | "terms": { "field": "status" } |
| 220 | }, |
| 221 | "avg_score": { |
| 222 | "avg": { "field": "score" } |
| 223 | }, |
| 224 | "date_histogram": { |
| 225 | "date_histogram": { |
| 226 | "field": "timestamp", |
| 227 | "calendar_interval": "day" |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | }' |
| 232 | ``` |
| 233 | |
| 234 | ## 备份与恢复 |
| 235 | |
| 236 | ```bash |
| 237 | # 注册快照仓库 |
| 238 | curl -X PUT "localhost:9200/_snapshot/my_backup" -H 'Content-Type: application/json' -d' |
| 239 | { |
| 240 | "type": "fs", |
| 241 | "settings": { |
| 242 | "location": "/backup/elasticsearch" |
| 243 | } |
| 244 | }' |
| 245 | |
| 246 | # 创建快照 |
| 247 | curl -X PUT "localhost:9200/_snapshot |