# ElasticSearch中有哪些常用的curl命令
ElasticSearch作為一款流行的分布式搜索和分析引擎,提供了豐富的RESTful API接口。通過curl命令可以方便地與ElasticSearch集群進行交互。本文將詳細介紹ElasticSearch中常用的curl命令,涵蓋集群管理、索引操作、文檔CRUD、搜索查詢等場景。
## 一、集群管理命令
### 1. 檢查集群健康狀態
```bash
curl -X GET "localhost:9200/_cat/health?v"
該命令返回集群的健康狀態(green/yellow/red)、節點數、分片數等關鍵信息。
curl -X GET "localhost:9200/_cat/nodes?v"
顯示集群中所有節點的詳細信息,包括IP地址、堆內存使用情況等。
curl -X GET "localhost:9200/_cluster/stats?human&pretty"
返回包括索引計數、存儲大小、CPU使用率等綜合統計信息。
curl -X PUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d'
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
}
}
'
創建名為my_index的索引,指定3個主分片和每個主分片2個副本。
curl -X GET "localhost:9200/_cat/indices?v"
列出集群中所有索引,顯示健康狀態、文檔數、存儲大小等信息。
curl -X DELETE "localhost:9200/my_index?pretty"
永久刪除指定索引及其所有數據。
curl -X GET "localhost:9200/my_index/_mapping?pretty"
查看索引的字段映射關系(mapping)。
curl -X POST "localhost:9200/my_index/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"title": "ElasticSearch Guide",
"author": "John Doe",
"publish_date": "2023-01-15"
}
'
創建ID為1的文檔,如果已存在則更新。
curl -X GET "localhost:9200/my_index/_doc/1?pretty"
根據文檔ID檢索特定文檔。
curl -X POST "localhost:9200/_bulk?pretty" -H 'Content-Type: application/json' -d'
{ "index" : { "_index" : "my_index", "_id" : "2" } }
{ "title": "Bulk API Tutorial", "author": "Jane Smith" }
{ "delete" : { "_index" : "my_index", "_id" : "1" } }
'
批量執行索引/刪除操作,提高大批量數據處理的效率。
curl -X POST "localhost:9200/my_index/_update/1?pretty" -H 'Content-Type: application/json' -d'
{
"doc": {
"author": "Updated Author Name"
}
}
'
僅更新文檔的指定字段而非替換整個文檔。
curl -X GET "localhost:9200/my_index/_search?q=title:Guide&pretty"
使用URI搜索參數進行簡單查詢。
curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": [
{ "match": { "title": "ElasticSearch" }},
{ "range": { "publish_date": { "gte": "2023-01-01" }}}
]
}
},
"sort": [
{ "publish_date": { "order": "desc" }}
],
"from": 0,
"size": 10
}
'
使用ElasticSearch強大的DSL(Domain Specific Language)進行復雜查詢。
curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d'
{
"aggs": {
"authors": {
"terms": { "field": "author.keyword" }
},
"avg_word_count": {
"avg": { "field": "word_count" }
}
},
"size": 0
}
'
執行聚合操作,統計作者分布和平均字數。
curl -X POST "localhost:9200/_reindex?pretty" -H 'Content-Type: application/json' -d'
{
"source": {
"index": "old_index"
},
"dest": {
"index": "new_index"
}
}
'
將數據從一個索引復制到另一個索引,常用于映射變更或索引遷移。
curl -X GET "localhost:9200/my_index/_explain/1?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"match": { "title": "ElasticSearch" }
}
}
'
查看特定文檔為何匹配或不匹配給定查詢的詳細解釋。
curl -X POST "localhost:9200/my_index/_update/1?pretty" -H 'Content-Type: application/json' -d'
{
"script": {
"source": "ctx._source.word_count = params.new_count",
"params": {
"new_count": 1500
}
}
}
'
通過Painless腳本語言執行復雜的文檔更新操作。
curl -u username:password -X GET "localhost:9200/_cluster/health?pretty"
在啟用安全認證的集群中,使用-u參數提供憑據。
curl -X POST "localhost:9200/_security/api_key?pretty" -H 'Content-Type: application/json' -u elastic:password -d'
{
"name": "my-api-key",
"role_descriptors": {
"read-only-role": {
"cluster": ["monitor"],
"indices": [
{
"names": ["*"],
"privileges": ["read"]
}
]
}
}
}
'
生成具有特定權限的API密鑰用于程序訪問。
curl -X POST "localhost:9200/my_index/_cache/clear?pretty"
清除索引緩存以進行性能測試。
curl -X POST "localhost:9200/my_index/_forcemerge?max_num_segments=1&pretty"
合并索引段以減少段數量和提高查詢性能。
本文介紹了ElasticSearch中最常用的curl命令,涵蓋從基礎集群管理到高級搜索功能的各個方面。掌握這些命令可以幫助您:
實際使用時,建議結合pretty參數使JSON輸出更易讀,并通過-H 'Content-Type: application/json'確保正確的請求頭設置。對于生產環境,還應考慮使用Kibana Dev Tools或ElasticSearch客戶端庫等更高級的工具。
“`
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。