九.全文检索ElasticSearch经典入门-ElasticSearch映射修改

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 九.全文检索ElasticSearch经典入门-ElasticSearch映射修改

前言

这篇文章的内容是ElasticSearch映射修改,写这篇文章是有水友公司里面遇到了映射修改问题,我这里做了一个整理,希望对你有所帮助。

映射修改问题

在ElasticSearch中一旦创建了映射想要进行修改是不被允许的。比如我这里有一个案例

#创建索引
PUT employee

#创建映射
POST employee/_doc/_mapping
{
   
   
  "properties":{
   
   
    "id":{
   
   
      "type":"integer"
    },
    "name":{
   
   
      "type":"keyword"
    }
  }
}

上面创建了索引employee ,同时为其创建映射,指定了id和name的类型为integer和keyword,下面尝试修改

POST employee/_doc/_mapping
{
   
   
  "properties":{
   
   
    "id":{
   
   
      "type":"long"
    },
    "name":{
   
   
      "type":"keyword"
    }
  }
}

当我尝试把id的类型由integer修改为long时,发生下面这个错误
在这里插入图片描述
说的是不能把integer修改为long,这个也很好理解,因为ES考虑到索引库已经存放了数据,如果你要修改字段类型会导致类型不兼容。

但是我们在开发中难免遇到字段类型没设定好而导致映射的修改。这里分为两种情况,一是添加字段做映射,二是修改已有字段的类型做映射。我们先说第一种。

添加映射

添加字段映射是比较方便的,因为添加不涉及到已有字段类型的修改,是可以直接添加语法如下

PUT employee/_doc/_mapping
{
  "properties":{
    "age":{
      "type":"integer"
    }
  }
}

这里使用PUT,为employee添加了一个 age,指定的类型为 integer。然后我们获取employee的映射 : GET employee/_doc/_mapping ,结果如下
在这里插入图片描述

修改映射

前面有说到,修改已有索引的映射是不被允许的,那么当我们遇到这样的需求应该怎么做呢?官方文档 是这样说明的

Updating existing field mappings
Other than where documented, existing field mappings cannot be updated. Changing the mapping would mean invalidating already indexed documents. Instead, you should create a new index with the correct mappings and reindex your data into that index. If you only wish to rename a field and not change its mappings, it may make sense to introduce an alias field

大概意思就是:现有的字段映射无法更新,更改映射将意味着使已有索引的文档无效。相反,您应该使用正确的映射创建一个新索引,并将数据重新索引到该索引中。简单理解就是让我们创建一个新的索引,做好映射,把数据重新写入到新的索引中。

第一步:创建一个新的索引和映射 , id的类型我做了修改,指定为 long

#创建映射
PUT employees

PUT employees/_doc/_mapping
{
  "properties":{
    "id":{
      "type":"long"
    },
    "name":{
      "type":"keyword"
    }
  }
}

第二步:数据的迁移,把老索引中的数据迁移到新的索引中,官方文档给出了语法:如下

POST _reindex
{
  "source": {
    "index": "employee"
  },
  "dest": {
    "index": "employees"
  }
}
  • source:是老索引
  • dest :是新的索引

如果是在7以前,老的索引和新的索引都有不同的type,那么要使用如下语法

POST _reindex
{
  "source": {
    "index": "",
    "type": ""
  },
  "dest": {
    "index": "",
    "type": ""
  }
}
相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
16小时前
|
安全 Linux 开发工具
Elasticsearch 搜索入门技术之一
Elasticsearch 搜索入门技术之一
228 1
|
16小时前
|
JSON 自然语言处理 数据库
数据库-ElasticSearch入门(索引、文档、查询)
数据库-ElasticSearch入门(索引、文档、查询)
317 0
|
16小时前
|
搜索推荐 Java 数据库
springboot集成ElasticSearch的具体操作(系统全文检索)
springboot集成ElasticSearch的具体操作(系统全文检索)
|
16小时前
|
运维 监控 Java
探索Elasticsearch在Java环境下的全文检索应用实践
【4月更文挑战第17天】本文介绍了在Java环境下使用Elasticsearch实现全文检索的步骤。首先,简述了Elasticsearch的功能和安装配置。接着,通过Maven添加`elasticsearch-rest-high-level-client`依赖,创建`RestHighLevelClient`实例连接Elasticsearch。内容包括:创建/删除索引,插入/查询文档。还探讨了高级全文检索功能、性能优化和故障排查技巧。通过Elasticsearch,开发者能高效处理非结构化数据,提升应用程序价值。
|
6月前
|
自然语言处理 关系型数据库 定位技术
分布式系列教程(35) -ElasticSearch文档映射
分布式系列教程(35) -ElasticSearch文档映射
44 0
|
16小时前
|
存储 关系型数据库 MySQL
ElasticSearch 入门
【2月更文挑战第7天】ElasticSearch 入门 简介 ElasticSearch 的基本概念 ElasticSearch 的查询流程 ElasticSearch 的更新流程
40 2
|
16小时前
|
存储 自然语言处理 搜索推荐
ElasticSearch入门篇
ElasticSearch入门篇
|
16小时前
|
自然语言处理 搜索推荐 算法
数据库-Elasticsearch进阶学习笔记(分片、映射、分词器、即时搜索、全文搜索等)
数据库-Elasticsearch进阶学习笔记(分片、映射、分词器、即时搜索、全文搜索等)
146 0
|
16小时前
|
存储 Java 网络架构
Spring Data Elasticsearch基础入门详解
Spring Data Elasticsearch基础入门详解
137 0
|
16小时前
|
存储 数据挖掘 Java
Elasticsearch基础入门与安装部署
Elasticsearch基础入门与安装部署
184 0

热门文章

最新文章