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": []
}
### 整合 Elasticsearch 到 Spring Boot 项目 为了在 Spring Boot 项目中整合 Elasticsearch,需遵循特定的步骤来设置环境并调整配置文件。当使用 `spring-boot-starter-data-elasticsearch` 启动器时,可以通过定义相应的仓库接口轻松地执行 CRUD 操作。 #### 修改 Configuration 文件 对于配置文件而言,推荐采用 YAML 格式的 `application.yml` 来替代传统的 `.properties` 文件形式[^3]。以下是针对 Elasticsearch 的基本配置实例: ```yaml spring: elasticsearch: rest: uris: http://1xx.xxx.xxx.250:9200 ``` 此片段指定了连接到 Elasticsearch 实例所需的 URI 地址。确保替换示例 IP 和端口号为实际运行 ES 节点的位置信息。 #### 创建 Repository 接口 接着,在应用程序内创建一个继承自 `ElasticsearchRepository<T,ID>` 或者更通用版本 `ElasticsearchCrudRepository<T,ID>` 的接口用于操作指定类型的文档记录[^1][^2]。这里 T 表示映射至索引内的实体类;ID 是唯一标识符的数据类型(通常是字符串或整数)。例如: ```java package com.pro.tools.elasticsearch.dao; import com.pro.tools.elasticsearch.vo.Message; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; public interface MessageRepository extends ElasticsearchRepository<Message, String> { } ``` 上述代码展示了如何声明一个名为 `MessageRepository` 的接口以处理消息类型的持久化逻辑。 #### 更新 data.elasticsearch 字段 如果要更改与 `data.elasticsearch` 相关的具体字段,则取决于具体的应用场景以及所使用的实体模型结构。通常情况下,这些属性会在对应的 Java 类里通过注解方式标注出来,比如利用 `@Document(indexName="messages")` 设置索引名称,或是借助 `@Field(type=FieldType.Text)` 定义字段特性等。因此,任何关于修改此类元数据的操作都应集中在实体 Bean 上完成而不是直接改动配置项本身。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值