Elasticsearch之_reindex

这篇具有很好参考价值的文章主要介绍了Elasticsearch之_reindex。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

_reindex可是个好东西,尤其是针对开发者而言,从小的方面讲在存储数据是我们常常可能由于字段类型的问题,值大小写的问题,分词器的问题导致查询不到,或者结构不对,或者分片数,副本数不对等这类问题,从大的方面讲,跨集群数据迁移的时候,你就需要用到关键指令 _reindex ,换句话说,数据库大家都用过吧,总有的时候需要调整表结构,或者值大小写等等这种恶心的情况,笨一点,新建一张正确的临时表,写个脚本,把数据从错误的表读取出来,通过程序处理数据符合预期后,在插入到新表,然后在删除旧表,在创建一个和旧表相同的表名,在把临时表数据导入到旧表中。这一系列操作下来,整个人都麻了。当然思路是这个思路,但是实现过程我们在elasticsearch中不需要写脚本,而是直接使用指令 _reindex 即可完成,废话不多少,懂的人自然懂。

注意事项

  • 源和目的不能相同, 比如不能将数据流reindex给它自身
  • 源索引的文档中 _source 字段必须开启(默认为开启)
  • reindex不会复制源的setting和源所匹配的模板,因此在调用_reindex前,你需要设置好目标索引的mapping,(action.auto_create_index 为 false 或者 -.* 时)
  • 目标索引的mapping,主分片数,副本数等推荐提前配置

如果配置了安全策略和权限策略

  • 如果elasticsearch集群配置了安全策略和权限策略, 则进行reindex必须拥有以下权限

    • 如果reindex的源为远程集群,必须在当前集群的请求节点 elasticsearch.yml文件配置远程白名单reindex.remote.whitelist
    • 读取源的数据流、 索引、 索引别名等索引级别权限。
    • 对于目的数据流、 索引、 索引别名的写权限。
  • 最简单的使用方式文章来源地址https://www.toymoban.com/news/detail-771652.html

curl --location 'http://localhost:9200/_reindex' \
--header 'Content-Type: application/json' \
--data '{
    "source": {
        "index": "旧索引"
    },
    "dest": {
        "index": "新索引"
    }
}'
  • 指定size控制复制的条数,不指定则为全部
curl --location 'http://localhost:9200/_reindex' \
--header 'Content-Type: application/json' \
--data '{
    "size": 100,
    "source": {
        "index": "source_index"
    },
    "dest": {
        "index": "dest_index"
    }
}'
  • 将多个索引reindex到一个目标
curl --location 'http://localhost:9200/_reindex' \
--header 'Content-Type: application/json' \
--data '{
    "source": {
        "index": [
            "source_index_1",
            "source_index_2"
        ],
        "type": [
            "source_type_1",
            "source_type_2"
        ]
    },
    "dest": {
        "index": "dest_index"
    }
}'
  • 只复制特定的字段
curl --location 'http://localhost:9200/_reindex' \
--header 'Content-Type: application/json' \
--data '{
    "source": {
        "index": "source_index_1",
        "_source": [
            "username",
            "sex"
        ]
    },
    "dest": {
        "index": "dest_index"
    }
}'
  • 使用script(例:_id的值需要大写)
curl --location 'http://192.168.5.235:9210/_reindex' \
--header 'Content-Type: application/json' \
--data '{
    "script": {
        "source": "String uppercaseId = ctx._id.toUpperCase(); ctx._source.remove(\"id\"); ctx._id = uppercaseId;  ",
        "lang": "painless"
    },
    "source": {
        "index": "source_index"
    },
    "dest": {
        "index": "dest_index"
    }
}'

# 如果是_source中的值需要:String uppercaseUuid = ctx._source.ENTITY_UUID.toUpperCase(); ctx._source.remove(\"_source.ENTITY_UUID\"); ctx._source.ENTITY_UUID = uppercaseUuid;
  • 跨集群使用remote属性(涵query match和sort)
