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

    你可能感兴趣的文章
    npm—小记
    查看>>
    npm上传自己的项目
    查看>>
    npm介绍以及常用命令
    查看>>
    NPM使用前设置和升级
    查看>>
    npm入门,这篇就够了
    查看>>
    npm切换到淘宝源
    查看>>
    npm切换源淘宝源的两种方法
    查看>>
    npm前端包管理工具简介---npm工作笔记001
    查看>>
    npm包管理深度探索:从基础到进阶全面教程!
    查看>>
    npm升级以及使用淘宝npm镜像
    查看>>
    npm发布包--所遇到的问题
    查看>>
    npm发布自己的组件UI包(详细步骤,图文并茂)
    查看>>
    npm和package.json那些不为常人所知的小秘密
    查看>>
    npm和yarn清理缓存命令
    查看>>
    npm和yarn的使用对比
    查看>>
    npm如何清空缓存并重新打包?
    查看>>
    npm学习(十一)之package-lock.json
    查看>>
    npm安装 出现 npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! 解决方法
    查看>>
    npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
    查看>>
    npm安装教程
    查看>>