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"