7.Mapping详解
1.什么是Mapping
1.Mapping 类似数据库中的schema的定义,作用如下
- 定义索引中的字段的名称
- 定义字段的数据类型,例如字符串,数字,布尔…
- 字段,倒排索引的相关配置(Analyzed or Not Analyzed,Analyzer)
2.Mapping会把JSON文档映射成Lucene所需要的扁平格式
3.一个Mapping属于一个索引的Type
- 每个文档都属于一个Type
- 一个Type有一个Mapping定义
- 7.0开始不需要在Mapping定义中指定type信息
1.1 字段的数据类型
- 简单类型
- Text/Keyword
- Date
- Integer/Floating
- Boolean
- IPv4 & IPv6
- 复杂类型-对象和嵌套对象
- 对象类型/嵌套类型
- 特殊类型
- geo_point & geo_shape / percolator
2 什么是Dynamic Mapping
在写入文档时候,如果索引不存在,会自动创建索引
Dynamic Mapping的机制,使得我们无需手动定义Mappings. Elasticsearch 会自动根据文档信息,推算出字段的类型
但是有时候会推算的不对,例如地理位置信息推算成text
当类型如果设置不对时,会导致一些功能无法正常运行,例如Range查询
//写入文档,查看 MappingPUT mapping_test/_doc/1{"firstName":"Chan","lastName": "Jackie","loginDate":"2018-07-24T10:29:48.103Z"}// 查看mapping GET mapping_test/_mapping// 返回结果{"mapping_test" : {"mappings" : {"properties" : {"firstName" : {"type" : "text", // 类型 每一个 text 都有 keyword"fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}},"lastName" : {"type" : "text", //类型"fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}},"loginDate" : {"type" : "date" //日期类型}}}}}DELETE mapping_test
2.1 类型的自动识别
JSON类型 | Elasticsearch类型 |
---|---|
字符串 | 匹配日期格式,设置成Date |
字符串 | 配置数字设置为float或者long,该选项默认关闭 |
字符串 | 设置为Text ,并且增加keyword子字段 |
布尔值 | boolean |
浮点数 | float |
整数 | long |
对象 | Object |
数组 | 由第一个非空数值的类型所决定 |
空值 | 忽略 |
//dynamic mapping,推断字段的类型
PUT mapping_test/_doc/1
{"uid" : "123","isVip" : false,"isAdmin": "true","age":19,"heigh":180
}
GET mapping_test/_mapping
# 返回结果{"mapping_test" : {"mappings" : {"properties" : {"age" : {"type" : "long"},"heigh" : {"type" : "long"},"isAdmin" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}},"isVip" : {"type" : "boolean"},"uid" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}}}}}
}
2.2 能否更改Mapping的字段类型
两种情况
- 新增加字段
- Dynamic 设为true时,一旦有新增字段的文档写入,Mapping也同时被更新
- Dynamic 设为false,Mapping不会被更新,新增字段的数据无法被索引
- Dynamic 设置成Strict ,文档写入失败
- 对已有字段,一旦已经有数据写入,就不再支持修改字段定义
- Lucene 实现的倒排索引,一旦生成后,就不允许修改
- 如果希望改变字段类型,必须 Reindex API 重建索引
- 如果修改了字段的数据类型,会导致已被索引的属于无法被搜索
- 但是如果是增加了新的字段,就不会有这样的影响
2.3 控制Dynamic Mappings
当dynamic 被设置成false时候,存在新增字段的数据写入,该数据可以被索引,但是新增字段被丢弃
当设置成Strict模式时候,数据直接写入直接出错
//默认Mapping支持dynamic,写入的文档中加入新的字段PUT dynamic_mapping_test/_doc/1{"newField":"someValue"}//该字段可以被搜索,数据也在_source中出现POST dynamic_mapping_test/_search{"query":{"match":{"newField":"someValue"}}}//修改为dynamic falsePUT dynamic_mapping_test/_mapping{"dynamic": false}//新增 anotherFieldPUT dynamic_mapping_test/_doc/10{"anotherField":"someValue"}//该字段不可以被搜索,应为dynamic已经被设置为falsePOST dynamic_mapping_test/_search{"query":{"match":{"anotherField":"someValue"}}}get dynamic_mapping_test/_doc/10//修改为strictPUT dynamic_mapping_test/_mapping{"dynamic": "strict"}//写入数据出错,HTTP Code 400PUT dynamic_mapping_test/_doc/12{"lastField":"value"}DELETE dynamic_mapping_test
3 显示定义一个Mapping
一些建议
3.1 Index
Index 控制当前字段是否被索引,默认是true,如果设置为false,该字段不可被索引
设置索引
PUT users
{"mappings" : {"properties" : {"firstName" : {"type" : "text"},"lastName" : {"type" : "text"},"mobile" : {"type" : "text","index": false // 设置mobile 字段不可被索引}}}
}
插入数据
PUT users/_doc/1
{"firstName":"Ruan","lastName": "Yiming","mobile": "12345678"
}
搜索字段
POST /users/_search
{"query": {"match": {"mobile":"12345678"}}
}
返回结果
"error": {"root_cause": [{"type": "query_shard_exception","reason": "failed to create query:
3.2 Index Options
- 四种不同级别的Index Options 配置,可以控制倒排索引记录的内容
- docs --记录 doc id
- freqs – 记录 doc id , term frequencies
- posititions --记录 doc id ,term frequencies, term position
- offsets --记录 doc id,term frequencies,term posistion,character offects
- Text 类型默认记录 positions,其他默认为docs
- 记录内容越多,占用空间越大
定义mapping
PUT my_index{"mappings": {"properties": {"text": {"type": "text","index_options": "offsets"}}}}
插入数据
PUT /my_index/_doc/1{"text": "Quick brown fox"}
search
GET /my_index/_search'{"query": {"match": {"text": "brown fox"}},"highlight": {"fields": {"text": {} }}}
3.3 null_value
一个字段是null值则不能被索引或搜索。当字段设置为null(或空数组)时,它被视为该字段没有值。
null_value参数允许您使用指定的值替换null,以便可以对其进行索引和搜索
定义mapping
DELETE users
PUT users
{"mappings" : {"properties" : {"firstName" : {"type" : "text"},"lastName" : {"type" : "text"},"mobile" : {"type" : "keyword","null_value": "NULL_test"}}}
}
插入值
PUT users/_doc/1
{"firstName":"Ruan","lastName": "Yiming","mobile": null
}
PUT users/_doc/2
{"firstName":"Ruan2","lastName": "Yiming2"
搜索
}
GET users/_search
{"query": {"match": {"mobile":"NULL_test"}}
}
返回值
{"took" : 25,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 1,"relation" : "eq"},"max_score" : 0.2876821,"hits" : [{"_index" : "users","_type" : "_doc","_id" : "1","_score" : 0.2876821,"_source" : {"firstName" : "Ruan","lastName" : "Yiming","mobile" : null // 可以被索引 但是id=2的没有不能被搜索到}}]}
}
3.4 copy_to
copy_to将字段的数值拷贝到目标字段,实现类似 _all的作用
copy_to的目标字段不出现在_source中,可以满足一些特定的搜索需求
定义mapping
DELETE users
PUT users
{"mappings": {"properties": {"firstName":{"type": "text","copy_to": "fullName"},"lastName":{"type": "text","copy_to": "fullName"}}}
}
插入值
PUT users/_doc/1
{"firstName":"Ruan","lastName": "Yiming"
}
搜索
GET users/_search?q=fullName:(Ruan Yiming)POST users/_search
{"query": {"match": {"fullName":{"query": "Ruan Yiming","operator": "and"}}}
}
返回结果
{"took" : 16,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 1,"relation" : "eq"},"max_score" : 0.5753642,"hits" : [{"_index" : "users","_type" : "_doc","_id" : "1","_score" : 0.5753642,"_source" : {"firstName" : "Ruan","lastName" : "Yiming"}}]}
}
更多的参数请参考
Elasticsearch
4 数组类型
Elasticsearch 中不提供专门的数组类型。但是任何字段,都可以包含多个相同多个相同类类型的数值
//数组类型PUT users/_doc/1
{"name":"onebird","interests":"reading"
}PUT users/_doc/1
{"name":"twobirds","interests":["reading","music"]
}
POST users/_search
{"query": {"match_all": {}}
}
GET users/_mapping
7.Mapping详解相关推荐
- mapping 详解1(mapping type)
映射(mapping) 映射是定义一个文档以及其所包含的字段如何被存储和索引的方法. 例如,用映射来定义以下内容: 哪些 string 类型的 field 应当被当成当成 full-text 字段 哪 ...
- Elasticsearch Mapping 详解
1. Mapping(映射) Mapping 是定义文档及其包含的字段是如何存储和索引的过程 例如,我们用映射来定义: 哪些字符串字段应该被当做全文字段 哪些字段包含数字.日期或地理位置 是否应该将 ...
- python中mapping_python-学习-ORM中遇到的 mapping 详解并再总结字典dict
python里的字典dict(mapping)如何使用 Python字典的构造函数有三个,dict().dict(**args).dict(mapping),其中第一个.第二个构造函数比较好理解也比较 ...
- Elasticsearch 5.4 Mapping详解
为什么80%的码农都做不了架构师?>>> 前言 一Field datatype字段数据类型 1string类型 2 text类型 3 keyword类型 4 数字类型 5 Ob ...
- Elasticsearch之Mapping设置详解
Elasticsearch之Mapping详解 什么是Mapping? 字段的数据类型 Dynamic Mapping ES类型自动识别机制 更新mapping 定义Mapping 1.控制字段是否被 ...
- IP组播---IGMP、MLD、IGMP SSM Mapping、IGMP Snooping详解
简介 Internet Group Management Protocol 互联网组管理协议,负责IPv4组播成员管理的协议 在接收者主机和组播路由器之间交互IGMP报文实现组成员的管理功能,建立.维 ...
- 【linux】top命令详解
1.参数详解 $ top -helpprocps-ng version 3.3.9 Usage:top -hv | -bcHiOSs -d secs -n max -u|U user -p pid(s ...
- spring boot 实战 / 可执行war启动参数详解
概述 上一篇文章<spring boot 实战 / mvn spring-boot:run 参数详解>主要讲解了spring boot 项目基于maven插件启动过程中借助profil ...
- 处理器拦截器(HandlerInterceptor)详解
处理器拦截器(HandlerInterceptor)详解 编程界的小学生 关注 2017.04.06 15:19* 字数 881 阅读 657评论 0喜欢 4 简介 SpringWebMVC的处理器拦 ...
最新文章
- C语言:随笔9--链表
- 说实话,DataGrip真得牛逼,只是你不会用而已~
- 绘制ROC 曲线 计算 AUC PR曲线(精准率 召回率)示例
- 静态程序分析chapter3 - 数据流分析详述(Reaching Definitions、Live Variables、Available Expressions Analysis)
- Android 基础教程之-------Android ProgressBar的使用!
- 组合模式_设计模式结构性:组合模式(CompositePattern)
- 中小企业项目的痛VS感人IT团队
- SQL Server 2016的新功能–临时数据表
- 中国碳纤维复合加热元件行业市场供需与战略研究报告
- 【论文分享】ACL 2020 社交网络谣言判别中可解释性相关研究
- windows环境上robotframework环境搭建
- maven 手动安装 ojdbc7
- 企业BI项目蓝图规划建设方案
- 《Search to Distill: Pearls are Everywhere but not the Eyes》论文阅读
- PD协议快充,多方兼容,这款小方糖Mini充电器实测确实很强悍
- 扇贝编程python认知课_扇贝编程-人人能学会的python课 in de App Store
- 背完这10个经典面试答案,80%的面试都能喜提offer
- 科技作者吴军:不用低效率的算法做事情
- EOS智能合约开发系列(19): 合约应当开源
- 萧伯纳学演讲不怕出丑
热门文章
- Edify Script (Android Shell)定制Twrp刷机包
- a该内存不能为“read”或“written解决方案
- html5 safari浏览器 全屏显示 隐藏工具条,HTML5全屏API不IPhone SE Safari浏览器工作,也...
- 结构变异SV的鉴定--smartie-sv与bayestyper
- android 摄像头 ip,摄像头已连接,但没有发生任何事情,openCV-IP摄像头Android
- 河北计算机软件职业技术学院,河北软件职业技术学院
- Vue前端模板框架--vue-admin-template
- A段架构设计_隽语集(IT+設計思考_1601)
- slot卡槽封装公共组件
- Python3与SQLServer、Oracle、MySql的连接方法