首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Elasticsearch 索引操作:CentOS 下 CURL 增刪改查指南

     編輯:Java 綜合教學

Elasticsearch 索引操作:CentOS 下 CURL 增刪改查指南

本文整理 Elasticsearch 在 CentOS 環境下使用 CURL 對索引進行創建、刪除、映射配置、文檔增刪改查的常用命令,適合運維與開發快速參考。

目錄

  1. 索引管理
  2. 映射配置
  3. 文檔 CRUD
  4. 查詢與聚合
  5. 集群狀態檢查

一、索引管理

# 創建索引 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
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved