es使用reindex"修改"字段类型

    当使用空间(geo_point)查询或时间(date)查询时,字段类型错误,使用reindex“修改”字段类型,ES不支持支持修改字段类型。

 

思路:

  1. 新建索引,执行字段类型
  2. 使用reindex将原有数据复制到新索引中

 

设置:

    old_index = users

    old_type = user

    new_index = users_temp

    new_type = user

 

birth_date不是时间类型,location不是geo_point类型 

  1. 查询原索引数据
    GET /users/_search

  2. 查询索引
    GET /users/_mapping

  3. 新建索引
    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"
    				}
    			}
    		}
    	}
    }

  4. 使用reindex复制数据
    POST /_reindex
    {
      "source": {
        "index": "users",
        "query": {
          "match_all": {}
        }
      }, 
      "dest": {
        "index": "users_temp"
      }
    }

  5. 删除原有索引
    DELETE /users

  6. 创建索引,索引名为原来的索引名
    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"
    				}
    			}
    		}
    	}
    }

  7. reindex将数据复制回去
    POST /_reindex
    {
      "source": {
        "index": "users_temp",
        "query": {
          "match_all": {}
        }
      }, 
      "dest": {
        "index": "users"
      }
    }

  8. 删除那个创建的临时索引
    DELETE /users_temp

 

查看索引类型

GET /users/_mapping

 

 

数据查询

GET /users/_search

 

 

 

备注:

  1. 使用reindex的时候不要修改 type
  2. 跨域

    修改${ES_HOME}/config/elasticsearch.yml,加入或修改以下配置 

    1. 修改ES配置
      reindex.remote.whitelist: ["192.168.121.128:9200"]

      ":" 后面有个空格。 

      elasticsearch.yml是在当前机器,白名单指向的是数据来源的机器 

    2. _reindex
      POST /_reindex
      {
        "source": {
          "index": "users",
          "remote": {
            "host": "http://192.168.121.128:9200"
          }
        },
        "dest": {
          "index": "users_temp"
        }
      }

 

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流年ln

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值