elasticsearch 添加新字段

在使用ES的时候,给原有index的添加、删除、修改等操作都是比较常见的现象。因此,记录一些常用的用法对我们开发效率将有很大提升。

前面的文章已经介绍了如何创建ES索引的,本次实例讲继续使用使用datacube_live索引来做演示。

为了方便观看,仍然将datacube_live的现有结构展现出来,本次用来演示在datacube_live添加is_del字段,来给es数据做软删除标记,将is_del类型定义为boolean类型。

1.获取datacube_live当前结构体:

GET http://xxxx:9200/datacube_live/_mapping

{
 "datacube_live": {
 "mappings": {
 "properties": {
 "create_at": {
 "type": "keyword"
 },
 "creator_id": {
 "type": "keyword"
 },
 "goods_list": {
 "type": "nested",
 "properties": {
 "activity_type": {
 "type": "keyword"
 },
 "area_ids": {
 "type": "keyword"
 },
 "category_id": {
 "type": "keyword"
 },
 "goods_id": {
 "type": "keyword"
 },
 "goods_name": {
 "type": "text"
 },
 "lecture_type": {
 "type": "keyword"
 },
 "sku_code": {
 "type": "keyword"
 }
 }
 },
 "is_callback": {
 "type": "boolean"
 },
 "is_empty": {
 "type": "boolean"
 },
 "is_pal_15": {
 "type": "boolean"
 },
 "live_date": {
 "type": "keyword"
 },
 "live_start_time": {
 "type": "keyword"
 },
 "push_type": {
 "type": "keyword"
 },
 "room_id": {
 "type": "keyword"
 },
 "room_name": {
 "type": "text"
 },
 "shop_ids": {
 "type": "keyword"
 }
 }
 }
 }
}

2.定义is_del类型

在定义is_del类型的时候,需要将其放入properties属性当中去,结构如下所示。

{
 "properties": {
 "is_del": {
 "type": "boolean"
 }
 }
}

3.将定义的is_del字段添加至datacube_live结构体当中去。

调用ES的http://xxxx:9200/datacube_liv...接口,修改结构体。

PUT http://xxxx:9200/datacube_live/_mapping

{
 "properties": {
 "is_del": {
 "type": "boolean"
 }
 }
}

4.验证结构体

GET http://xxxx:9200/datacube_live/_mapping

{
 "datacube_live": {
 "mappings": {
 "properties": {
 "create_at": {
 "type": "keyword"
 },
 "creator_id": {
 "type": "keyword"
 },
 "goods_list": {
 "type": "nested",
 "properties": {
 "activity_type": {
 "type": "keyword"
 },
 "area_ids": {
 "type": "keyword"
 },
 "category_id": {
 "type": "keyword"
 },
 "goods_id": {
 "type": "keyword"
 },
 "goods_name": {
 "type": "text"
 },
 "lecture_type": {
 "type": "keyword"
 },
 "sku_code": {
 "type": "keyword"
 }
 }
 },
 "is_callback": {
 "type": "boolean"
 },
 "is_del": {
 "type": "boolean"
 },
 "is_empty": {
 "type": "boolean"
 },
 "is_pal_15": {
 "type": "boolean"
 },
 "live_date": {
 "type": "keyword"
 },
 "live_start_time": {
 "type": "keyword"
 },
 "push_type": {
 "type": "keyword"
 },
 "room_id": {
 "type": "keyword"
 },
 "room_name": {
 "type": "text"
 },
 "shop_ids": {
 "type": "keyword"
 }
 }
 }
 }
}

原有datacube_live的结构体添加了is_del类型,如下图所示。

到这里只能说明is_del新增字段添加成功了,此时仍然不能说明工作就完成了。如果现在就去查询datacube_live数据的话,目前仍然没有is_del属性,需要对datacube_live进行数据更新操作,之后才说明完成整个字段添加,关于如何更新这里暂不做说明。

作者:杨帆原文地址:https://segmentfault.com/a/1190000042184718

%s 个评论

要回复文章请先登录注册