重建索引

     重建索引步骤:

1、新增⼀个索引blog_lastest,Mapping数据结构与blog索引一致
2、将blog数据同步至blog_lastest
3、删除blog索引
4、数据同步后给blog_lastest添加别名blog

新建索引

PUT /blog_lastest
{
  "mappings": {
    "properties": {
      "title":{
        "type":"text",
        "analyzer": "ik_max_word"
      },
      "author":{
        "type":"keyword",
        "fields": {
          "seg":{
            "type":"text",
            "analyzer":"ik_max_word"
          }
        }
      }
    }
  }
}

索引数据同步到新索引

     reindex同步操作

POST /_reindex
{
  "source":{
    "index": "blog"
  },
  "dest": {
    "index": "blog_lastest"
  }
}

异步执⾏:reindex 时间过⻓,建议加上 wait_for_completion=false 的参数条件,这样 reindex 将直接返回 taskId

POST /_reindex?wait_for_completion=false
{
  "source":{
    "index": "blog"
  },
  "dest": {
    "index": "blog_lastest"
  }
}

taskId可以实时查看任务的执行状态

GET /_tasks/{taskId}
{
  "completed" : true,
  "task" : {
    "node" : "-VlhDd0nS0e0qH4Yq3hq0Q",
    "id" : 408017,
    "type" : "transport",
    "action" : "indices:data/write/reindex",
    "status" : {
      "total" : 1,
      "updated" : 0,
      "created" : 1,
      "deleted" : 0,
      "batches" : 1,
      "version_conflicts" : 0,
      "noops" : 0,
      "retries" : {
        "bulk" : 0,
        "search" : 0
      },
      "throttled_millis" : 0,
      "requests_per_second" : -1.0,
      "throttled_until_millis" : 0
    },
    "description" : "reindex from [blog] to [blog_lastest][_doc]",
    "start_time_in_millis" : 1670029957182,
    "running_time_in_nanos" : 1042895072,
    "cancellable" : true,
    "cancelled" : false,
    "headers" : { }
  },
  "response" : {
    "took" : 1033,
    "timed_out" : false,
    "total" : 1,
    "updated" : 0,
    "created" : 1,
    "deleted" : 0,
    "batches" : 1,
    "version_conflicts" : 0,
    "noops" : 0,
    "retries" : {
      "bulk" : 0,
      "search" : 0
    },
    "throttled" : "0s",
    "throttled_millis" : 0,
    "requests_per_second" : -1.0,
    "throttled_until" : "0s",
    "throttled_until_millis" : 0,
    "failures" : [ ]
  }
}

  cancellable 可以判断任务是否支持取消操作。

op_type 参数

       op_type 参数控制着写入数据的冲突处理方式,如果把 op_type 设置为 create【默认值】,在 _reindex API 中,写入时只在 dest index中添加不存在的 doucment,如果相同的 document 已经存在,则会报 version confilct 的错误,那么索引操作就会失败。【这种方式与使用 _create API 时效果一致】

POST _reindex
{
  "source": {
    "index": "blog"
  },
  "dest": {
    "index": "blog_lastest",
    "op_type": "create"
  }
}

  可以把 op_type 设置为 index,表示所有的数据全部重新索引创建。

conflicts 配置

  当发生 version conflict 的时候,_reindex 会被 abort,任务终止【此时数据还没有 reindex 完成】,在返回体中的 failures 指标中会包含冲突的数据【有时候数据会非常多】,除非把 conflicts 设置为 proceed

POST _reindex
{
  "source": {
    "index": "blog"
  },
  "dest": {
    "index": "blog_lastest",
    "op_type": "create"
  },
  "conflicts": "proceed"
}

删除旧索引

DELETE /blog

使用别名

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "blog_lastest",
        "alias": "blog"
      }
    }
  ]
}

别名访问新索引

POST /blog/_search
{
  "query":{
    "match": {
      "author": "长江大浪滔天"
    }
  }
}

 

  

posted on 2022-12-03 09:22  溪水静幽  阅读(131)  评论(0)    收藏  举报