前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ElasticSearch常用命令

ElasticSearch常用命令

作者头像
十毛
发布2019-03-27 14:57:43
1K0
发布2019-03-27 14:57:43
举报

ElasticSearch用作全文检索,一直没有好好研究它的命令,每次使用的时候都要谷歌搜索,效率太低。 本文把一些特别常用的运维及操作命令整理一下,方便归类记忆

状态查询

  • 获取所有_cat系列的操作
代码语言:javascript
复制
curl http://localhost:9200/_cat
=^.^=
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/tasks
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/thread_pool/{thread_pools}
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}
/_cat/templates

可以后面加一个v,让输出内容表格显示表头; pretty则让输出缩进更规范

集群状态

  • 集群状态
代码语言:javascript
复制
curl -X GET "localhost:9200/_cluster/health?pretty"

节点状态

  • 节点简要信息
代码语言:javascript
复制
curl -X GET "localhost:9200/_cat/nodes?pretty&v"
ip             heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.58.101           69          99  71   12.67   12.25    11.71 mdi       -      node-101
192.168.58.103           23          99  70   14.64   13.45    12.68 mdi       -      node-103
192.168.58.105           60          97  69   11.17   10.96    10.88 mdi       *      node-105
  • 节点详细信息
代码语言:javascript
复制
curl -X GET "localhost:9200/_nodes/stats/http?pretty"

后面的http是查看的属性,另外还有indices, fs, http, jvm, os, process, thread_pool, discovery等,支持组合(如indices,fs,http

分片状态

  • 分片
代码语言:javascript
复制
curl -X GET "localhost:9200/_cat/shards?v&pretty"
index                           shard prirep state    docs store ip          node
tenmao_index_153915944934 1     p      STARTED 39931 4.1mb 172.17.0.14 35S66p1
tenmao_index_153915944934 1     r      STARTED 39931   4mb 172.17.0.3  DPKsmMN
tenmao_index_153915944934 0     p      STARTED 39634   4mb 172.17.0.2  PE8QHxz
tenmao_index_153915944934 0     r      STARTED 39634   4mb 172.17.0.3  DPKsmMN

分片中如果存在未分配的分片, 可以查看未分片的原因:_cat/shards?h=index,shard,prirep,state,unassigned.reason&v

索引

索引管理

  • 索引列表
代码语言:javascript
复制
curl -X GET "localhost:9200/_cat/indices?v"
health status index                           uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   tenmao_index_153915944934 Z6BV1VaMRc-tC-7IucJE2w   5   1     198444            0     40.9mb         20.4mb

条件过滤:_cat/indices?v&health=yellow

排序:_cat/indices?v&health=yellow&s=docs.count:desc

  • 索引详细信息
代码语言:javascript
复制
curl -X GET "localhost:9200/chat_index_alias/_stats?pretty"
  • 数据量
代码语言:javascript
复制
curl -X GET "localhost:9200/_cat/count/chat_index_alias?v&pretty"
  • 新建索引
代码语言:javascript
复制
curl -X PUT "localhost:9200/my_index" -d '
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3, 
            "number_of_replicas" : 2 
        }
    }
}'
  • 删除索引
代码语言:javascript
复制
curl -X DELETE "localhost:9200/tenmao_index"

curl -X DELETE "localhost:9200/tenmao_index_1504520299944"

索引使用

  • 分词搜索
代码语言:javascript
复制
curl -X POST "localhost:9200/chat_index_alias/_search" -d '
{
  "query": {
    "match": {
      "question": "吃饭了吗"
    }
  }
}'
  • 完全匹配搜索
代码语言:javascript
复制
curl -X POST "localhost:9200/chat_index_alias/_search" -d '
{
  "query": {
    "match_phrase": {
      "question": "你吃饭了"
    }
  }
}'

别名

  • 查看别名
代码语言:javascript
复制
curl -X GET "localhost:9200/_alias/chat_index_alias?pretty"
  • 增加别名
代码语言:javascript
复制
curl -X PUT "localhost:9200/my_index/_alias/my_index_alias?pretty"
  • 删除别名
代码语言:javascript
复制
curl -X POST 'http://localhost:9200/_aliases' -d '
{
    "actions": [
        {"remove": {"index": "my_index", "alias": "my_index_alias"}}
    ]
}'

一般纯删除别名使用的比较少,一般是别名重新绑定(删除和绑定为一个原子操作)

  • 别名重新绑定
代码语言:javascript
复制
curl -XPOST 'http://localhost:9200/_aliases' -d '
{
    "actions" : [
        { "remove" : { "index" : "my_index", "alias" : "my_index_alias" } },
        { "add" : { "index" : "my_index_v2", "alias" : "my_index_alias" } }
    ]
}'

参考

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.01.21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 状态查询
    • 集群状态
      • 节点状态
        • 分片状态
        • 索引
          • 索引管理
            • 索引使用
            • 别名
            • 参考
            相关产品与服务
            Elasticsearch Service
            腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档