以下是一些 Elasticsearch 常用的命令,涵盖了索引管理、数据操作和集群管理等方面:
基本操作
-
检查集群状态:
curl -X GET "localhost:9200/_cluster/health?pretty"
查看集群健康状态和基本信息。
-
查看所有索引:
curl -X GET "localhost:9200/_cat/indices?v"
列出所有索引及其状态、文档数量等信息。
-
创建索引:
curl -X PUT "localhost:9200/my_index?pretty"
创建一个名为
my_index
的索引。 -
删除索引:
curl -X DELETE "localhost:9200/my_index?pretty"
删除名为
my_index
的索引。 -
查看索引映射:
curl -X GET "localhost:9200/my_index/_mapping?pretty"
查看索引
my_index
的字段映射和数据类型。
文档操作
-
索引文档:
curl -X POST "localhost:9200/my_index/_doc/1" -H 'Content-Type: application/json' -d' { "user": "john", "message": "Hello, Elasticsearch!" } '
将一条文档插入到
my_index
索引中,ID 为1
。 -
获取文档:
curl -X GET "localhost:9200/my_index/_doc/1?pretty"
获取 ID 为
1
的文档内容。 -
更新文档:
curl -X POST "localhost:9200/my_index/_doc/1/_update" -H 'Content-Type: application/json' -d' { "doc": { "message": "Updated message" } } '
更新 ID 为
1
的文档中的message
字段。 -
删除文档:
curl -X DELETE "localhost:9200/my_index/_doc/1?pretty"
删除 ID 为
1
的文档。
搜索查询
-
基本搜索:
curl -X GET "localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d' { "query": { "match": { "message": "Hello" } } } '
在
my_index
索引中搜索message
字段包含 “Hello” 的文档。 -
分页搜索:
curl -X GET "localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d' { "from": 0, "size": 10, "query": { "match_all": {} } } '
从结果的第 0 条开始,返回 10 条文档。
-
聚合查询:
curl -X GET "localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d' { "aggs": { "message_count": { "terms": { "field": "message.keyword" } } } } '
按
message
字段进行聚合统计。
集群和节点管理
-
集群健康状态:
curl -X GET "localhost:9200/_cluster/health?pretty"
查看集群的健康状态。
-
查看节点信息:
curl -X GET "localhost:9200/_nodes?pretty"
获取集群中所有节点的信息。
-
查看节点统计信息:
curl -X GET "localhost:9200/_nodes/stats?pretty"
查看节点的统计信息,如 CPU 使用率、内存使用情况等。
其他管理
- 查看 Elasticsearch 版本:
查看 Elasticsearch 的版本和基本信息。curl -X GET "localhost:9200"
- 查看只读状态
存在“read_only_allow_delete”:“true” 字段说明只读
curl -u elastic:111111 http://127.0.0.1:9200/_settings?pretty
- 解除只读状态
curl -XPUT -H "Content-Type: application/json" http://127.0.0.1:9200/_all/_settings -d '{"index.blocks.read_only_allow_delete": false}'
这些命令可以帮助您在 Elasticsearch 中进行日常操作、数据管理和故障排除。