分享

ES 常用命令

 好闺女瑶瑶 2021-11-12
  • ES 常用命令查看版本

    curl -XGET 'localhost:9200'
    {
      "name" : "5koA13t",
      "cluster_name" : "elasticsearch",
      "cluster_uuid" : "kQhdXKz4S_6iWNZy7NITuw",
      "version" : {
        "number" : "5.4.2",
        "build_hash" : "929b078",
        "build_date" : "2017-06-15T02:29:28.122Z",
        "build_snapshot" : false,
        "lucene_version" : "6.5.1"
      },
      "tagline" : "You Know, for Search"
    }

    查看索引状态

    curl -XGET 'localhost:9200/_cat/indices?v&pretty'

    查看mapping

    curl -XGET 'localhost:9200/logstash-event-login/_mapping/?pretty'

    删除index

    curl -XDELETE 'localhost:9200/logstash-event-login?pretty'

    备份和恢复使用 es 提供的 snapshot 功能

    首先需要编辑config/elasticsearch.yml文件增加备份存储库的位置。比如path.repo: /tmp

    建立repo

    curl -XPUT 'http://localhost:9200/_snapshot/my_repository' -d '
    {
        "type": "fs",
        "settings": {
            "location": "/tmp/my_repository",
            "compress": true
        }
    }'

    返回

    {"acknowledged":true}

    创建 snapshot

    curl -XPUT "http://localhost:9200/_snapshot/my_repository/snap_1?wait_for_completion=true" -d '
    {
        "indices": "logstash-event-login,logstash-event-view",
        "ignore_unavailable": "true",
        "include_global_state": false
    }'

    wait_for_completion参数表示会等到snapshot完成才返回,不加这个参数也可以通过其他接口获取到快照的进度

    curl -XGET "http://localhost:9200/_snapshot/my_repository/snap_1/_status?pretty"
    查看 snapshot
    curl -XGET 'http://localhost:9200/_snapshot/my_repository/snap_1?pretty'

    返回结果

    {
      "snapshots" : [
        {
          "snapshot" : "snap_1",
          "uuid" : "1PXR1UNeSCK_BlfK_ZMyTg",
          "version_id" : 5040299,
          "version" : "5.4.2",
          "indices" : [
            "logstash-event-login",
            "logstash-event-view"
          ],
          "state" : "SUCCESS",
          "start_time" : "2017-07-11T08:18:26.567Z",
          "start_time_in_millis" : 1499761106567,
          "end_time" : "2017-07-11T08:18:28.660Z",
          "end_time_in_millis" : 1499761108660,
          "duration_in_millis" : 2093,
          "failures" : [ ],
          "shards" : {
            "total" : 10,
            "failed" : 0,
            "successful" : 10
          }
        }
      ]
    }
    删除快照
    curl -XDELETE 'http://localhost:9200/_snapshot/my_repository/snap_1'

    查看所有快照

    curl -XGET 'http://localhost:9200/_snapshot/my_repository/_all?pretty'

    恢复 snapshot

    curl -XPOST "http://localhost:9200/_snapshot/my_repository/snap_1/_restore?wait_for_completion=true&pretty" -d '
    {
        "indices": "logstash-event-login",
        "ignore_unavailable": "true",
        "include_global_state": false,
        "rename_pattern": "logstash-event-login",
        "rename_replacement": "restore_logstash-event-login"
    }'

    参数 rename_patternrename_replacement 用来正则匹配要恢复的索引,并且重命名。下面的例子:test-index => copy_index, test_2 => coyp_2

    curl -XPOST "http://localhost:9200/_snapshot/my_repository/snap_1/_restore?wait_for_completion=true&pretty" -d '
    {
        "indices": "test-index,test-2",
        "ignore_unavailable": "true",
        "include_global_state": false,
        "rename_pattern": "test-(.+)",
        "rename_replacement": "copy_$1"
    }'

    使用 elasticdump

    安装

    npm install elasticdump -g

    elasticdump 的 input 和 output 都可以指定为 elasticsearch 和文件

    # 导出数据
    elasticdump --input=http://localhost:9200/logstash-event-login --output=data.json --type=data
    # 导出mapping
    elasticdump --input=http://localhost:9200/logstash-event-login --output=mapping.json --type=mapping
    
    
    # 导入mapping
    elasticdump --input=mapping.json --output=http://localhost:9200/elasticdump-event-login --type=mapping
    # 导入数据
    elasticdump --input=data.json --output=http://localhost:9200/elasticdump-event-login --type=data

    参考资料

    https:///archives/371

    http://www./articl...

    https://segmentfault.com/a/11...

    https://github.com/taskrabbit...

    文章来源:segmentfault,作者:openmartin。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:sean.lii#(邮箱中#请改为@)进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多