Elasticsearch常用命令备注

技术解码器 2025-01-20T12:01:13+08:00
0 0 179

一、集群相关命令

1.启动和关闭集群

  • 启动集群:./bin/elasticsearch
  • 关闭集群:curl -XPOST 'http://localhost:9200/_shutdown'

2.查看集群状态

  • 查看集群健康状态:curl -XGET 'http://localhost:9200/_cluster/health?pretty=true'
  • 查看节点状态:curl -XGET 'http://localhost:9200/_cat/nodes?v'
  • 查看索引列表:curl -XGET 'http://localhost:9200/_cat/indices?v'

二、索引相关命令

1.创建和删除索引

  • 创建索引:curl -XPUT 'http://localhost:9200/index_name?pretty'
  • 删除索引:curl -XDELETE 'http://localhost:9200/index_name?pretty'

2.索引操作

  • 添加文档:curl -XPUT 'http://localhost:9200/index_name/_doc/document_id?pretty' -H 'Content-Type: application/json' -d '{"field1": "value1", "field2": "value2"}'
  • 获取文档:curl -XGET 'http://localhost:9200/index_name/_doc/document_id?pretty'
  • 更新文档:curl -XPOST 'http://localhost:9200/index_name/_update/document_id?pretty' -H 'Content-Type: application/json' -d '{"doc": {"field1": "new_value1"}}'
  • 删除文档:curl -XDELETE 'http://localhost:9200/index_name/_doc/document_id?pretty'

三、查询相关命令

1.全文搜索

  • 使用match查询:curl -XPOST 'http://localhost:9200/index_name/_search?pretty' -H 'Content-Type: application/json' -d '{"query": {"match": {"field1": "search_term"}}}'
  • 使用term查询:curl -XPOST 'http://localhost:9200/index_name/_search?pretty' -H 'Content-Type: application/json' -d '{"query": {"term": {"field1": "search_term"}}}'

2.聚合查询

  • 聚合统计:curl -XPOST 'http://localhost:9200/index_name/_search?pretty' -H 'Content-Type: application/json' -d '{"aggs": {"agg_name": {"term": {"field": "field1"}}}}'

3.排序和分页

  • 排序:curl -XPOST 'http://localhost:9200/index_name/_search?pretty' -H 'Content-Type: application/json' -d '{"sort": {"field1": "asc"}}'
  • 分页:curl -XPOST 'http://localhost:9200/index_name/_search?pretty' -H 'Content-Type: application/json' -d '{"from": 0, "size": 10}'

四、集群管理命令

1.节点操作

  • 强制节点脱离集群:curl -XPOST 'http://localhost:9200/_cluster/nodes/node_id/_shutdown'
  • 重新路由分片:curl -XPOST 'http://localhost:9200/_cluster/reroute?pretty' -H 'Content-Type: application/json' -d '{ "commands" : [{ "allocate" : { "index" : "index_name", "shard" : shard_number, "node" : "node_name", "allow_primary" : true } }]}'

2.索引操作

  • 刷新索引:curl -XPOST 'http://localhost:9200/index_name/_refresh'
  • 优化索引:curl -XPOST 'http://localhost:9200/index_name/_optimize'
  • 强制合并索引段:curl -XPOST 'http://localhost:9200/index_name/_forcemerge?max_num_segments=5'

以上是一些常用的Elasticsearch命令备注,涵盖了集群、索引、查询和集群管理等方面的操作。通过这些命令,您可以方便地管理和查询数据,并针对索引和节点进行操作和优化。希望对您有所帮助!

相似文章

    评论 (0)