博客
关于我
Elasticsearch(5) --- 基本命令(集群相关命令、索引CRUD命令、文档CRUD命令)
阅读量:447 次
发布时间:2019-03-06

本文共 4311 字,大约阅读时间需要 14 分钟。

Elasticsearch(5.x) 基本命令

这篇文章主要介绍了Elasticsearch 5.x的基本命令,包括ES集群相关命令、索引CRUD命令和文档CRUD命令。由于Query查询命令较为复杂,故未包含在此。


一、ES集群相关命令

1. _cat 命令

_cat 命令系列用于查询Elasticsearch集群的状态信息。以下是常用命令的示例:

  • 查看单节点的 shard 分配情况

    curl -X GET "localhost:9200/_cat/allocation?v&pretty"
  • 查看各 shard 的详细情况

    curl -X GET "localhost:9200/_cat/shards?v&pretty"
  • 查看指定分片的详细情况

    curl -X GET "localhost:9200/_cat/shards/{index}?v&pretty"
  • 查看 master 节点信息

    curl -X GET "localhost:9200/_cat/master?v&pretty"
  • 查看所有节点信息

    curl -X GET "localhost:9200/_cat/nodes?v&pretty"
  • 查看集群中所有 index 的详细信息

    curl -X GET "localhost:9200/_cat/indices?v&pretty"
  • 查看指定 index 的详细信息

    curl -X GET "localhost:9200/_cat/indices/{index}?v&pretty"
  • 查看各 index 的 segment 详细信息

    curl -X GET "localhost:9200/_cat/segments?v&pretty"
  • 查看指定 index 的 segment 详细信息

    curl -X GET "localhost:9200/_cat/segments/{index}?v&pretty"
  • 查看当前集群的文档总数

    curl -X GET "localhost:9200/_cat/count?v&pretty"
  • 查看指定索引的文档总数

    curl -X GET "localhost:9200/_cat/count/{index}?v&pretty"
  • 查看集群内每个 shard 的 recovery 过程和调整 replica

    curl -X GET "localhost:9200/_cat/recovery?v&pretty"
  • 查看指定索引 shard 的 recovery 过程

    curl -X GET "localhost:9200/_cat/recovery/{index}?v&pretty"
  • 查看集群当前状态(红、黄、绿)

    curl -X GET "localhost:9200/_cat/health?v&pretty"
  • 查看当前集群的 pending task

    curl -X GET "localhost:9200/_cat/pending_tasks?v&pretty"
  • 查看集群中所有 alias 信息

    curl -X GET "localhost:9200/_cat/aliases?v&pretty"
  • 查看指定索引的 alias 信息

    curl -X GET "localhost:9200/_cat/aliases/{alias}?v&pretty"
  • 查看集群各节点内部的 threadpool 统计信息

    curl -X GET "localhost:9200/_cat/thread_pool?v&pretty"
  • 查看集群各节点上的 plugin 信息

    curl -X GET "localhost:9200/_cat/plugins?v&pretty"
  • 查看当前集群各节点的 fielddata 内存使用情况

    curl -X GET "localhost:9200/_cat/fielddata?v&pretty"
  • 查看指定字段的内存使用情况

    curl -X GET "localhost:9200/_cat/fielddata/{fields}?v&pretty"
  • 查看单节点的自定义属性

    curl -X GET "localhost:9200/_cat/nodeattrs?v&pretty"
  • 输出集群中注册的快照存储库

    curl -X GET "localhost:9200/_cat/repositories?v&pretty"
  • 输出当前存在的模板信息

    curl -X GET "localhost:9200/_cat/templates?v&pretty"

注意:上述命令均支持 ?v 参数,会输出表格形式的内容;支持 ?pretty 参数,会格式化输出更具可读性。


2. 示例

1. 节点信息

curl -X GET "localhost:9200/_cat/nodes?v&pretty"

输出示例:

ip         heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name172.18.0.4           52          97   6    0.02    0.11     0.28 mdi       -      es7_021172.18.0.5           57          97   6    0.02    0.11     0.28 mdi       *      es7_01

