Elasticsearch学习(四)

来源:互联网 发布:合肥广电网络客服电话 编辑:程序博客网 时间:2024/06/10 06:29

Elasticsearch的mapping一旦创建,只能增加字段,而不能修改已经mapping的字段。如果已经在线上运行着,我们想修改mapping字段的类型或者结构,那我们该怎么做呢?如果数据量小,可以快速删除索引,并重新构建新的索引,然后再把数据同步到索引中。如果数据量非常庞大,显然这种方式存在问题。如何不停服务重建索引是我们要考虑的问题。当我们的程序访问索引库时,考虑使用别名来访问,而不要使用真正的indexName。我们可以重新再构建一个新的索引,然后在索引更完数据之后,修改之前的别名即可。

创建别名

curl -XPOST localhost:9200/_aliases -d '{     "actions": [        {"add": {"index":"test","alias": "test-aliases"}}        ]    }'

索引结构

{  "test" : {    "aliases" : {      "test-aliases" : { }    },    "mappings" : {      "user" : {        "properties" : {          "account" : {            "type" : "string",            "index" : "not_analyzed"          },          "avatar" : {            "type" : "string",            "index" : "not_analyzed"          },          "createdTimestamp" : {            "type" : "date",            "format" : "yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"          }        }      }    },    "settings" : {      "index" : {        "creation_date" : "1473248589616",        "number_of_shards" : "5",        "number_of_replicas" : "1",        "uuid" : "eX_Wr45-Q3KBfoijNV_UXw",        "version" : {          "created" : "2020199"        }      }    },    "warmers" : { }  }}

待更新索引

新建索引test_v1(本次只修改account字段类型)。

{  "test" : {    "aliases" : {      "test-aliases" : { }    },    "mappings" : {      "user" : {        "properties" : {          "account" : {            "type" : "integer"          },          "avatar" : {            "type" : "string",            "index" : "not_analyzed"          },          "createdTimestamp" : {            "type" : "date",            "format" : "yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"          }        }      }    },    "settings" : {      "index" : {        "creation_date" : "1473248589616",        "number_of_shards" : "5",        "number_of_replicas" : "1",        "uuid" : "eX_Wr45-Q3KBfoijNV_UXw",        "version" : {          "created" : "2020199"        }      }    },    "warmers" : { }  }}

然后把数据同步到test_v1索引。

修改别名

"actions": [      {"remove": {"index":"test","alias": "test-aliases"}},      {"add": {"index":"test-v1","alias": "test-aliases"}}        ]    }

然后查看别名test-aliases对应的mapping,满足期望效果。

{  "test_v1" : {    "aliases" : {      "test-aliases" : { }    },    "mappings" : {      "user" : {        "properties" : {          "account" : {            "type" : "integer"          },          "avatar" : {            "type" : "string",            "index" : "not_analyzed"          },          "createdTimestamp" : {            "type" : "date",            "format" : "yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"          }        }      }    },    "settings" : {      "index" : {        "creation_date" : "1473305627645",        "number_of_shards" : "5",        "number_of_replicas" : "1",        "uuid" : "DTlK4py3Tcy-2LNruLWGAw",        "version" : {          "created" : "2020199"        }      }    },    "warmers" : { }  }}

别名test-aliases实际指向的是test-v1。如果数据一切OK了,就可以删除原来的索引test

0 0
原创粉丝点击