目录:场景一:修改 ElasticSearch 的字段(Field)的类型
场景二:添加/新增 ElasticSearch 的字段(Field)
场景一:需要修改ElasticSearch的字段(Field)的类型
分析:ElasticSearch是不支持修改字段(Field)的类型,不像mysql一个可以修改字段(Column)的数据类型,所有我们需要其他的思路来解决这个需求
解决方法:假设我们已经有了一个索引(Index)名称为:test 我们需要修改字段(Field)name(现在为“text”类型) 的类型为“keyword”类型,那么需要一下操作:
- 1:新建一个索引(Index)名称为:test_v2,将字段(Field)name 的类型设置为“keyword”类型。
- 2:将原来索引test的数据复制到test_v2中。
- 3:删除索引test。
- 4:新建一个新的索引(Index)名称为:test,并且将字段(Field)name 的类型设置为“keyword”类型。
- 5:将原来索引test_v2的数据复制到test中。
- 6:删除索引test_v2。至此字段修改已经完成了
实践一下:
现在test的索引类型如下:知识点:查看索引的mapping结构
GET /test/_mapping
#返回信息
{
"test": {
"mappings": {
"user": {
"properties": {
"age": {
"type": "integer"
},
"createDate": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"id": {
"type": "keyword"
},
"name": {
"type": "text"
}
}
}
}
}
}
1:新建一个索引(Index)名称为:test_v2,将字段(Field)name 的类型设置为“keyword”类型。知识点:创建索引
PUT test_v2
{
"mappings": {
"user": {
"properties": {
"age": {
"type": "integer"
},
"createDate": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"id": {
"type": "keyword"
},
"name": {
"type": "keyword"
}
}
}
}
}
2:将原来索引test的数据复制到test_v2中。知识点:复制索引数据到另个一个索引里
POST _reindex
{
"source": {
"index": "test"
},
"dest": {
"index": "test_v2"
}
}
3:删除索引test。知识点:删除索引
DELETE /test
4:新建一个新的索引(Index)名称为:test,并且将字段(Field)name 的类型设置为“keyword”类型。
PUT test
{
"mappings": {
"user": {
"properties": {
"age": {
"type": "integer"
},
"createDate": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"id": {
"type": "keyword"
},
"name": {
"type": "keyword"
}
}
}
}
}
5:将原来索引test_v2的数据复制到test中。
POST _reindex
{
"source": {
"index": "test_v2"
},
"dest": {
"index": "test"
}
}
6:删除索引test_v2。
DELETE /test_v2
注意:如果ElasticSearch的索引使用了别名可以在“步骤3”完成后做一下操作即可
查看索引—别名映射
GET _cat/aliases
#返回信息如下
test_aliases test - - -
第一列是索引别名,第二列是索引名称
“步骤3”完成后,将test_v2索引映射到索引别名test_aliases即可
POST _aliases
{
"actions": [
{
"add": {
"alias": "test_aliases",
"index": "test_v2"
}
}
]
}
场景二:添加/新增ElasticSearch的字段(Field)
分析:如果ElasticSearch字段已经建好,但是需要添加字段怎么办呢,ElasticSearch是支持添加字段的,但是添加后之前的数据是没有这个字段的,之后新增的或者修改后添加了这个字段才会显示
基本语法:
PUT /{index}/_mapping/{type}
{
"{type}": {
"properties": {
"{filedName1}": {
"type": "keyword"
}, "{filedName2}": {
"type": "text"
}
}
}
}
实践:向test_v2索引新增togs字段
PUT /test_v2/_mapping/user
{
"user": {
"properties": {
"tags": {
"type": "keyword"
}
}
}
}
场景三:ElasticSearch根据某个字段模糊查询
分析:ElasticSearch 的字段类型为“keyword” 是支持模糊查询,就像mysql的select * from table like '%孩%' 一样
基本语法:
GET /{index}/_search
{
"query": {
"wildcard": {
"FIELD": {
"value": "VALUE"
}
}
}
}
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 1,
"hits": [
{
"_index": "test_v2",
"_type": "user",
"_id": "5",
"_score": 1,
"_source": {
"id": "5",
"name": "我们是祖国的花朵",
"age": 30,
"createDate": "2021-03-01 09:03:04"
}
},
{
"_index": "test_v2",
"_type": "user",
"_id": "2",
"_score": 1,
"_source": {
"id": "2",
"name": "rose",
"age": 19,
"createDate": "2021-03-01 09:03:04"
}
},
{
"_index": "test_v2",
"_type": "user",
"_id": "4",
"_score": 1,
"_source": {
"id": "4",
"name": "我们是孩子",
"age": 30,
"createDate": "2021-03-01 09:03:04"
}
},
{
"_index": "test_v2",
"_type": "user",
"_id": "1",
"_score": 1,
"_source": {
"id": "2",
"name": "rose",
"age": 30,
"createDate": "2021-03-01 09:03:04"
}
},
{
"_index": "test_v2",
"_type": "user",
"_id": "3",
"_score": 1,
"_source": {
"id": "3",
"name": "我们都是好孩子",
"age": 30,
"createDate": "2021-03-01 09:03:04"
}
}
]
}
}
上面是基础数据,我们来实践一下:
GET /test_v2/_search
{
"query": {
"wildcard": {
"name": {
"value": "孩"
}
}
}
}
返回结果:
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "test_v2",
"_type": "user",
"_id": "4",
"_score": 1,
"_source": {
"id": "4",
"name": "我们是孩子",
"age": 30,
"createDate": "2021-03-01 09:03:04"
}
},
{
"_index": "test_v2",
"_type": "user",
"_id": "3",
"_score": 1,
"_source": {
"id": "3",
"name": "我们都是好孩子",
"age": 30,
"createDate": "2021-03-01 09:03:04"
}
}
]
}
}
作者:杜云明 欢迎转载,与人分享是进步的源泉!