简体   繁体   English

ElasticSearch:如何更新现有索引中的文档类型

[英]ElasticSearch: How to update a document type in an existing index

Long story short:长话短说:

I have created a strict template, created several daily indices and changes one of the document types ( %_level type from float to integer ) in the template.我创建了一个严格的模板,创建了几个每日索引并更改了模板中的一种文档类型( %_level类型从floatinteger )。

After that, I've created the rest of the daily indices.之后,我创建了其余的每日指数。

The problem:问题:

I need to change the document type ( %_level type from float to integer ) in the old indices to be compatible with the new indices.我需要将旧索引中的文档类型( %_level类型从float更改为integer )以与新索引兼容。 How can I do this?我怎样才能做到这一点?

Now for the details...现在了解详情...

I have the following strict template:我有以下严格的模板:

PUT _template/example-template
{
    "order":0,
    "version":200,
    "index_patterns":["example-*"],
    "settings":{
        "index":{
            "number_of_shards":4
         }
    },
    "mappings": {
        "iterations": {
            "dynamic":"strict",
            "properties": {
                "%_average1": {
                    "type":"float"
                },
                "%_average2": {
                    "type":"float"
                },
                "sum": {
                    "type":"integer"
                },
                "%_level": {
                    "type":"float"
                }
            }
        }
    }
}

Several indices were created with this template.使用此模板创建了多个索引。

  • example-20191220示例-20191220
  • example-20191221示例-20191221
  • example-20191222示例-20191222
  • example-20191223示例-20191223

After a while I've realised that we need to change %_level type from float to integer , so I have changed the template into the following:一段时间后,我意识到我们需要将%_level类型从float更改为integer ,因此我将模板更改为以下内容:

PUT _template/example-template
{
    "order":0,
    "version":200,
    "index_patterns":["example-*"],
    "settings":{
        "index":{
            "number_of_shards":4
         }
    },
    "mappings": {
        "iterations": {
            "dynamic":"strict",
            "properties": {
                "%_average1": {
                    "type":"float"
                },
                "%_average2": {
                    "type":"float"
                },
                "sum": {
                    "type":"integer"
                },
                "%_level": {
                    "type":"integer"
                }
            }
        }
    }
}

Now the the following indices were created where %_level type is an integer .现在创建了以下索引,其中%_level类型是整数

  • example-20192412示例-20192412
  • example-20192512示例-20192512
  • example-20192612示例-20192612

But the old indices contains indices with %_level that is float and the new indices are with %_level that is an integer .但是旧索引包含%_level浮点数的索引,而新索引包含%_level整数

I needed to convert the old indices %_level from float to integer so I can build a report with all the indices.我需要将旧索引%_level浮点数转换为整数,以便我可以构建包含所有索引的报告。

How can I change the %_level in the old indices from float to integer ?如何将旧索引中的%_levelfloat更改为integer

Although you can add and remove document names from an exisitng index by providing the document ID, it is problematic to do so if you want to update the document type, and to apply it on all the index document.尽管您可以通过提供文档 ID 从现有索引中添加和删除文档名称,但如果您想更新文档类型并将其应用于所有索引文档,则这样做是有问题的。

A good solution for this problem will be reindex.这个问题的一个很好的解决方案是重新索引。

Important:重要的:

The reindex process can convert some types implicitly ( Numeric Type Casting ) and the others explicitly.重新索引过程可以隐式转换某些类型( 数字类型转换)和显式转换其他类型。

In case you need implicit conversion, you can skip section 2如果您需要隐式转换,您可以跳过第 2 节

Meaning, apply the following over all the old indices (eg example-20191220 ):意思是,对所有旧索引应用以下内容(例如example-20191220 ):

  1. create a new index from example-20191220 that will be called example-20191220-newexample-20191220创建一个名为example-20191220-new的新索引
  2. add a painless script in the creation process that convert %_level type from float to integer在创建过程中添加一个轻松的脚本,将%_level类型从浮点数转换为整数
  3. delete the old index example-20191220删除旧索引示例-20191220
  4. reindex example-20191220-new into example-20191220example-20191220-new重新索引为example-20191220

Now, the 'updated' index example-20191220 is with the correct %_level type.现在,'updated' 索引example-20191220具有正确的%_level类型。

The reindex in section 2 should look like the following:第 2 节中的重新索引应如下所示:

(it is performed and tested on Kibana Dev Tools ) (它在Kibana Dev Tools上执行和测试)

POST _reindex
{
    "source": {
        "index": "example-20191220"
    },
    "dest": {
        "index": "example-20191220-new"
    },
    "script": {
    "lang": "painless", 
    "source": 
        """
        Double num = ctx._source['%_level'];
        if(num != null)
        { ctx._source['%_level'] = num.intValue(); }
        """
    }
}

The reindex in section 4 should look like the following:第 4 节中的重新索引应如下所示:

(it is performed and tested on Kibana Dev Tools ) (它在Kibana Dev Tools上执行和测试)

POST _reindex
{
    "source": {
        "index": "example-20191220-new"
    },
    "dest": {
        "index": "example-20191220"
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM