`
tcxiang
  • 浏览: 89464 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

ElasticSearch 索引热切换(转)

 
阅读更多

Elasticsearch 的坑爹事 

本文记录一次Elasticsearch mapping field修改过程


团队使用Elasticsearch做日志的分类检索分析服务,使用了类似如下的_mapping

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
    "settings" : {
        "number_of_shards" : 20
    },
    "mappings" : {
      "client" : {
        "properties" : {
          "ip" : {
            "type" "long"
          },
          "cost" : {
            "type" "long"
          },
}

 
现在问题来了,日志中输出的"127.0.0.1"这类的IP地址在Elasticsearch中是不能转化为long的(报错Java.lang.NumberFormatException),所以我们必须将字段改为string型或者ip型(Elasticsearch支持, 数据类型可见mapping-core-types)才能达到理想的效果.

目标明确了,就是改掉mapping的ip的field type即可.
elasticsearch.org找了一圈 嘿嘿, update一下即可

1
2
3
4
5
6
7
8
curl -XPUT localhost:8301/store/client/_mapping -d '
{
    "client" : {
        "properties" : {
            "local_ip" : {"type" "string""store" "yes"}   
        }
    }
}


报错结果

1
{"error":"MergeMappingException[Merge failed with failures {[mapper [local_ip] of different type, current_type [long], merged_type [string]]}]","status":400}



尼玛 真逗  我long想转一下string 居然失败(elasticsearch产品层面理应支持这种无损转化)  无果
Google了一下类似的案例 (案例)
在一个帖子中得到的elasticsearch开发人员的准确答复


  "You can't change existing mapping type, you need to create a new index with the correct mapping and index the data again."

想想 略坑啊 我不管是因为elasticsearch还是因为底层Lucene的原因,修改一个field需要对所有已有数据的所有field进行reindex,这本身就是一个逆天的思路,但是elasticsearch的研发人员还觉得这没有什么不合理的.

在Elasticsearch上游逛了一圈,上面这样写到
(http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/)
the problem — why you can’t change mappings

You can only find that which is stored in your index. In order to make your data searchable, your database needs to know what type of data each field contains and how it should be indexed. If you switch a field type from e.g. a string to a date, all of the data for that field that you already have indexed becomes useless. One way or another, you need to reindex that field.

...
OK,这一段话很合理,我改了一个field的类型 需要对这个field进行reindex,如论哪种数据库都需要这么做,没错.
我们再继续往下看看,reindexing your data, 尼玛一看,弱爆了,他的reindexing your data不是对修改的filed进行reindex,而是创建了一个新的index,对所有的filed进行reindexing, 太逆天了。

吐槽归吐槽,这个事情逃不了,那我就按他的来吧.
首先创建一个新的索引

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
curl -XPUT localhost:8305/store_v2 -d '
{
    "settings" : {
        "number_of_shards" : 20
    },
    "mappings" : {
      "client" : {
        "properties" : {
          "ip" : {
            "type" "string"
          },
          "cost" : {
            "type" "long"
          },
}


等等,我创建了新索引,client往Elasticsearch的代码不会需要修改吧,瞅了一眼,有解决方案,建立一个alias(别名,和C++引用差不多),通过alias来实现对后面索引数据的解耦合,看到这,舒了一口气。

现在的问题是 这是一个线上服务,不能停服务,所以我需要一个倒数据到我的新索引的一个方案
Elasticsearch官网写到
  pull the documents in from your old index, using a scrolled search and index them into the new index using the bulk API. Many of the client APIs provide a reindex() method which will do all of this for you. Once you are done, you can delete the old index.
第一句,看起来很美好,找了一圈,尼玛无图无真相,Google都没有例子,你让我怎么导数据?
第二句 client APIS, 看起来只有这个方法可搞了

python用起来比较熟,所以我就直接选 pyes了,装了一大堆破依赖库之后,终于可以run起来了

1
2
3
4
5
6
7
8
import pyes
conn = pyes.es.ES("http://10.xx.xx.xx:8305/")
search = pyes.query.MatchAllQuery().search(bulk_read=1000)
hits = conn.search(search, 'store_v1''client', scan=True, scroll="30m", model=lambda _,hit: hit)
for hit in hits:
     #print hit
     conn.index(hit['_source'], 'store_v2''client', hit['_id'], bulk=True)
conn.flush()

 
花了大概一个多小时,新的索引基本和老索引数据一致了,对于线上完成瞬间的增量,这里没心思关注了,数据准确性要求没那么高,得过且过。

接下来修改alias别名的指向(如果你之前没有用alias来改mapping,纳尼就等着哭吧)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
curl -XPOST localhost:8305/_aliases -d '
{
    "actions": [
        "remove": {
            "alias""store",
            "index""store_v1"
        }},
        "add": {
            "alias""store",
            "index""store_v2"
        }}
    ]
}
'

 
啷啷锵锵,正在追数据中

等新索引的数据已经追上时

将老的索引删掉

1
curl -XDELETE localhost:8303/store_v1

 

至此完成!

一件如此简单的事情,Elasticsearch居然能让他变得如此复杂,真是牛逼啊...

分享到:
评论

相关推荐

    ES客户端+谷歌浏览器插件+Multi-Elasticsearch-Head

    多弹性搜索头,对著名的 Elasticsearch Head 的改进 1.保存和存储几个Elasticsearch端点 ...它通常提供了一种直观的方式来查看索引结构、执行查询语句、分析日志数据,以及执行其他与Elasticsearch管理相关的任务。

    ES同义词插件 elasticsearch-analysis-dynamic-synonym-6.5.1.rar

    在IT领域,尤其是在搜索引擎优化和大数据分析中,Elasticsearch(ES)是一个广泛使用的开源全文检索引擎。它基于Lucene库,提供了分布式、实时、高可用性以及容错能力的数据存储和搜索解决方案。本篇文章将重点讲解...

    elasticsearch-analysis-ik-8.2.0.zip

    Elasticsearch(ES)作为一款强大的开源搜索引擎,广泛应用于大数据处理和信息检索领域。在处理中文文本时,选择合适的分词器至关重要,elasticsearch-analysis-ik-8.2.0.zip 提供了针对 Elasticsearch 8.2.0 版本的...

    elasticsearch-head 安装工具资源

    - **灵活性**:可以配置连接到多个Elasticsearch集群,并在它们之间轻松切换。 **3. 安装Elasticsearch-Head** Elasticsearch-Head通常作为一个独立的Grunt项目运行,首先确保已安装Node.js和Grunt CLI。然后,通过...

    elasticsearch安装文档

    ### Elasticsearch在Linux环境下的安装与配置 #### 一、安装准备 在开始安装Elasticsearch之前,确保系统中已经安装了JDK 1.8。这是因为Elasticsearch依赖于Java运行环境。 **步骤1:安装JDK 1.8** 1. 下载JDK 1.8...

    监控ElasticSearch状态可视化工具.zip

    ElasticSearch连接树管理(更方便的切换测试/生产环境) 支持权限管理 支持sql转换成dsl语法 更方便的重建索引 任务管理 备份管理 可将查询内容下载为excel文件 可进行索引创建,映射创建,别名创建,索引删除等操作...

    Elasticsearch Sense插件

    6. **多集群支持**:你可以配置多个Elasticsearch集群,并在它们之间轻松切换。 7. **快捷键**:Sense支持一系列快捷键,提高操作效率。 **应用场景** Elasticsearch Sense广泛应用于数据分析、日志分析、搜索引擎...

    Blog.Core 5.0 开源框架 换掉ES 使用elasticsearch 。

    在Blog.Core 5.0的更新中,开发团队决定替换原有的搜索引擎,从ES(Elasticsearch)转向了Elasticsearch,这是一个非常重要的决策,因为它涉及到大数据处理、全文检索以及搜索引擎优化等关键领域。 首先,Elastic...

    elasticsearch-analysis-ik-7.3.2.zip

    Elasticsearch(ES)作为一个强大的全文搜索引擎,其在处理中文文档时,对中文分词的准确性和效率有着至关重要的作用。"elasticsearch-analysis-ik"是ES中最受欢迎的中文分词器之一,专为提升中文文本分析性能而设计...

    最新版 elasticsearch-analysis-ik-6.8.10.zip

    Elasticsearch Analysis IK 是一个为 Elasticsearch 设计的中文分词插件,它的最新版本是 6.8.10。这个压缩包包含了该插件及其依赖的库文件,用于优化针对中文文本的索引和搜索性能。以下是关于 Elasticsearch、...

    Elasticsearch集群部署.docx

    值得注意的是,实际生产环境中还需要根据具体的业务需求对Elasticsearch进行更详细的配置和优化,例如增加副本数量、调整索引设置等。此外,对于高可用性和容错性的要求较高的场景,还需考虑引入Kibana、Logstash等...

    1 ElasticSearch 安装

    ### Elasticsearch在Linux系统的安装与配置详解 #### 一、引言 Elasticsearch是一款基于Lucene的开源搜索和分析引擎,适用于全文检索、结构化数据检索等场景。它以其高性能、高扩展性和易于使用的特性而受到广泛...

    最新版 elasticsearch-analysis-ik-7.8.0.zip

    在 Elasticsearch 中,IK 分析器是处理中文文档的关键,能够帮助搜索引擎更好地理解和索引中文文本。 压缩包中的文件列表包括了一些核心的依赖库,这些库是 IK 分析器运行所必需的: 1. `httpclient-4.5.2.jar`:...

    ElasticSearch 7.0 版本

    - **类型移除**:在7.0版本中,Elasticsearch移除了类型(type)的概念,每个索引只包含一种类型,即 `_doc`,简化了数据模型,提高了索引管理和查询的效率。 - **字段别名**:新增了字段别名功能,允许用户为字段...

    elasticsearch-6.1.3

    7. **容错机制**:Elasticsearch通过副本分片(replicas)提供数据冗余,当主分片出现问题时,可以无缝切换到副本分片,保证服务连续性。 8. **RESTful API**:Elasticsearch采用HTTP RESTful接口,易于集成到各种...

    elasticsearch-head-master.zip

    Elasticsearch Head 插件是一款极其实用的工具,它提供了基于Web的用户界面,使得用户可以方便地监控和管理Elasticsearch集群。这个名为"elasticsearch-head-master.zip"的压缩包包含了该插件的源码及其相关文件,...

    最新版 elasticsearch-analysis-ik-6.8.8.zip

    这个插件的主要目标是提高 Elasticsearch 对中文文本的处理能力,使其能够更好地进行全文搜索、索引和分析。IK 分词器在中文社区中有很高的评价,它提供了丰富的自定义策略,可以满足各种复杂的中文分词需求。 首先...

    Laravel开发-laravel-scout-elasticsearch

    一旦定义了这个方法,当你保存或更新模型时,Scout 会自动同步数据到 Elasticsearch 索引。 进行搜索时,你可以使用 Scout 提供的 `search()` 方法: ```php $results = YourModel::search('query term')->get(); ...

Global site tag (gtag.js) - Google Analytics