# 跨集群传输时,如果单个document的平均大小超过100Kb,则有可能会报错,需要在source中指定size,定义每批次传输的doc个数
curl --location 'http://localhost:9200/_reindex' \
--header 'Content-Type: application/json' \
--data '{
    "source": {
        // "sort": {
            // "date": "desc"
        // },
        // "query": {
        //     "match": {
        //         "test": "data"
        //     }
        // },
        // "size": 100,
        "remote": {
            "host": "http://otherhost:9200",
            "username": "username",
            "password": "password"
        },
        "index": "source_index"
    },
    "dest": {
        "index": "dest_index"
    }
}'
  • 如果目标index中有数据,并且可能发生冲突
# version_type为internal则Elasticsearch强制性的将文档转储到目标中,覆盖具有相同类型和ID的任何内容
# version_type为external则做更新
curl --location 'http://localhost:9200/_reindex' \
--header 'Content-Type: application/json' \
--data '{
    "source": {
        "index": "source_index"
    },
    "dest": {
        "index": "dest_index",
        "version_type": "internal"
    }
}'
  • op_type为create
# 只在dest index中添加不不存在的doucments。如果相同的documents已经存在,则会报version confilct的错误。
curl --location 'http://localhost:9200/_reindex' \
--header 'Content-Type: application/json' \
--data '{
    "source": {
        "index": "source_index"
    },
    "dest": {
        "index": "dest_index",
        "op_type": "create"
    }
}'
  • 由于op_type为create引发的version confilct
curl --location 'http://localhost:9200/_reindex' \
--header 'Content-Type: application/json' \
--data '{
    "conflicts": "proceed",
    "source": {
        "index": "source_index"
    },
    "dest": {
        "index": "dest_index",
        "op_type": "create"
    }
}'
  • 查看reindex进度
curl --location --request POST 'http://localhost:9200/_tasks?detailed=true&actions=*reindex'

问题发现

  • reindex的核心做跨索引、跨集群的数据迁移,慢的原因及优化思路无非包括:
    1)批量大小值可能太小。需要结合堆内存、线程池调整大小;
    2)reindex的底层是scroll实现,借助scroll并行优化方式,提升效率;
    3)跨索引、跨集群的核心是写入数据,考虑写入优化角度提升效率。
    • 提升批量写入大小值:在source中指定 size 的值改变每个批次的大小
    • sliced并行,每个Scroll请求,可以分成多个Slice请求,可以理解为切片,各Slice独立并行,利用Scroll重建或者遍历要快很多倍,自动设置分片如下:
      • 1)slices大小的设置可以手动指定,或者设置slices设置为auto,auto的含义是:针对单索引,slices大小=分片数;针对多索引,slices=分片的最小值
      • 2)当slices的数量等于索引中的分片数量时,查询性能最高效。slices大小大于分片数,非但不会提升效率,反而会增加开销。
      • 3)如果这个slices数字很大(例如500),建议选择一个较低的数字,因为过大的slices会影响性能。
curl --location 'http://localhost:9200/_reindex?slices=5&refresh=null' \
--header 'Content-Type: application/json' \
--data '{
    "source": {
        "index": "source_index"
    },
    "dest": {
        "index": "dest_index"
    }
}'

