1. 建立索引
curl -X PUT "http://localhost:9200/my_index" -H "Content-Type: application/json" -d'
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "standard"
},
"content": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}'
2. 刪除索引
curl -X DELETE "http://localhost:9200/my_index"
3. 寫入單個文件
curl -X POST "http://localhost:9200/my_index/_doc/1" -H "Content-Type: application/json" -d'
{
"title": "Real-Time Data Analytics",
"content": "實時資料分析可以幫助企業做出更快的決策。"
}'
4. 批次寫入文件(Bulk API)
curl -X POST "http://localhost:9200/my_index/_bulk" -H "Content-Type: application/json" -d'
{ "index": { "_id": "2" } }
{ "title": "Big Data Processing", "content": "大資料處理技術在現代企業中的應用。" }
{ "index": { "_id": "3" } }
{ "title": "Machine Learning", "content": "機器學習演算法在推薦系統中的應用。" }
'
5. 更新文件
curl -X POST "http://localhost:9200/my_index/_update/1" -H "Content-Type: application/json" -d'
{
"doc": {
"title": "Updated Title",
"content": "更新後的內容。"
}
}'
6. 刪除文件
curl -X DELETE "http://localhost:9200/my_index/_doc/1"
7. 簡單查詢(Match Query)
curl -X GET "http://localhost:9200/my_index/_search" -H "Content-Type: application/json" -d'
{
"query": {
"match": {
"title": "Real-Time"
}
}
}'
8. 多欄位查詢(Multi-Match Query)
curl -X GET "http://localhost:9200/my_index/_search" -H "Content-Type: application/json" -d'
{
"query": {
"multi_match": {
"query": "實時 資料分析",
"fields": ["title", "content"]
}
}
}'
9. 布林查詢(Bool Query)
curl -X GET "http://localhost:9200/my_index/_search" -H "Content-Type: application/json" -d'
{
"query": {
"bool": {
"must": [
{ "match": { "title": "Real-Time" } },
{ "match": { "content": "資料" } }
],
"should": [
{ "match": { "content": "分析" } }
],
"filter": [
{ "term": { "status": "published" } }
]
}
}
}'
10. 分頁查詢(Paging with from and size)
curl -X GET "http://localhost:9200/my_index/_search" -H "Content-Type: application/json" -d'
{
"from": 0,
"size": 10,
"query": {
"match_all": {}
}
}'
11. 排序查詢(Sorting)
curl -X GET "http://localhost:9200/my_index/_search" -H "Content-Type: application/json" -d'
{
"query": {
"match_all": {}
},
"sort": [
{ "timestamp": { "order": "desc" } }
]
}'
12. 高亮顯示(Highlighting)
curl -X GET "http://localhost:9200/my_index/_search" -H "Content-Type: application/json" -d'
{
"query": {
"match": {
"content": "實時 資料分析"
}
},
"highlight": {
"fields": {
"content": {}
}
}
}'
13. 聚合查詢(Aggregations)
curl -X GET "http://localhost:9200/my_index/_search" -H "Content-Type: application/json" -d'
{
"size": 0,
"aggs": {
"status_counts": {
"terms": {
"field": "status.keyword"
}
}
}
}'
14. 範圍查詢(Range Query)
curl -X GET "http://localhost:9200/my_index/_search" -H "Content-Type: application/json" -d'
{
"query": {
"range": {
"timestamp": {
"gte": "2024-01-01T00:00:00",
"lte": "2024-12-31T23:59:59"
}
}
}
}'
15. 字首查詢(Prefix Query)
curl -X GET "http://localhost:9200/my_index/_search" -H "Content-Type: application/json" -d'
{
"query": {
"prefix": {
"title": "real"
}
}
}'
16. 萬用字元查詢(Wildcard Query)
curl -X GET "http://localhost:9200/my_index/_search" -H "Content-Type: application/json" -d'
{
"query": {
"wildcard": {
"title": "real*"
}
}
}'
17. 正規表示式查詢(Regexp Query)
curl -X GET "http://localhost:9200/my_index/_search" -H "Content-Type: application/json" -d'
{
"query": {
"regexp": {
"title": "real.*"
}
}
}'
18. 模糊查詢(Fuzzy Query)
curl -X GET "http://localhost:9200/my_index/_search" -H "Content-Type: application/json" -d'
{
"query": {
"fuzzy": {
"title": {
"value": "real",
"fuzziness": "AUTO"
}
}
}
}'
19. 測試分詞器(Analyze API)
curl -X POST "http://localhost:9200/my_index/_analyze" -H "Content-Type: application/json" -d'
{
"analyzer": "ik_max_word",
"text": "實時資料分析可以幫助企業做出更快的決策。"
}'
20. 獲取索引對映(Get Mapping)
curl -X GET "http://localhost:9200/my_index/_mapping"
21. 獲取索引設定(Get Settings)
curl -X GET "http://localhost:9200/my_index/_settings"
22. 重新整理索引(Refresh Index)
curl -X POST "http://localhost:9200/my_index/_refresh"
23. 強制合併段(Force Merge)
curl -X POST "http://localhost:9200/my_index/_forcemerge?max_num_segments=1"
24. 關閉和開啟索引(Close/Open Index)
# 關閉索引
curl -X POST "http://localhost:9200/my_index/_close"
# 開啟索引
curl -X POST "http://localhost:9200/my_index/_open"
25. 檢視叢集健康狀態(Cluster Health)
curl -X GET "http://localhost:9200/_cluster/health?pretty"
26. 檢視節點資訊(Nodes Info)
curl -X GET "http://localhost:9200/_nodes?pretty"
27. 檢視叢集狀態(Cluster State)
curl -X GET "http://localhost:9200/_cluster/state?pretty"
28. 檢視所有索引(Cat Indices)
curl -X GET "http://localhost:9200/_cat/indices?v"
29. 檢視所有別名(Cat Aliases)
curl -X GET "http://localhost:9200/_cat/aliases?v"
30. 檢視所有分片(Cat Shards)
curl -X GET "http://localhost:9200/_cat/shards?v"