gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:46:55 24 4
gpt4 key购买 nike

长话短说:

我创建了一个严格的模板,创建了多个每日索引,并在模板中更改了一种文档类型(%_level 类型从float更改为integer)。

之后,我创建了其余的每日索引。

问题:

我需要在旧索引中更改文档类型(从浮点数到整数的%_level 类型),以便与新索引兼容。
我怎样才能做到这一点?

现在了解详细信息...

我有以下严格模板:

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"
}
}
}
}
}

使用此模板创建了多个索引。
  • 示例-20191220
  • 示例-20191221
  • 示例-20191222
  • 示例-20191223

  • 一段时间后,我意识到我们需要将 %_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"
    }
    }
    }
    }
    }

    现在,创建了以下索引,其中 %_level 类型是整数。
  • 示例-20192412
  • 示例-20192512
  • 示例-20192612

  • 但是,旧索引包含 %_level 为浮点的索引,而新索引包含 %_level 为整数。

    我需要将旧索引 %_level 从float转换为整数,以便可以使用所有索引构建报告。

    如何将旧索引中的 %_level 从float更改为整数?

    最佳答案

    尽管您可以通过提供文档ID来从现有索引中添加和删除文档名称,但是如果要更新文档类型并将其应用于所有索引文档,这样做是有问题的。

    解决此问题的一个好方法是重新索引。

    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



    意思是,将以下内容应用于所有旧索引(例如example-20191220):
  • example-20191220 创建新索引,该索引将称为 example-20191220-new
  • 在创建过程中添加一个轻松的脚本,该脚本将%_level 类型从float转换为整数
  • 删除旧索引示例-20191220
  • 重新索引示例20191220-新示例20191220

  • 现在,“已更新”索引 示例-20191220 具有正确的 %_level 类型。

    第2节中的重新索引应如下所示:

    (它是在Kibana开发工具上执行和测试的)
    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(); }
    """
    }
    }

    第4节中的重新索引应如下所示:

    (它是在Kibana开发工具上执行和测试的)
    POST _reindex
    {
    "source": {
    "index": "example-20191220-new"
    },
    "dest": {
    "index": "example-20191220"
    }
    }

    关于elasticsearch - ElasticSearch:如何更新现有索引中的文档类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59519350/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com