当使用空间(geo_point)查询或时间(date)查询时,字段类型错误,使用reindex“修改”字段类型,ES不支持支持修改字段类型。
思路:
- 新建索引,执行字段类型
- 使用reindex将原有数据复制到新索引中
设置:
old_index = users
old_type = user
new_index = users_temp
new_type = user
birth_date不是时间类型,location不是geo_point类型
- 查询原索引数据
GET /users/_search
- 查询索引
GET /users/_mapping
- 新建索引
PUT /users_temp { "mappings": { "user": { "properties": { "age": { "type": "long" }, "birth_date": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" }, "lat": { "type": "float" }, "location": { "type": "geo_point" }, "lon": { "type": "float" }, "sex": { "type": "long" }, "user_age": { "type": "integer" }, "user_id": { "type": "long" }, "user_name": { "type": "text" }, "user_sex": { "type": "integer" } } } } }
- 使用reindex复制数据
POST /_reindex { "source": { "index": "users", "query": { "match_all": {} } }, "dest": { "index": "users_temp" } }
- 删除原有索引
DELETE /users
- 创建索引,索引名为原来的索引名
PUT /users { "mappings": { "user": { "properties": { "age": { "type": "long" }, "birth_date": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" }, "lat": { "type": "float" }, "location": { "type": "geo_point" }, "lon": { "type": "float" }, "sex": { "type": "long" }, "user_age": { "type": "integer" }, "user_id": { "type": "long" }, "user_name": { "type": "text" }, "user_sex": { "type": "integer" } } } } }
- reindex将数据复制回去
POST /_reindex { "source": { "index": "users_temp", "query": { "match_all": {} } }, "dest": { "index": "users" } }
- 删除那个创建的临时索引
DELETE /users_temp
查看索引类型
GET /users/_mapping
数据查询
GET /users/_search
备注:
- 使用reindex的时候不要修改 type
- 跨域
修改${ES_HOME}/config/elasticsearch.yml,加入或修改以下配置
- 修改ES配置
reindex.remote.whitelist: ["192.168.121.128:9200"]
":" 后面有个空格。
elasticsearch.yml是在当前机器,白名单指向的是数据来源的机器
- _reindex
POST /_reindex { "source": { "index": "users", "remote": { "host": "http://192.168.121.128:9200" } }, "dest": { "index": "users_temp" } }
- 修改ES配置