elasticsearch之type基本操作

elasticsearch中的type,相当于sql中的数据表,另外表是有结构的,所以在创建表时要指定结构,es中的表结构指mapping。注意,在es6.0以后的版本,只支持一个type,也就是type名称默认为 _mappings,不再有其他别名,所操作的都是 _mappings 这个 type。

(案例使用到了 ik 分词,需要先安装 ik 分词器插件)

elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.2.0/elasticsearch-analysis-ik-8.2.0.zip

新建mapping的数据格式为:

PUT index_name/_mappings
{
    "properties": {
		"content": {
			"analyzer": "ik_max_word",
			"type": "text",
			"fields": {
				"keyword": {
					"ignore_above": 256,
					"type": "keyword"
				}
			}
		},
		"relation": {
			"type": "nested"
		},
		"params": {
			"type": "object"
		},
		"time": {
			"type": "date",
			"format": "epoch_millis"
		}
	}

}

(时间日期,可指定多种类型 ,如:yyyy-MM-dd HH:mm:ss|| epoch_millis)

查询所有的type,使用 pretty 时显示 index 和 type 的关系

curl -XGET "http://localhost:9200/_mapping?pretty=true"

删除一个mapping(type)

似乎是不可以删除mapping的,可以把index删除重建(或新建mapping),此时mapping也跟着删除了。

curl -X DELETE localhost:9200/index_name?pretty

对mapping添加字段:

添加字段时,方式与新增一致,也使用 put 把新的mapping添加字段添加到 index 中即可。

对于修改字段类型、删除mapping等操作,都需要创建一个新的index,并按照新的type同步索引,是比较麻烦的,所以索引的建立应该思考确定后再提交,不要频繁更换。

如果创建新的index太费劲,也可以考虑创建新的 mapping,并为该字段同步数据,即可得到想要的数据,旧的mapping不管它即可。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注