1.本文介绍了 elasticsearch 映射的操作方式,包括映射创建,修改,删除;
2.映射定义:映射是定义文档及其包含的字段如何存储和索引的过程。 例如,使用映射来定义:
小结:
3.索引类型被移除
4.本文部分内容总结自:
Mapping | Elasticsearch Guide [7.2] | Elastic
序号 |
字段类型 (首字母小写) |
字段类型父类 |
描述 |
1 |
text |
string |
文本字符串-分词-支持全文检索 |
2 |
keyword |
string |
文本字符串-不分词-不支持全文检索 |
3 |
long integer short byte double float half_float scaled_float |
numeric |
数值型 |
4 |
date |
date |
日期 |
5 |
date_nanos |
date nanoseconds |
日期-纳秒 |
6 |
boolean |
boolean |
布尔 |
7 |
binary |
binary |
二进制 |
8 |
integer_range float_range long_range double_range date_range |
range |
范围 |
9 |
object |
object |
单个json对象类型 |
10 |
nested |
nested |
Json对象数组嵌套 |
11 |
geo-point geo-shape |
geo |
地理数据类型 |
12 |
arrays |
arrays |
数组类型; 数组元素必须有相同的字段类型 |
13 |
multi-fields |
multi-fields |
多字段类型; |
Get localhost:9200/bank/_mappings
{
"bank": {
"mappings": {
"properties": {
"account_number": {
"type": "long"
},
"address": {
"type": "text",
"fields": {
"keyword": { // 子属性keyword 用于做精确匹配搜索
"type": "keyword",
"ignore_above": 256
}
}
},
"age": {
"type": "long"
},
"balance": {
"type": "long"
},
"city": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
...............
}
Put localhost:9200/my_index
{
"mappings":{
"properties":{
"age":{"type":"integer"}
, "email":{"type":"keyword"}
, "name":{"type":"text"}
}
}
}
// 响应结果
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "my_index"
}
Put localhost:9200/my_index/_mappings
{
"properties":{
"employee_id":{
"type":"keyword"
,"index":false // 该字段不被索引到
}
}
}
1)除了记录在案的情况外,现有的字段映射无法更新。
对于已经存在的映射字段,我们不能更新,无论是更新字段名,还是字段类型。
【例】 根据老索引 bank 创建新索引 newbank,并把age的字段类型从long修改为 integer ;
Put localhost:9200/newbank
{
"mappings":{
"properties": {
"account_number": {
"type": "long"
},
"address": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"balance": {
"type": "long"
},
"city": {
"type": "text"
},
"email": {
"type": "text"
},
"employer": {
"type": "text"
},
"firstname": {
"type": "text"
},
"gender": {
"type": "keyword"
},
"lastname": {
"type": "text"
},
"state": {
"type": "text"
}
}
}
}
// 创建结果
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "newbank"
}
Post localhost:9200/_reindex
{
"source":{
"index":"bank"
, "type":"account"
}
, "dest":{
"index":"newbank"
}
}
// 数据迁移结果
{
"took": 349,
"timed_out": false,
"total": 1000,
"updated": 0,
"created": 1000,
"deleted": 0,
"batches": 1,
"version_conflicts": 0,
"noops": 0,
"retries": {
"bulk": 0,
"search": 0
},
"throttled_millis": 0,
"requests_per_second": -1.0,
"throttled_until_millis": 0,
"failures": []
}