更新Elasticsearch文档中的字段

时间:2019-10-31 13:47:06

标签: elasticsearch

我需要更新索引到Elasticsearch的文档中的字段。我该怎么做。

"redcash_sale": {
"type": "object"
}

将上方字段更新为下方(将enable设为false):-

sale_property_development_j/_mapping/property

{
  "properties": {
    "redcash_sale": {
      "type": "object",
      "enabled": false
    }
  }
}

再次映射到elasticsearch时引发错误:-

错误

{
"error": {
"root_cause": [
{
"type": "mapper_exception",
"reason": "Can't update attribute for type [_doc.redcash_sale.enabled] in index mapping"
}
],
"type": "mapper_exception",
"reason": "Can't update attribute for type [_doc.redcash_sale.enabled] in index mapping"
},
"status": 500
}

提前谢谢!

2 个答案:

答案 0 :(得分:3)

您可以做的是将数据 _reindex 转换为目标索引,删除原始索引,然后使用新映射再次将 _reindex 还原为原始索引。

重新编制索引:

POST _reindex   
{
  "source": {
    "index": "sale_property_development_j"
  },
  "dest": {
    "index": "new_sale_property_development_j"
  }
}

删除原始索引:

DELETE sale_property_development_j

创建请求的映射:

PUT sale_property_development_j
{
   "mappings":{
     "property":{
       "properties": {
         "redcash_sale": {
           "type": "object",
           "enabled": false
          }
       }
     }
  }
}

重新索引:

POST _reindex?wait_for_completion=false    
{
  "source": {
    "index": "new_sale_property_development_j"
  },
  "dest": {
    "index": "sale_property_development_j"
  }
}

最后:

DELETE new_sale_property_development_j

有解决方案真好

答案 1 :(得分:1)

根据:https://www.elastic.co/guide/en/elasticsearch/reference/current/enabled.html

已启用,无法使用PUT映射API更新。

然后您必须重新索引数据。