本文整理 Elasticsearch 在 CentOS 環境下使用 CURL 對索引進行創建、刪除、映射配置、文檔增刪改查的常用命令,適合運維與開發快速參考。
# 創建索引 company
curl -X PUT "localhost:9200/company?pretty"
# 刪除索引
curl -X DELETE "localhost:9200/company?pretty"
# 查看索引信息
curl -X GET "localhost:9200/company?pretty"
# 關閉/打開索引
curl -X POST "localhost:9200/company/_close?pretty"
curl -X POST "localhost:9200/company/_open?pretty"
# 索引別名
curl -X POST "localhost:9200/_aliases?pretty" -H 'Content-Type: application/json' -d'
{ "actions": [ { "add": { "index": "company", "alias": "comp_alias" } } ] }'
# 創建索引並定義映射
curl -X PUT "localhost:9200/company?pretty" -H 'Content-Type: application/json' -d'
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"id": { "type": "keyword" },
"name": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" },
"address": { "type": "text", "analyzer": "ik_max_word" },
"registered_capital": { "type": "double" },
"establish_date": { "type": "date", "format": "yyyy-MM-dd||epoch_millis" },
"status": { "type": "keyword" }
}
}
}'
# 獲取映射
curl -X GET "localhost:9200/company/_mapping?pretty"
# 新增字段映射 (不可修改現有字段類型)
curl -X PUT "localhost:9200/company/_mapping?pretty" -H 'Content-Type: application/json' -d'
{ "properties": { "email": { "type": "keyword" } } }'
# 新增文檔 (指定 ID)
curl -X PUT "localhost:9200/company/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{ "id": "1", "name": "阿里巴巴", "address": "杭州", "registered_capital": 1000000, "establish_date": "1999-04-04", "status": "active" }'
# 新增文檔 (自動生成 ID)
curl -X POST "localhost:9200/company/_doc?pretty" -H 'Content-Type: application/json' -d'
{ "name": "騰訊", "address": "深圳", "registered_capital": 500000, "establish_date": "1998-11-11", "status": "active" }'
# 獲取文檔
curl -X GET "localhost:9200/company/_doc/1?pretty"
# 更新文檔 (部分更新)
curl -X POST "localhost:9200/company/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{ "doc": { "registered_capital": 2000000, "status": "listed" } }'
# 刪除文檔
curl -X DELETE "localhost:9200/company/_doc/1?pretty"
# 批量操作 (_bulk)
curl -X POST "localhost:9200/_bulk?pretty" -H 'Content-Type: application/json' -d'
{ "index": { "_index": "company", "_id": "3" } }
{ "name": "百度", "address": "北京", "registered_capital": 300000, "establish_date": "2000-01-01", "status": "active" }
{ "delete": { "_index": "company", "_id": "2" } }'
# 全文檢索
curl -X GET "localhost:9200/company/_search?pretty" -H 'Content-Type: application/json' -d'
{ "query": { "match": { "name": "阿里" } }, "highlight": { "fields": { "name": {} } } }'
# 精確匹配 + 篩選
curl -X GET "localhost:9200/company/_search?pretty" -H 'Content-Type: application/json' -d'
{ "query": { "bool": { "must": [{ "term": { "status": "active" } }], "filter": [{ "range": { "registered_capital": { "gte": 100000 } } }] } } }'
# 聚合統計
curl -X GET "localhost:9200/company/_search?pretty" -H 'Content-Type: application/json' -d'
{ "size": 0, "aggs": { "by_status": { "terms": { "field": "status" } }, "avg_capital": { "avg": { "field": "registered_capital" } } } }'
# 集群健康
curl -X GET "localhost:9200/_cluster/health?pretty"
# 節點信息
curl -X GET "localhost:9200/_cat/nodes?v"
# 分片分佈
curl -X GET "localhost:9200/_cat/shards?v"
# 索引列表
curl -X GET "localhost:9200/_cat/indices?v"
# 熱線程分析
curl -X GET "localhost:9200/_nodes/hot_threads?pretty"
?pretty:格式化 JSON 輸出-H 'Content-Type: application/json':指定請求體類型-d 或 --data:POST/PUT 請求體-u user:pass