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

    你可能感兴趣的文章
    NullPointerException Cannot invoke setSkipOutputConversion(boolean) because functionToInvoke is null
    查看>>
    null可以转换成任意非基本类型(int/short/long/float/boolean/byte/double/char以外)
    查看>>
    Numix Core 开源项目教程
    查看>>
    numpy
    查看>>
    NumPy 或 Pandas:将数组类型保持为整数,同时具有 NaN 值
    查看>>
    numpy 或 scipy 有哪些可能的计算可以返回 NaN?
    查看>>
    numpy 数组 dtype 在 Windows 10 64 位机器中默认为 int32
    查看>>
    numpy 数组与矩阵的乘法理解
    查看>>
    NumPy 数组拼接方法-ChatGPT4o作答
    查看>>
    numpy 用法
    查看>>
    Numpy 科学计算库详解
    查看>>
    Numpy.fft.fft和numpy.fft.fftfreq有什么不同
    查看>>
    Numpy.ndarray对象不可调用
    查看>>
    numpy、cv2等操作图片基本操作
    查看>>
    numpy判断对应位置是否相等,all、any的使用
    查看>>
    Numpy如何使用np.umprod重写range函数中i的python
    查看>>
    numpy数组替换其中的值(如1替换为255)
    查看>>
    numpy数组索引-ChatGPT4o作答
    查看>>
    numpy转PIL 报错TypeError: Cannot handle this data type
    查看>>
    NutzCodeInsight 2.0.7 发布,为 nutz-sqltpl 提供友好的 ide 支持
    查看>>