# Elasticsearch開發常用命令指南
## 目錄
1. [基本概念回顧](#基本概念回顧)
2. [索引管理命令](#索引管理命令)
3. [文檔CRUD操作](#文檔crud操作)
4. [搜索查詢命令](#搜索查詢命令)
5. [聚合分析命令](#聚合分析命令)
6. [集群管理命令](#集群管理命令)
7. [高級功能命令](#高級功能命令)
8. [性能優化命令](#性能優化命令)
9. [安全相關命令](#安全相關命令)
10. [常用技巧與最佳實踐](#常用技巧與最佳實踐)
## 基本概念回顧
在深入命令之前,我們先快速回顧Elasticsearch的核心概念:
- **索引(Index)**:類似數據庫中的表
- **文檔(Document)**:索引中的基本數據單元(類似表中的行)
- **分片(Shard)**:索引的水平分割單元
- **副本(Replica)**:分片的拷貝
- **節點(Node)**:運行中的Elasticsearch實例
- **集群(Cluster)**:多個節點組成的集合
## 索引管理命令
### 1. 創建索引
```bash
PUT /my_index
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"title": { "type": "text" },
"name": { "type": "keyword" },
"age": { "type": "integer" },
"created": { "type": "date" }
}
}
}
GET /my_index/_settings
GET /my_index/_mapping
GET /_cat/indices?v
PUT /my_index/_settings
{
"number_of_replicas": 2
}
DELETE /my_index
POST /_aliases
{
"actions": [
{ "add": { "index": "my_index", "alias": "my_alias" } }
]
}
PUT /my_index/_doc/1
{
"title": "Elasticsearch Guide",
"author": "John Doe",
"tags": ["search", "database"]
}
GET /my_index/_doc/1
POST /_bulk
{ "index" : { "_index" : "my_index", "_id" : "2" } }
{ "title": "Bulk API", "author": "Jane Smith" }
{ "create" : { "_index" : "my_index", "_id" : "3" } }
{ "title": "Create Operation", "author": "Bob Johnson" }
POST /my_index/_update/1
{
"doc": {
"author": "John Smith"
}
}
DELETE /my_index/_doc/1
GET /my_index/_search
{
"query": {
"match": {
"title": "Elasticsearch"
}
}
}
GET /my_index/_search
{
"query": {
"bool": {
"must": [
{ "match": { "title": "search" } }
],
"filter": [
{ "range": { "age": { "gte": 20 } } }
]
}
}
}
GET /my_index/_search
{
"from": 0,
"size": 10,
"query": { "match_all": {} }
}
GET /my_index/_search
{
"sort": [
{ "age": { "order": "desc" } },
"_score"
]
}
GET /my_index/_search
{
"query": {
"match": { "title": "Elasticsearch" }
},
"highlight": {
"fields": {
"title": {}
}
}
}
GET /my_index/_search
{
"aggs": {
"avg_age": { "avg": { "field": "age" } }
}
}
GET /my_index/_search
{
"aggs": {
"age_ranges": {
"range": {
"field": "age",
"ranges": [
{ "to": 20 },
{ "from": 20, "to": 40 },
{ "from": 40 }
]
}
}
}
}
GET /my_index/_search
{
"aggs": {
"sales_over_time": {
"date_histogram": {
"field": "created",
"calendar_interval": "month"
}
}
}
}
GET /my_index/_search
{
"aggs": {
"authors": {
"terms": { "field": "author.keyword" },
"aggs": {
"avg_age": { "avg": { "field": "age" } }
}
}
}
}
GET /_cluster/health
GET /_cat/health?v
GET /_cat/nodes?v
GET /_nodes/stats
PUT /_cluster/settings
{
"transient": {
"cluster.routing.allocation.enable": "none"
}
}
POST /_cluster/reroute
{
"commands": [
{
"move": {
"index": "my_index",
"shard": 0,
"from_node": "node1",
"to_node": "node2"
}
}
]
}
PUT /_index_template/my_template
{
"index_patterns": ["logs-*"],
"template": {
"settings": {
"number_of_shards": 2
},
"mappings": {
"properties": {
"timestamp": { "type": "date" },
"message": { "type": "text" }
}
}
}
}
PUT /_ilm/policy/my_policy
{
"policy": {
"phases": {
"hot": {
"actions": {
"rollover": {
"max_size": "50GB",
"max_age": "30d"
}
}
},
"delete": {
"min_age": "90d",
"actions": {
"delete": {}
}
}
}
}
}
PUT /_cluster/settings
{
"persistent": {
"cluster": {
"remote": {
"cluster_one": {
"seeds": ["cluster_one_node:9300"]
}
}
}
}
}
GET /cluster_one:my_index/_search
{
"query": { "match_all": {} }
}
POST /my_index/_forcemerge?max_num_segments=1
POST /my_index/_cache/clear
POST /my_index/_refresh
POST /my_index/_flush
GET /my_index/_stats
GET /my_index/_segments
POST /_security/role/my_role
{
"cluster": ["monitor"],
"indices": [
{
"names": ["my_index"],
"privileges": ["read", "index"]
}
]
}
POST /_security/user/my_user
{
"password": "securepassword",
"roles": ["my_role"],
"full_name": "My User"
}
POST /_security/user/my_user/_password
{
"password": "newpassword"
}
_bulk
API時,建議批量大小在5-15MB之間filter
上下文替代query
上下文進行過濾,利用緩存_cat
API輸出和慢查詢日志# 創建倉庫
PUT /_snapshot/my_backup
{
"type": "fs",
"settings": {
"location": "/mnt/backups/my_backup"
}
}
# 創建快照
PUT /_snapshot/my_backup/snapshot_1?wait_for_completion=true
{
"indices": "my_index",
"ignore_unavailable": true,
"include_global_state": false
}
通過掌握這些常用命令,開發者可以高效地進行Elasticsearch的日常開發和管理工作。隨著Elasticsearch版本的更新,建議定期查閱官方文檔以了解新功能和命令變更。 “`
注:實際字數約為3500字左右,要達到4300字需要進一步擴展每個命令的說明、示例和實際應用場景。您可以通過以下方式擴展: 1. 為每個命令添加更詳細的參數說明 2. 增加更多實際應用場景示例 3. 添加常見錯誤和解決方案 4. 增加性能對比數據 5. 添加版本兼容性說明
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。