到了这里,关于Elasticsearch之_reindex的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • ElasticSearch系列-索引原理与数据读写流程

    倒排索引(Inverted Index) 也叫反向索引,有反向索引必有正向索引。通俗地来讲, 正向索引是通过key找value,反向索引则是通过value找key。ES底层在检索时底层使用的就是倒排索引。 现有索引和映射如下: 先录入如下数据,有三个字段title、price、description等 _id title price descri

    2024年02月08日
    浏览(39)
  • Grafana系列-统一展示-7-ElasticSearch数据源

    Grafana 系列文章 Grafana内置了对Elasticsearch的支持。你可以进行多种类型的查询,以可视化存储在Elasticsearch中的日志或指标,并使用存储在Elasticsearch中的日志事件对图表进行注释。 关键的几项配置如下: URL: 设置你的Elasticsearch服务器的HTTP协议、IP和端口。如: http://192.168.2.1:92

    2024年02月04日
    浏览(33)
  • Elasticsearch 系列(六)- ES数据同步和ES集群

    本章将和大家分享ES的数据同步方案和ES集群相关知识。废话不多说,下面我们直接进入主题。 1、数据同步问题 Elasticsearch中的酒店数据来自于mysql数据库,因此mysql数据发生改变时,Elasticsearch也必须跟着改变,这个就是Elasticsearch与mysql之间的数据同步。 在微服务中,负责酒

    2024年04月28日
    浏览(71)
  • Elasticsearch系列组件:Logstash强大的日志管理和数据分析工具

    Elasticsearch 是一个开源的、基于 Lucene 的分布式搜索和分析引擎,设计用于云计算环境中,能够实现实时的、可扩展的搜索、分析和探索全文和结构化数据。它具有高度的可扩展性,可以在短时间内搜索和分析大量数据。 Elasticsearch 不仅仅是一个全文搜索引擎,它还提供了分布

    2024年02月08日
    浏览(31)
  • Elasticsearch系列组件:Kibana无缝集成的数据可视化和探索平台

    Elasticsearch 是一个开源的、基于 Lucene 的分布式搜索和分析引擎,设计用于云计算环境中,能够实现实时的、可扩展的搜索、分析和探索全文和结构化数据。它具有高度的可扩展性,可以在短时间内搜索和分析大量数据。 Elasticsearch 不仅仅是一个全文搜索引擎,它还提供了分布

    2024年02月08日
    浏览(43)
  • ElasticSearch系列 - SpringBoot整合ES:映射中定义字段的数据类型及属性

    ElasticSearch - SpringBoot整合ES:映射定义字段的数据类型及属性 01. ElasticSearch 搜索结果的准确性和召回率是什么? 在Elasticsearch中,搜索结果的准确性和召回率是非常重要的指标,它们反映了搜索引擎的性能和效果。以下是这两个指标的定义和解释: 准确性:搜索结果的准确性

    2024年02月08日
    浏览(37)
  • Debezium系列之:基于debezium将mysql数据库数据更改流式传输到 Elasticsearch和PostgreSQL数据库

    基于 Debezium 的端到端数据流用例,将数据流式传输到 Elasticsearch 服务器,以利用其出色的功能对我们的数据进行全文搜索。 同时把数据流式传输到 PostgreSQL 数据库,通过 SQL 查询语言来优化对数据的访问。 下面的图表显示了数据如何流经我们的分布式系统。首先,Debezium M

    2024年02月13日
    浏览(45)
  • 【天衍系列 04】深入理解Flink的ElasticsearchSink组件:实时数据流如何无缝地流向Elasticsearch

    Flink的Elasticsearch Sink是用于将Flink数据流(DataStream)中的数据发送到Elasticsearch的组件。它是Flink的一个连接器(Connector),用于实现将实时处理的结果或数据持续地写入Elasticsearch集群中的索引中。 下面是一些关于Flink的Elasticsearch Sink的基础概念: 数据源(Source) :Flink数据流

    2024年02月20日
    浏览(40)
  • Java SpringBoot API 实现ES(Elasticsearch)搜索引擎的一系列操作(超详细)(模拟数据库操作)

    小编使用的是elasticsearch-7.3.2 基础说明: 启动:进入elasticsearch-7.3.2/bin目录,双击elasticsearch.bat进行启动,当出现一下界面说明,启动成功。也可以访问http://localhost:9200/ 启动ES管理:进入elasticsearch-head-master文件夹,然后进入cmd命令界面,输入npm run start 即可启动。访问http

    2024年02月04日
    浏览(44)
  • 【ElasticSearch系列-05】SpringBoot整合elasticSearch

    ElasticSearch系列整体栏目 内容 链接地址 【一】ElasticSearch下载和安装 https://zhenghuisheng.blog.csdn.net/article/details/129260827 【二】ElasticSearch概念和基本操作 https://blog.csdn.net/zhenghuishengq/article/details/134121631 【三】ElasticSearch的高级查询Query DSL https://blog.csdn.net/zhenghuishengq/article/details/1

    2024年02月06日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包