【问题标题】:How to update a field type in elasticsearch如何更新elasticsearch中的字段类型
【发布时间】:2013-04-30 01:06:03
【问题描述】:

ElasticSearch 文档并不清楚如何执行此操作。

我索引了一些推文,其中一个字段 created_at 被索引为字符串而不是日期。我找不到如何通过 curl 调用重新索引此更改。如果重新索引是一个复杂的过程,那么我宁愿删除那里的内容并重新开始。但是,我也找不到如何指定字段类型!

非常感谢任何帮助。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您需要使用Put Mapping API 定义映射。

    curl -XPUT 'http://localhost:9200/twitter/_doc/_mapping' -H 'Content-Type: application/json'  -d '
    {
        "_doc" : {
            "properties" : {
                "message" : {"type" : "text", "store" : true}
            }
        }
    }
    '
    

    日期可以定义如下:

    curl -XPUT 'http://localhost:9200/twitter/_doc/_mapping' -H 'Content-Type: application/json'  -d '
    {
        "_doc" : {
            "properties" : {
                "user" : {"type" : "keyword", "null_value" : "na"},
                "message" : {"type" : "text"},
                "postDate" : {"type" : "date"},
                "priority" : {"type" : "integer"},
                "rank" : {"type" : "float"}
            }
        }
    }
    '
    

    【讨论】:

    • 是的,就是这样。谢谢dadoonet。
    • @dadoonet 将“消息”字段类型从字符串更改为“长”的任何方式。合并失败,失败 {[mapper [message] of different type, current_type [string]
    • @dibish 否。您需要重新索引。
    • "error": "MergeMappingException[Merge failed with failures {[mapper [event-time] of different type, current_type [string], mapped_type [date]]}]",
    • @Tyguy7 事件时间类型是较早的字符串,然后您尝试将其更改为日期。不允许像这样更改类型,因为它会干扰包含该字段的文档的索引。您将需要删除此类型并从头开始重新创建其映射。
    【解决方案2】:

    如果要插入 mysql 时间戳,您还需要指定格式,而不仅仅是类型,那么您应该像这样添加格式。

    "properties": {
        "updated_at": {
             "type": "date",
             "format": "yyyy-MM-dd HH:mm:ss"
         }
     }
    

    如果我们考虑你的例子,那么它应该是这样的

    "tweet" : {
        "properties" : {
            "user" : {"type" : "string", "index" : "not_analyzed"},
            "message" : {"type" : "string", "null_value" : "na"},
            "postDate" : {"type" : "date" , "format": "yyyy-MM-dd HH:mm:ss" },
            "priority" : {"type" : "integer"},
            "rank" : {"type" : "float"}
        }
    } 
    

    【讨论】:

    • 为什么会抛出错误然后 PUT filebeat-2018.10.22 { "mappings": { "doc": { "properties": { "system": { "type": "float" } , "idle": { "type": "float"} } } } } resource_already_exists_exception
    【解决方案3】:

    新版本的 Elasticsearch 不支持更改字段类型,但我们可以通过重新索引来实现。您可以按照以下步骤来实现索引的重新编排并在 Elasticsearch 中更改类型。

    创建新索引

    PUT project_new
    

    使用新的字段类型映射更新映射

    PUT project_new/_mapping/_doc
    {
        "properties": {
            "created_by": {
                "type": "text"
            },
            "created_date": {
                "type": "date"
            },
            "description": {
                "type": "text"
            }
    }
    }
    

    用旧索引重新索引新索引,即数据迁移

    POST _reindex
    {
        "source": {
            "index": "project"
        },
        "dest": {
            "index": "project_new",
            "version_type": "external"
        }
    }
    

    将新建索引的别名改为指向旧索引名

    POST _aliases
    {
        "actions": [
            {
                "add": {
                    "index": "project_new",
                    "alias": "project"
                }
            },
            {
                "remove_index": {
                    "index": "project"
                }
            }
        ]
    }
    

    现在您将能够在现有索引中查看更新后的类型。

    Elelasticsearch 版本 6.4.3

    中测试和工作

    【讨论】:

      【解决方案4】:

      更新现有索引中的字段类型:

      PUT test-index/doc/_mapping
      {
          "doc" : {
              "properties" : {
                  "testDateField" : {"type" : "date"}
              }
          }
      }
      

      在现有索引中添加特定类型的字段:

      PUT test-index
      {
        "mappings": {
          "doc": {
            "properties": {
              "testDateField" : {
                "type": "date"
              }
            }
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-15
        • 1970-01-01
        • 2016-11-05
        • 1970-01-01
        • 1970-01-01
        • 2017-07-28
        相关资源
        最近更新 更多