ElasticSearch 7.6.2 索引填加字段并设置默认值

因为 es 索引结构特性当我们对现有索引新增字段时并不会影响历史数据,并且如果没有写入这个字段值时,也不会有默认值

所以有时我们需要对历史数据设置默认值

1. 使用 put 方法加字段

PUT my_index/_mapping
{
  "properties": {
    "字段":{
      "type": "类型"
    }
  }
}

2. 设置默认值使用 post

POST my_index/_update_by_query
{
  "script": {
    "lang": "painless",
    "source": "if (ctx._source.字段== null) {ctx._source.字段= '0'}"
  }
}

如果数据量较大,会出现 timeout 情况,这时,可以通过设置超时时间进行处理,或者提前使用query语句进行过滤。如下:

POST organization_community_index/_update_by_query
{
  "script": {
    "lang": "painless",
    "source": "if (ctx._source.字段== null) {ctx._source.字段= '0'}"
  },
  "query":{
     "match": {
      "字段": "特殊字段"
    }
  },"timeout": "10s"
}

如果数据量大到,在设置超时时间后依旧会超时,则可以使用后台执行,在查询路径后面添加参数?wait_for_completion=false

POST organization_community_index/_update_by_query?wait_for_completion=false
{
  "script": {
    "lang": "painless",
    "source": "if (ctx._source.字段== null) {ctx._source.字段= '0'}"
  },
  "query":{
     "match": {
      "字段": "特殊字段"
    }
  }
}

他会返回一个后台任务 id

{
  "task" : "Hzl9nI4MS2mFkrTirsWDeg:9124874"
}

通过task 接口查询任务完成情况

GET _tasks/Hzl9nI4MS2mFkrTirsWDeg:9124874

处理中的查询结果如下:

{
  "completed" : false,           // 这里会显示任务是否完成,当前状态未完成
  "task" : {
    "node" : "Hzl9nI4MS2mFkrTirsWDeg",
    "id" : 9129756,
    "type" : "transport",
    "action" : "indices:data/write/update/byquery",
    "status" : {
      "total" : 457828,           // 这里标记需要处理记录的总数
      "updated" : 25000,          // 这里显示已经更新的数量
      "created" : 0,
      "deleted" : 0,
      "batches" : 26,
      "version_conflicts" : 0,
      "noops" : 0,
      "retries" : {
        "bulk" : 0,
        "search" : 0
      },
      "throttled_millis" : 0,
      "requests_per_second" : -1.0,
      "throttled_until_millis" : 0
    },
    "description" : "update-by-query [organization_community_index] updated with Script{type=inline, lang='painless', idOrCode='if (ctx._source.type== 'court') {ctx._source.court=ctx._source.court}', options={}, params={}}",
    "start_time_in_millis" : 1623290578163,    // 开始时间  时间戳
    "running_time_in_nanos" : 5749019391,      // 已经运行时间 纳秒
    "cancellable" : true,    // 当前任务可以取消,但是因为es 没有事务,就算取消,已经更新的数据不会回退
    "headers" : { }
  }
}

处理完成结果如下,注意如果有错误的话,会有相应的 error 字段对错误进行描述

{
  "completed" : true,
  "task" : {
    "node" : "Hzl9nI4MS2mFkrTirsWDeg",
    "id" : 9129756,
    "type" : "transport",
    "action" : "indices:data/write/update/byquery",
    "status" : {
      "total" : 457828,
      "updated" : 457828,
      "created" : 0,
      "deleted" : 0,
      "batches" : 458,
      "version_conflicts" : 0,
      "noops" : 0,
      "retries" : {
        "bulk" : 0,
        "search" : 0
      },
      "throttled_millis" : 0,
      "requests_per_second" : -1.0,
      "throttled_until_millis" : 0
    },
    "description" : "update-by-query [organization_community_index] updated with Script{type=inline, lang='painless', idOrCode='if (ctx._source.type== 'court') {ctx._source.court=ctx._source.court}', options={}, params={}}",
    "start_time_in_millis" : 1623290578163,
    "running_time_in_nanos" : 91446334635,
    "cancellable" : true,
    "headers" : { }
  },
  "response" : {
    "took" : 91446,
    "timed_out" : false,
    "total" : 457828,
    "updated" : 457828,
    "created" : 0,
    "deleted" : 0,
    "batches" : 458,
    "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" : [ ]
  }
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