5.elasticsearch映射操作(创建|修改|删除)及字段类型

【README】

1.本文介绍了 elasticsearch 映射的操作方式,包括映射创建,修改,删除;

2.映射定义:映射是定义文档及其包含的字段如何存储和索引的过程。 例如,使用映射来定义:

  • ① 哪些字符串字段应该被作为全文检索字段;
  • ② 哪些字段包含 数字,日期,及地理位置;
  • ③ 日期格式化;
  • ④ 自定义规则控制动态增加字段的映射;

小结:

  • 映射是用来定义一个文档,以及它所包含的属性,是如何存储和索引的

3.索引类型被移除

  • 6.0之前: index/type/document
  • 6.0之后: 移除了type 类型;

4.本文部分内容总结自:

Mapping | Elasticsearch Guide [7.2] | Elastic


【1】elasticsearch映射字段类型

序号

字段类型

(首字母小写)

字段类型父类

描述

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

多字段类型;


【2】_mapping 映射 api

【2.1】查询映射详情

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
                        }
                    }
                },
...............
}

【2.2】创建索引,指定映射

Put localhost:9200/my_index
{
    "mappings":{
        "properties":{
            "age":{"type":"integer"}
            , "email":{"type":"keyword"}
            , "name":{"type":"text"}
        }
    }
}

// 响应结果 
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "my_index"
}

【2.3】修改映射,添加新字段

Put localhost:9200/my_index/_mappings 
{
   "properties":{
       "employee_id":{
           "type":"keyword"
           ,"index":false  // 该字段不被索引到 
        }
   }
}

【3】修改索引映射与数据迁移

1)除了记录在案的情况外,现有的字段映射无法更新

  • 更改映射意味着使已索引的文档无效。
  • 相反,您应该使用正确的映射创建一个新索引,并将您的数据重新索引到该新索引中(数据迁移)。
  • 如果您只想重命名字段而不更改其映射,那么引入别名(alias)字段可能是有意义的。

【3.1】不能直接修改映射字段(新增字段是可以的)

对于已经存在的映射字段,我们不能更新,无论是更新字段名,还是字段类型。

  • 更新必须创建新的索引,然后进行数据迁移
  • 注意: 添加字段是可以的;

【3.2】修改映射字段的方法(间接)

  • 步骤1:根据老索引创建新的索引;
  • 步骤2:然后进行数据迁移;

【例】 根据老索引 bank 创建新索引 newbank,并把age的字段类型从long修改为 integer ;

步骤1)创建新索引 newbank  

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"
}

步骤2)迁移数据-reindex

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": []
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值