es 常用操作 备份 恢复 重建索引

    科技2024-05-30  80

    es 常用操作 备份 恢复 重建索引

    es备份操作 操作步骤

    配置elasticsearch.yml,增加或者修改

    /home/es/elasticsearch-7.8.0/backup位置自定义

    path.repo: ["/home/es/elasticsearch-7.8.0/backup"]

    创建仓库

    curl -X PUT "127.0.0.1:9200/_snapshot/es_doc_test" -H 'Content-Type: application/json' -d'{ "type": "fs", "settings": { "location": "/home/es/elasticsearch-7.8.0/backup/es_doc_test_backup_0711" } }'

    查看仓库

    curl -X GET "127.0.0.1:9200/_snapshot"

    创建快照 snapshot_0711_1 自定义

    curl -X PUT '127.0.0.1:9200/_snapshot/es_doc_test/snapshot_0711_1?wait_for_completion=true' -H 'Content-Type: application/json' -d' { "indices": "es_doc_test", "ignore_unavailable": true, "include_global_state": false }'

    查看快照

    curl -X GET "127.0.0.1:9200/_snapshot/es_doc_test/snapshot_0711_1"

    es恢复操作 操作步骤

    关闭索引

    curl -X POST "127.0.0.1:9200/es_doc_test/_close"

    同步操作

    curl -X POST "127.0.0.1:9200/_snapshot/es_doc_test/snapshot_0711_1/_restore?wait_for_completion=true"

    开启索引

    curl -X POST "127.0.0.1:9200/es_doc_test/_open"

    es索引迁移 操作步骤

    创建一个新的索引

    es_doc_test0711可以更改,以下步骤替换es_doc_test0711为更改的

    curl --location --request PUT 'http://127.0.0.1:9200/es_doc_test0711' \ --header 'Content-Type: application/json' \ --data-raw '{ "mappings": { "properties": { "areaId": { "type": "long" }, "areaNames": { "type": "text", "analyzer": "ik_smart" }, "formId": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "id": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } }, "settings": { "index": { "refresh_interval": "1s", "number_of_shards": "5", "store": { "type": "fs" }, "number_of_replicas": "1" } } }'

    同步数据

    curl --location --request POST 'http://127.0.0.1:9200/_reindex' \ --header 'Content-Type: application/json' \ --data-raw '{ "source": { "index": "es_doc_test" }, "dest": { "index": "es_doc_test0711" } }'

    删除旧索引

    curl --location --request DELETE 'http://127.0.0.1:9200/es_doc_test'

    重复执行第1到第3步,更换es_doc_test0711和es_doc_test的位置 (这一步骤可以省略,使用alias也可以解决问题)

    新增es_doc_test索引

    curl --location --request PUT 'http://127.0.0.1:9200/es_doc_test' \ --header 'Content-Type: application/json' \ --data-raw '{ "mappings": { "properties": { "areaId": { "type": "long" }, "areaNames": { "type": "text", "analyzer": "ik_smart" }, "formId": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "id": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "updateUserName": { "type": "text", "analyzer": "ik_smart" } } }, "settings": { "index": { "refresh_interval": "1s", "number_of_shards": "5", "store": { "type": "fs" }, "number_of_replicas": "1" } } }'

    重建索引

    curl --location --request POST 'http://127.0.0.1:9200/_reindex' \ --header 'Content-Type: application/json' \ --data-raw '{ "source": { "index": "es_doc_test0711" }, "dest": { "index": "es_doc_test" } }'

    删除旧索引

    curl --location --request DELETE 'http://127.0.0.1:9200/es_doc_test0711'
    Processed: 0.015, SQL: 8