前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Elasticsearch Service想要删除索引中某一个字段数据如何操作(最佳实践)

Elasticsearch Service想要删除索引中某一个字段数据如何操作(最佳实践)

原创
作者头像
zjiekou
发布2023-09-12 17:55:14
1.1K0
发布2023-09-12 17:55:14
举报
文章被收录于专栏:大数据zjiekou大数据zjiekou

一、需求背景描述

某客户的需求删除一个index 里的一个长文字段 :web_content

二、制定方案

方案1:新建一个索引,索引mapping中去掉web_content字段,通过reindex api将老索引数据同步到新索引

1、客户创建索引请求如下,mapping没有web_content字段:

代码语言:javascript
复制
PUT /index_bookmark_v2

{

"settings": {

"number_of_replicas": 0,

"number_of_shards": 15

},

"mappings": {

"properties" : {

"archive_name" : {

"type" : "keyword"

},

"archiving" : {

"type" : "long"

},

"article_name" : {

"type" : "keyword"

},

"article_score" : {

"type" : "float"

},

"article_site_type" : {

"type" : "long"

},

"article_status" : {

"type" : "long"

},

"content" : {

"type" : "text",

"analyzer" : "ik_max_word"

},

"cover" : {

"type" : "keyword"

},

"create_time" : {

"type" : "long"

},

"description" : {

"type" : "keyword",

"normalizer" : "lowercase"

},

"has_mark" : {

"type" : "long"

},

"id" : {

"type" : "keyword"

},

"is_read" : {

"type" : "long"

},

"sourceType" : {

"type" : "long"

},

"star_target" : {

"type" : "long"

},

"state" : {

"type" : "long"

},

"tags" : {

"type" : "keyword",

"normalizer" : "lowercase"

},

"title" : {

"type" : "keyword",

"normalizer" : "lowercase"

},

"type" : {

"type" : "long"

},

"update_time" : {

"type" : "long"

},

"url" : {

"type" : "keyword",

"normalizer" : "lowercase"

},

"user_id" : {

"type" : "keyword"

},

"wechat_msgtime" : {

"type" : "long"

}

}

}

}

2、通过reindex同步数据到新索引,出现报错

原因:默认情况下reindex会将源索引的所有数据及mapping结构这些都会同步到目标索引,上面的报错就在reindex的时候给目标索引自动创建了一个keyword类型的web_content字段。

结论:那第一种方案就无法采用了。

方案二、使用_reindexAPI 将数据从old_index复制到new_index,并在过程中删除web_content字段。可以使用sourcedest参数指定源索引和目标索引,然后使用script参数删除字段

实践操作步骤:

1、创建一个新的索引,例如 new_index。你可以使用 Elasticsearch 的 PUT API 创建新索引。确保新索引的映射不包含 web_content 字段。创建索引请求上文已提供。

2、使用_reindexAPI 将数据从old_inde复制到new_index,并在过程中删除web_content字段。可以使用source和dest参数指定源索引和目标索引,然后使用script参数删除字段

代码语言:javascript
复制
curl -X POST "localhost:9200/_reindex" -H 'Content-Type: application/json' -d'
{
  "source": {
    "index": "old_index"
  },
  "dest": {
    "index": "new_index"
  },
  "script": {
    "lang": "painless",
    "source": "ctx._source.remove(\"web_content \")"
  }
}'

3、会议引导用户操作后数据成功同步到新索引,web_content 字段也删掉了

注意事项:在执行reindex的时候可能会出现报错504 gateway timeout报错,这个报错没关系。实际reindex任务是在后台异步执行的。也可以在reindex请求后加wait_for_completion=false参数,执行会返回一个taskid,通过GET _tasks/<task_id>命令即可看到任务执行进度。

reindex参考文档:https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-reindex.html

Painless script参考文档:https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-scripting.html

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、需求背景描述
    • 某客户的需求删除一个index 里的一个长文字段 :web_content
    • 二、制定方案
      • 方案1:新建一个索引,索引mapping中去掉web_content字段,通过reindex api将老索引数据同步到新索引
        • 原因:默认情况下reindex会将源索引的所有数据及mapping结构这些都会同步到目标索引,上面的报错就在reindex的时候给目标索引自动创建了一个keyword类型的web_content字段。
        • 结论:那第一种方案就无法采用了。
      • 方案二、使用_reindexAPI 将数据从old_index复制到new_index,并在过程中删除web_content字段。可以使用source和dest参数指定源索引和目标索引,然后使用script参数删除字段
        • 实践操作步骤:
        • 注意事项:在执行reindex的时候可能会出现报错504 gateway timeout报错,这个报错没关系。实际reindex任务是在后台异步执行的。也可以在reindex请求后加wait_for_completion=false参数,执行会返回一个taskid,通过GET _tasks/<task_id>命令即可看到任务执行进度。
    相关产品与服务
    Elasticsearch Service
    腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档