Elasticsearch 通过 curl 命令来实现与 Elasticsearch 集群的交互。

  1. 查看版本:可以使用 curl 命令来查看 Elasticsearch 版本等信息。

    curl -u elastic http://localhost:9200
  2. 查看集群状态:可以使用 curl 命令来查看 Elasticsearch 集群的健康状态。

    curl -u elastic http://localhost:9200/_cat/health
  3. 修改集群访问密码:可以使用 PUT 请求来更新用户密码。

    curl -H "Content-Type: application/json" --user elastic -XPUT 'http://localhost:9200/_xpack/security/user/elastic/_password' -d '{"password":"new_password"}'
  4. 查看索引列表:列出所有索引及其相关信息。

    curl -u elastic http://localhost:9200/_cat/indices
  5. 删除索引:删除指定的索引。

    curl -H "Content-Type: application/json" -u elastic -XDELETE http://localhost:9200/testmapping
  6. 创建索引:创建一个新的索引,需要注意索引名称必须为小写。

    curl -H "Content-Type: application/json" -u elastic -XPUT http://localhost:9200/testindex
  7. 查看索引结构:查看指定索引的映射结构。

    curl -XGET -u elastic http://localhost:9200/testindex/_mapping
  8. 给索引设置字段:为索引添加字段。

    curl -H "Content-Type: application/json" -u elastic -XPUT 'http://localhost:9200/testindex/_mapping' -d '{"properties":{"mchnt_id" : {"type" : "keyword","index" : false},"mchnt_name" : {"type" : "text"}}}'
  9. 添加文档:向索引中添加文档。

    curl -u elastic -H "Content-Type: application/json" -XPOST 'http://localhost:9200/testindex/_doc/1' -d '{"mchnt_id": "1","mchnt_name": "鹿角巷"}'
  10. 删除文档:删除索引中的文档。

    curl -u elastic -H "Content-Type: application/json" -XDELETE 'http://localhost:9200/testindex/_doc/3'
  11. 统计索引文档数:统计指定索引下的文档数量。

    curl -s --user elastic -XGET 'http://localhost:9200/_cat/indices/testindex' | awk -F ' ' {'print $7'} | grep -v docs.count
  12. 查看索引下的全部数据:获取指定索引下的所有数据。

    curl -u elastic -XGET 'http://localhost:9200/testindex/_search?pretty=true'
  13. 搜索索引下的全部数据:使用 match_all 查询来搜索索引下的所有数据。

    curl --user elastic -XGET 'http://localhost:9200/testindex/_search?pretty' -H 'Content-Type:application/json' -d '{"query":{"match_all":{}}}'
  14. 根据文档 ID 精准获取索引数据:根据文档 ID 精确查询数据。

    curl -H "Content-Type:application/json" -u elastic -XGET http://localhost:9200/testindex/_doc/20200634?pretty

标签: none

添加新评论