项目中经常出现的问题,例如添加字段、修改字段,那原先的索引规则就要跟着改变,最好是一开始就给索引一个别名,修改字段时新增映射,然后将笔名指向新的映射,当然需要将之前的索引搬迁到新的映射当中。

1、获取映射信息(例如索引库是db_student)

GET  http://localhost:/db_student/_mapping

这时可以看到返回结果:

{
"db_student": {
"mappings": {
"properties": {
"chinese": {
"type": "integer",
"store": true
},
"class": {
"type": "integer",
"store": true
},
"english": {
"type": "integer",
"store": true
},
"math": {
"type": "integer",
"store": true
},
"name": {
"type": "text",
"store": true
},
"school": {
"type": "text",
"store": true,
"analyzer": "ik_max_word"
}
}
}
}
}

这正是上一篇我们设置的映射规则,现在我们想要增加一列 desc ,用来保存学生的自我介绍介绍信息,首先,我们可以为 索引库 db_student 取一个别名  student 。

2、为旧索引库   db_student  起一个别名  student

PUT   http://localhost:/db_student/_alias/student

此时,再查一下就索引库有哪些别名:

GET  http://localhost:/db_student/_alias

呈现的效果是:

{
"db_student": {
"aliases": {
"student": {}
}
}
}

此时,别名已经生效。

3、建立新的索引库  db_student_v1,在原索引库的基础上加上 desc 这一新的字段,并且使用 ik 分词

PUT  http://localhost:/db_student_v1
{
"mappings": {
"properties": {
"class": {
"type": "integer",
"store": true,
"index":true
},
"chinese": {
"type": "integer",
"store": true,
"index":true
},
"english": {
"type": "integer",
"store": true,
"index":true
},
"math": {
"type": "integer",
"store": true,
"index":true
},
"name": {
"type": "text",
"store": true,
"index":true
},
"school": {
"type": "text",
"store": true,
"index":true,
"analyzer":"ik_max_word"
},
"desc": {
"type": "text",
"store": true,
"index":true,
"analyzer":"ik_max_word"
}
}
}
}

4、数据搬迁,现在将索引库 db_student  的数据搬到新的索引库  db_student_v1

POST  http://localhost:/_reindex
{
"conflicts": "proceed",
"source": {
"index": "db_student"
},
"dest": {
"index": "db_student_v1",
"op_type": "create",
"version_type": "external"
}
}

5、更改别名,将旧版索引库 db_student 的别名去除,将别名指向新版的索引库 db_student_v1

去除旧版索引库 db_student 的别名:

POST  http://localhost:/_aliases
{
"actions": [
{
"remove": {
"index": "db_student",
"alias": "student"
}
}
]
}

为新版索引库 db_student_v1 加上别名:

POST  http://localhost:/_aliases
{
"actions": [
{ "add": {
"index": "db_student_v1",
"alias": "student"
}
}
]
}

此时就可以统一按照别名 student 来进行上一篇的所有查询动作。

