如何更新elasticsearch中的字段类型

max*_*mus 32 elasticsearch

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

我索引了一些推文,其中一个字段created_at,索引为字符串而不是日期.我无法通过卷曲调用找到如何通过此更改重新索引.如果重建索引是一个复杂的过程,那么我宁愿只删除那里的东西并重新开始.但是,我找不到如何指定字段类型!

任何帮助是极大的赞赏.

dad*_*net 28

您需要使用Put Mapping AP I 定义映射.

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

日期可以定义如下:

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"}
        }
    }
}
'
Run Code Online (Sandbox Code Playgroud)

  • @dadoonet 任何将“消息”字段类型更改从字符串更改为“长”的方法。合并失败,失败 {[mapper [message] of different type, current_type [string] (2认同)

小智 9

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

"properties": {
    "updated_at": {
         "type": "date",
         "format": "yyyy-MM-dd HH:mm:ss"
     }
 }
Run Code Online (Sandbox Code Playgroud)

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

"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"}
    }
} 
Run Code Online (Sandbox Code Playgroud)


Akh*_*N S 5

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

创建新索引

PUT project_new
Run Code Online (Sandbox Code Playgroud)

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

PUT project_new/_mapping/_doc
{
    "properties": {
        "created_by": {
            "type": "text"
        },
        "created_date": {
            "type": "date"
        },
        "description": {
            "type": "text"
        }
}
}
Run Code Online (Sandbox Code Playgroud)

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

POST _reindex
{
    "source": {
        "index": "project"
    },
    "dest": {
        "index": "project_new",
        "version_type": "external"
    }
}
Run Code Online (Sandbox Code Playgroud)

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

POST _aliases
{
    "actions": [
        {
            "add": {
                "index": "project_new",
                "alias": "project"
            }
        },
        {
            "remove_index": {
                "index": "project"
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

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

Elasticsearch 版本 6.4.3 中测试和工作