2. 分片信息

curl -X GET "localhost:9200/_cat/shards?v&pretty"

输出示例:

index                           shard prirep state    docs   store ip         nodemonitoring-es-7-2019.08.30     0     p      STARTED 21333  11.8mb 172.18.0.5kibana_1                       0     r      STARTED     4  22.4kb 172.18.0.5

二、索引 CRUD 命令

1. 查询索引

  • 查看健康状态为 yellow 的索引

    curl -X GET "localhost:9200/_cat/indices?v&health=yellow"
  • 根据文档数量排序

    curl -X GET "localhost:9200/_cat/indices?v&health=yellow&s=docs.count:desc"
  • 查看索引详细信息

    curl -X GET "localhost:9200/my_index/_stats?pretty"

2. 创建索引

curl -X PUT "localhost:9200/student" -H "Content-Type: application/json" -d {    "settings": {        "number_of_shards": 3,        "number_of_replicas": 1    },    "mappings": {        "properties": {            "name": {                "type": "text"            },            "country": {                "type": "keyword"            },            "age": {                "type": "integer"            },            "date": {                "type": "date",                "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"            }        }    }}

3. 删除索引

curl -X DELETE "localhost:9200/index-name"

三、文档 CRUD 命令

1. 创建文档

  • 使用 PUT 方法

    curl -X PUT "localhost:9200/student/_doc/1" -H "Content-Type: application/json" -d {    "name": "徐小小",    "country": "杭州",    "age": "3",    "date": "2019-09-04"}
  • 使用 POST 方法(不指定 ID)

    curl -X POST "localhost:9200/student/_doc" -H "Content-Type: application/json" -d {    "name": "徐小小",    "country": "杭州",    "age": "3",    "date": "2019-09-04"}
  • 使用 POST 方法(指定 ID)

    curl -X POST "localhost:9200/student/_doc/88" -H "Content-Type: application/json" -d {    "name": "徐小小",    "country": "杭州",    "age": "3",    "date": "2019-09-04"}

2. 查看文档

curl -X GET "localhost:9200/student/_doc/1"

3. 更新文档

  • 使用 POST 方法

    curl -X POST "localhost:9200/student/_update/1" -H "Content-Type: application/json" -d {    "doc": {        "age": 5    }}

4. 删除文档

curl -X DELETE "localhost:9200/student/_doc/1"

参考

  • 《Elasticsearch 核心技术与实战》 - 阮一鸣
  • 转载地址:http://ftffz.baihongyu.com/

    你可能感兴趣的文章
    PHP如何获取当前页面的最后修改时间
    查看>>
    PHP如何读取json数据
    查看>>
    PHP字符串
    查看>>
    PHP字符串递增
    查看>>
    php学习之基础语法
    查看>>
    RabbitMQ集群 - 仲裁队列、Raft协议(最详细的选举流程)
    查看>>
    PHP学习总结(11)——PHP入门篇之WAMPServer多站点配置
    查看>>
    PHP学习总结(12)——PHP入门篇之变量
    查看>>
    PHP学习总结(13)——PHP入门篇之常量
    查看>>
    PHP学习总结(14)——PHP入门篇之常用运算符
    查看>>
    PHP学习总结(1)——PHP入门篇之PHP可以做什么?
    查看>>
    PHP学习总结(2)——PHP入门篇之PHP代码标识
    查看>>
    PHP学习总结(3)——PHP入门篇之PHP的echo语句
    查看>>
    PHP学习总结(4)——PHP入门篇之PHP计算表达式
    查看>>
    PHP学习总结(5)——PHP入门篇之PHP字符串
    查看>>
    PHP学习总结(6)——PHP入门篇之PHP语句结束符
    查看>>
    PHP学习总结(7)——PHP入门篇之PHP注释
    查看>>
    rabbitmq重启失败
    查看>>
    PHP学习总结(9)——PHP入门篇之WAMPServer服务控制面板介绍
    查看>>
    php学习笔记---php调试和开发工具整理
    查看>>