博客
关于我
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/

    你可能感兴趣的文章
    PostgreSQL cube 插件 - 多维空间对象
    查看>>
    PostgreSQL Daily Maintenance - cluster table
    查看>>
    PostgreSQL on Linux 最佳部署手册
    查看>>
    PostgreSQL Oracle 兼容性之 - pipelined
    查看>>
    PostgreSQL Point-In-Time Recovery (Incremental Backup)
    查看>>
    postgresql Streaming Replication监控与注意事项
    查看>>
    postgresql 不需要付费_使用数据传输在PostgreSQL执行 外部连接运算符
    查看>>
    postgresql 主从配置_生产环境postgresql主从环境配置
    查看>>
    postgresql 函数&存储过程 ; 递归查询
    查看>>
    PostgreSQL 分组聚合查询中 filter 子句替换 case when
    查看>>
    PostgreSQL 同步流复制锁瓶颈分析
    查看>>
    PostgreSQL 备份与还原命令 pg_dump
    查看>>
    Postgresql 外部表插件postgres_fdw的安装和使用
    查看>>
    PostgreSQL 如何从崩溃状态恢复(上)
    查看>>
    PostgreSQL 存储过程基本语法
    查看>>
    PostgreSQL 实现批量更新、删除、插入
    查看>>
    PostgreSQL 导入 .gz 备份文件
    查看>>
    PostgreSQL 批量插入&更新数据时报错(ERROR: ON CONFLICT DO UPDATE command cannot affect row a second time)
    查看>>
    PostgreSQL 新增数据返回自增ID
    查看>>
    postgresql 更新多列数据
    查看>>