如何更新elasticsearch中字段的数据类型

How to update data type of a field in elasticsearch

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

我对生成数据的 python 代码进行了一些更改,因此现在此字段 Data.CPU 已变为 integer。但 elasticsearch 仍然将其显示为字符串。如何更新它的数据类型。

我在 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我们可以转换数据类型,但我无法正确理解它。

我不想删除索引并重新创建,因为我已经基于该索引创建了可视化,删除后它也会被删除。谁能帮忙解决这个问题。

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

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

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

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

重新索引其中的数据

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

您遇到的特定错误是因为您在请求路径中包含了 /doc/(您可能想要 /<index>/_mapping),但仅修复此问题是不够的。

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

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

使用脚本字段

您可以将脚本化字段添加到 Kibana index-pattern。它实施起来很快,但对性能有重大影响。您可以在 Elastic 博客 here 上阅读更多关于它们的信息(尤其是在“匹配数字和 return 匹配 ”标题下)。

添加一个新的multi-field

您可以添加一个 new multifield。下面的示例假设 CPUData 下的嵌套字段,而不是 实际上 被称为 Data.CPU 并带有文字 .

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

在 ES 中重新索引您的数据

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

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

如果您能够在不影响用户的情况下及时从源中重新生成数据,您只需删除索引并使用更新的映射重新获取所有数据。