如何在elasticsearch中更新字段类型

8e2ybdfx  于 7个月前  发布在  ElasticSearch
关注(0)|答案(4)|浏览(89)

ElasticSearch文档只是不清楚如何做到这一点。
我索引了一些tweets,其中一个字段created_at,索引为字符串而不是日期。我找不到如何通过curl调用重新索引此更改。如果重新索引是一个复杂的过程,那么我宁愿删除那里的内容并重新开始。但是,我也找不到如何指定字段类型!
任何帮助都非常感谢。

insrf1ej

insrf1ej1#

您需要使用Put Mapping AP I定义一个Map。

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

yrwegjxp

yrwegjxp2#

你还需要指定格式,而不仅仅是类型,如果你要插入一个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"}
    }
}

siv3szwd

siv3szwd3#

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

新建索引

PUT project_new

字符串
使用新的字段类型Map更新Map

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


现在,您将能够在现有索引中查看更新的类型。
已在Eleasticsearch 6.4.3版中测试和运行

mqkwyuun

mqkwyuun4#

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

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

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

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

相关问题