ES 常用命令

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

参考资料

  1. https://wenchao.ren/archives/371

  2. http://www.tuicool.com/articl...

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

相关推荐