Elasticsearch第四篇:索引别名、添加或修改映射规则的更多相关文章

  1. elasticsearch 第四篇(API约定)

    对多个indices进行操作 es中大多resetapi支持请求多个index, 例如”test1,test2,test3”,index也可以使用通配符, 例如”test*“, 还可以使用+,-来包含 ...

  2. MySQL-第四篇索引

    1.创建索引的作用 创建索引的唯一作用就是加速对表的查询.索引通过使用快速路径访问方法来快速定位数据,从而减少了磁盘的I/O. 2.索引和表一样也是数据库中的一种对象,但它必须从属于某张表,不能独立存 ...

  3. Lucene索引维护(添加、修改、删除)

    1. Field域属性分类 添加文档的时候,我们文档当中包含多个域,那么域的类型是我们自定义的,上个案例使用的TextField域,那么这个域他会自动分词,然后存储            我们要根据数 ...

  4. elasticsearch系列二:索引详解(快速入门、索引管理、映射详解、索引别名)

    一.快速入门 1. 查看集群的健康状况 http://localhost:9200/_cat http://localhost:9200/_cat/health?v 说明:v是用来要求在结果中返回表头 ...

  5. elasticsearch最全详细使用教程:入门、索引管理、映射详解、索引别名、分词器、文档管理、路由、搜索详解

    一.快速入门1. 查看集群的健康状况http://localhost:9200/_cat http://localhost:9200/_cat/health?v 说明:v是用来要求在结果中返回表头 状 ...

  6. ElasticSearch入门 第四篇:使用C#添加和更新文档

    这是ElasticSearch 2.4 版本系列的第四篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...

  7. elasticsearch 5.x 系列之七 基于索引别名的零停机升级服务

    一,写在前面的话,elasticsearch 建立索引时的Mapping 设置 建议你在设计索引的初期,就把索引的各个字段设计好,因为,elasticsearch 的各个字段,定义好类型后,就无法进行 ...

  8. Elasticsearch 通关教程(三): 索引别名Aliases问题

    业务问题 业务需求是不断变化迭代的,也许我们之前写的某个业务逻辑在下个版本就变化了,我们可能需要修改原来的设计,例如数据库可能需要添加一个字段或删减一个字段,而在搜索中也会发生这件事,即使你认为现在的 ...

  9. ElasticSearch查询 第四篇:匹配查询(Match)

    <ElasticSearch查询>目录导航: ElasticSearch查询 第一篇:搜索API ElasticSearch查询 第二篇:文档更新 ElasticSearch查询 第三篇: ...

随机推荐

  1. 太实用了!自己动手写软件——SSH、FTP和SQL server的密码破解

    我们的密码破解工具一共分为如下六个部分,前面四个部分我们都有在之前的文章中介绍过了 用户图形界面——GUI编程 密码字典获取——Excel文件读取 数据库类——MySQL.Oracle和SQL ser ...

  2. 如何看待HTTP/3

    前言 HTTP/2 相比于 HTTP/1.1,可以说是大幅度提高了网页的性能,只需要升级到该协议就可以减少很多之前需要做的性能优化工作,当然兼容问题以及如何优雅降级应该是国内还不普遍使用的原因之一. ...

  3. Pull后产生多余的log(Merge branch 'master' of ...)

    第一步: git reset --hard 73d0d18425ae55195068d39b3304303ac43b521a 第二步: git push -f origin feature/PAC_1 ...

  4. consul++ansible+shell批量下发注册node_exporter

    --日期:2020年7月21日 --作者:飞翔的小胖猪  文档功能说明: 文档通过ansible+shell+consul的方式实现批量下发安装Linux操作系统监控的node_exporter软件, ...

  5. 题解 洛谷 P6640 【[BJOI2020] 封印】

    设\(lenth_i\)为\(s\)在\(i\)位置的前缀的后缀为\(t\)的一个子串的最长长度,即为从\(i\)位置开始往前和\(t\)的最长公共子串长度.其可以通过对\(t\)建后缀自动机,然后让 ...

  6. Java Web(1)-JavaScript

    一.JavaScript 和 html 代码的结合方式 1. 第一种方式 只需要在 head 标签中,或者在 body 标签中, 使用 script 标签 来书写 JavaScript 代码 < ...

  7. 关于Type-C扩展坞干扰路由器交换机的解决方案

    近期看到网友反馈Type-C扩展坞干扰交换机的问题,具体表现为USB Type-C扩展坞在同时插上网线和PD充电的情况下,引起路由器或交换机死机,导致局域网断开的情况.经实测分析,原因为部分电脑默认设 ...

  8. http连接,缓存,cookie,重定向,代理

    早期的HTTP协议使用短连接,收到响应后就立即关闭连接,效率很低:   HTTP/1.1默认启用长连接,在一个连接上收发多个请求响应,提高了传输效率:   服务器会发送“Connection:     ...

  9. Linux企业运维人员最常用命令汇总

    本文目录 线上查询及帮助命令 文件和目录操作命令 查看文件及内容处理命令 文件压缩及解压缩命令 信息显示命令 搜索文件命令 用户管理命令 基础网络操作命令 深入网络操作命令 有关磁盘与文件系统的命令 ...

  10. 各版本arm-gcc区别与安装【转】

    转自:https://www.jianshu.com/p/fd0103d59d8e arm-linux-gcc.arm-none-eabi-gcc.arm-eabi-gcc.arm-none-linu ...