【问题标题】:How to update data type of a field in elasticsearch如何在elasticsearch中更新字段的数据类型
【发布时间】:2021-05-21 14:13:48
【问题描述】:

我正在使用 fluentd 将数据发布到 elasticsearch。它有一个字段Data.CPU,当前设置为string。索引名称为health_gateway

我对生成数据的 python 代码进行了一些更改,所以现在这个字段Data.CPU 现在变成了integer。但是弹性搜索仍然将其显示为字符串。如何更新它的数据类型。

我尝试在 kibana 开发工具中运行以下命令:

PUT health_gateway/doc/_mapping
{
    "doc" : {
        "properties" : {
            "Data.CPU" : {"type" : "integer"}
        }
    }
}

但它给了我以下错误:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "Types cannot be provided in put mapping requests, unless the include_type_name parameter is set to true."
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "Types cannot be provided in put mapping requests, unless the include_type_name parameter is set to true."
  },
  "status" : 400
}

还有这个document 说使用mutate 我们可以转换数据类型,但我无法正确理解。

我不想删除索引并重新创建,因为我已根据此索引创建了可视化,删除后它也会被删除。任何人都可以在这方面提供帮助。

【问题讨论】:

  • 您链接到的文档是关于 Logstash - 您能否确认您使用的是 Fluentd 的 Logstash 吗?
  • 不,我正在使用 fluentd。 Fluentd 正在将我所有的日志从 ubuntu 机器上传到 elasticsearch

标签: elasticsearch type-conversion kibana


【解决方案1】:

简短的回答是,您无法更改给定索引中已存在的字段的映射,如official docs 中所述。

您遇到的具体错误是因为您在请求路径中包含了/doc/(您可能想要/<index>/_mapping),但仅解决这个问题是不够的。

最后,我不确定你的字段名称中是否真的有一个点。上次我听说不能在字段名称中使用点。

尽管如此,在您的情况下,有几种前进的方法......这里有几个:

使用脚本字段

您可以将脚本字段添加到 Kibana 索引模式。它实施起来很快,但对性能有重大影响。您可以在 Elastic 博客 here 上阅读有关它们的更多信息(尤其是在标题“匹配一个数字并返回该匹配项”下)。

添加新的多字段

您可以添加new multifield。下面的示例假定CPUData 下的嵌套字段,而不是真正 被称为Data.CPU 并带有文字.

PUT health_gateway/_mapping
{
  "doc": {
    "properties": {
      "Data": {
        "properties": {
          "CPU": {
            "type": "keyword",
            "fields": {
              "int": {
                "type": "short"
              }
            }
          }
        }
      }
    }
  }
}

在 ES 中重新索引您的数据

使用Reindex API。请务必在目标索引上设置正确的映射。

从源中删除并重新索引所有内容

如果您能够及时从源重新生成数据,而不会干扰用户,您可以简单地删除索引并使用更新的映射重新获取所有数据。

【讨论】:

    【解决方案2】:

    您可以通过indexing the same field in multiple ways 更新映射,即使用多字段

    使用下面的映射,Data.CPU.raw 将是 integer 类型

    {
      "mappings": {
        "properties": {
          "Data": {
            "properties": {
              "CPU": {
                "type": "string",
                "fields": {
                  "raw": {
                    "type": "integer"
                  }
                }
              }
            }
          }
        }
      }
    }
    

    或者您可以使用正确的索引映射创建一个新索引,并使用reindex API重新索引其中的数据

    【讨论】:

    • 在您的映射中,您在哪里定义索引模式。我创建了多个索引,所以我认为我们还必须提及 Data.CPU 所属的索引。?
    • @SAndrew 您是否使用索引模板来创建索引,并且您已经定义了index_pattern
    • @SAndrew 您是否尝试过使用multi fields,如我的回答所示?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 2016-11-05
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    相关资源
    最近更新 更多