ElasticSearch(五):Mapping和常见字段类型
ElasticSearch(五):Mapping和常见字段类型
## 什么是Mapping
* Mapping类似数据库中的schema的定义,作用如下:
- 定义索引中的字段的名称;
- 定义字段的数据类型,例如字符串、数字、日期、布尔等;
- 对每个字段进行倒排索引的相关配置(Analyzed or Not Analyzed,Analyzer);
* Mapping 会把JSON文旦映射成Lucene所需要的扁平格式。
* 一个Mapping属于一个索引的Type:
- 每个文档都属于一个Type;
- 一个Tpye有一个Mapping定义;
- 7.0开始,不需要再Mapping定义中指定type信息;
## 字段的数据类型
* 简单类型
- Text
- Date
- Integer/Long/Floating
- Boolean
- IP4&IP6
- Keyword
* 复杂类型
- 对象类型
- 嵌套类型
* 特殊类型(地理信息)
- geo_point&geo_shape、percolator
## 什么是Dynamic Mapping
* 在写入文档的时候,如果索引不存在,则会自动创建索引;
* Dynamic Mapping机制,可以无需手动定义Mapping,ElasticSearch会自动根据文档信息,推算出字段的类型;
* 但是有时候推算的可能不对,例如地理位置信息;
* 当类型设置的不对时,会导致一些功能无法正常运行,比如范围内的Range查询;
## 类型的自动识别
JSON类型|Elasticsearch类型
---|---
字符串|匹配日期格式,设置成Date;匹配数字设置成Float或者Long,该选项默认关闭;设置为Text,并且增加keyword子字段
布尔值|Boolean
浮点数|Float
整数|Long
对象|Object
数组|由第一个非空数的类型所决定
空值|忽略
```
#写入文档,查看 Mapping
PUT mapping_test/_doc/1
{
"firstName":"Chan",
"loginDate":"2018-07-24T10:29:48.103Z",
"uid" : "123",
"isVip" : false,
"isAdmin": "true",
"age":19,
"heigh":180
}
Delete index
DELETE mapping_test
查看 Dynamic Mapping文件
GET mapping_test/_mapping
查看 Dynamic Mapping返回结果
{
"mapping_test" : {
"mappings" : {
"properties" : {
"age" : {
"type" : "long" # "age":19,设置为long
},
"firstName" : {
"type" : "text", # "firstName":"Chan",设置为Text,并且增加keyword子字段
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"heigh" : {
"type" : "long" #"heigh":180设置为long
},
"isAdmin" : {
"type" : "text", #"isAdmin": "true",设置为Text,并且增加keyword子字段
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"isVip" : {
"type" : "boolean" #"isVip" : false,设置为boolean
},
"loginDate" : {
"type" : "date" #"loginDate":"2018-07-24T10:29:48.103Z",设置为Date
},
"uid" : {
"type" : "text", # "uid" : "123",设置为Text,并且增加keyword子字段,匹配数字设置成Float或者Long,该选项默认关闭;
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
<br/>
## 能否更改 Mapping 的字段类型
分两种情况:
* 新增加字段
- Dynamic设置为true时,一旦有新增字段的文档写入,Mapping也同时被更新;
- Dynamic设置为false时,Mapping不会被更新,新增字段的数据无法被索引,但是信息会出现在_source中;
- Dynamic设置为strict时,文档写入失败;
* 对已有字段,一旦已有数据写入,就不在支持修改字段定义
- Lucene实现的倒排索引,一旦生成后,就不允许修改
- 如果希望修改字段类型,必须Reindex API,重建索引
- 如果修改了字段的数据类型,会导致已被索引的数据无法被搜索
<br/>
## 控制Dynamic Mappings
dynamic|true|false|strict
---|---|---|---
文档可索引|YES|YES|NO
字段可索引|YES|NO|NO
Mapping被更新|YES|NO|NO
<br/>
* 当dynamic被设置成false时,存在新增字段数据写入,该数据可以被索引,但新增字段被丢弃
* 当dynamic被设置成strict时,数据写入直接出错
1.默认Mapping支持dynamic,写入的文档中加入新的字段
PUT dynamic_mapping_test/_doc/1
{
"newField":"someValue"
}
2.该字段可以被搜索,数据也在_source中出现
POST dynamic_mapping_test/_search
{
"query":{
"match":{
"newField":"someValue"
}
}
}
返回结果:
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "dynamic_mapping_test",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"newField" : "someValue"
}
}
]
}
}
3.修改为dynamic false
PUT dynamic_mapping_test/_mapping
{
"dynamic": false
}
4.新增 anotherField
PUT dynamic_mapping_test/_doc/10
{
"anotherField":"someValue"
}
5.该字段不可以被搜索,因为dynamic已经被设置为false
POST dynamic_mapping_test/_search
{
"query":{
"match":{
"anotherField":"someValue"
}
}
}
返回结果:
{
"took" : 657,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
6.修改为strict
PUT dynamic_mapping_test/_mapping
{
"dynamic": "strict"
}
7.写入数据出错,HTTP Code 400
PUT dynamic_mapping_test/_doc/12
{
"lastField":"value"
}
返回结果:
{
"error": {
"root_cause": [
{
"type": "strict_dynamic_mapping_exception",
"reason": "mapping set to strict, dynamic introduction of [lastField] within [_doc] is not allowed"
}
],
"type": "strict_dynamic_mapping_exception",
"reason": "mapping set to strict, dynamic introduction of [lastField] within [_doc] is not allowed"
},
"status": 400
}
<br/>
## 如何定义一个 Mapping
PUT index_name
{
"mappings":{
"properties":{
//define your mappings here
}
}
}
* 可以参考API手册,纯手写;
* 为了减少输入的工作量,减少出错概率,可以依照以下步骤:
- 创建一个临时的index,写入一些样本数据;
- 通过访问Mapping API获取该临时文件的动态Mapping定义;
- 修改后,使用该配置创建你的索引
- 删除临时索引
<br/>
## Mapping的一些配置
* ` index`控制当前字段是否被索引,默认为`true`。如果设置成`false`,该字段不可被搜索。
* `index_options`可以控制倒排索引记录的内容,有四种不同级别的配置:
- `docs`记录 doc id
- `freqs`记录 doc id / term frequencies
- `positions`记录 doc id / term frequencies / term position
- `offects`记录 doc id / term frequencies / term position / character offects
* Text类型默认记录`positions`,其他默认为 `docs`。记录的类容越多,占用存储空间越大。
* ` null_value`控制需要对Null值实现搜索;只有Keyword类型支持设定null_value。
* ` copy_to`满足一些特定的搜索需求,` copy_to`将字段的数值拷贝到目标字段,实现类似`_all`的作用,`_all`在ES7中被` copy_to`所替代,` copy_to`的目标字段不出现在_source中。
* Elasticsearch中不提供专门的数组类型。但是任何字段,都可以包含多个相同类类型的数值。
1.设置 index 为 false
DELETE users
PUT users
{
"mappings" : {
"properties" : {
"firstName" : {
"type" : "text"
},
"lastName" : {
"type" : "text"
},
"mobile" : {
"type" : "text",
"index": false
}
}
}
}
插入数据
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: {\n "match" : {\n "mobile" : {\n "query" : "12345678",\n "operator" : "OR",\n "prefix_length" : 0,\n "max_expansions" : 50,\n "fuzzy_transpositions" : true,\n "lenient" : false,\n "zero_terms_query" : "NONE",\n "auto_generate_synonyms_phrase_query" : true,\n "boost" : 1.0\n }\n }\n}",
"index_uuid": "1oB9dwY2TPq-9QjiaMaU7g",
"index": "users"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "users",
"node": "u-4S1mfbQiuA1Bqe-wfPJQ",
"reason": {
"type": "query_shard_exception",
"reason": "failed to create query: {\n "match" : {\n "mobile" : {\n "query" : "12345678",\n "operator" : "OR",\n "prefix_length" : 0,\n "max_expansions" : 50,\n "fuzzy_transpositions" : true,\n "lenient" : false,\n "zero_terms_query" : "NONE",\n "auto_generate_synonyms_phrase_query" : true,\n "boost" : 1.0\n }\n }\n}",
"index_uuid": "1oB9dwY2TPq-9QjiaMaU7g",
"index": "users",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "Cannot search on field [mobile] since it is not indexed." #错误原因
}
}
}
]
},
"status": 400
}
设定Null_value
DELETE users
PUT users
{
"mappings" : {
"properties" : {
"firstName" : {
"type" : "text"
},
"lastName" : {
"type" : "text"
},
"mobile" : {
"type" : "keyword",
"null_value": "NULL"
}
}
}
}
插入数据
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"
}
}
}
查询返回结果:
{
"took" : 1,
"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
}
}
]
}
}
设置 Copy to
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"
}
查询方法1
GET users/_search?q=fullName:(Ruan Yiming)
查询方法2
POST users/_search
{
"query": {
"match": {
"fullName":{
"query": "Ruan Yiming",
"operator": "and"
}
}
}
}
查询返回结果:
{
"took" : 1,
"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"
}
}
]
}
}
数组类型
PUT users/_doc/1
{
"name":"twobirds",
"interests":["reading","music"]
}
GET users/_mapping
返回Mapping结果:
{
"users" : {
"mappings" : {
"properties" : {
"firstName" : {
"type" : "text",
"copy_to" : [
"fullName"
]
},
"fullName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"interests" : {
"type" : "text", #数组类型,根据数组里数据类型配置
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"lastName" : {
"type" : "text",
"copy_to" : [
"fullName"
]
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
<br/>
ElasticSearch(五):Mapping和常见字段类型的更多相关文章
- Dynamic Mapping和常见字段类型
原文:Dynamic Mapping和常见字段类型 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn. ...
- elasticsearch入门使用(二) Mapping + field type字段类型
Elasticsearch Reference [6.2] » Mapping 参考官方英文文档 https://www.elastic.co/guide/en/elasticsearch/refer ...
- ElasticSearch Mapping中的字段类型
1)string: 默认会被分词 2)数字类型主要如下几种: long:64位存储 integer:32位存储 short:16位存储 byte:8位存储 double:64位双精度存储 f ...
- 转载:Oracle常见字段类型
转载节选自:https://bbs.csdn.net/topics/220059184 数据类型 参数 描述 char(n) n=1 to 2000字节 定长字符串,n字节长,如果不指定长度,缺省为1 ...
- ElasticSearch(5)-Mapping
一.Mapping概述 映射 为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成全文本(Full-text)或精确的字符串值,Elasticsearch需要知道每个字段里面都包含了 ...
- 【转】elasticsearch中字段类型默认显示{ "foo": { "type": "text", "fields": { "keyword": {"type": "keyword", "ignore_above": 256} }
官方原文链接:https://www.elastic.co/cn/blog/strings-are-dead-long-live-strings 转载原文连接:https://segmentfault ...
- Elasticsearch支持的字段类型
es支持下列简单的字段类型: String: string Whole number: byte, short, integer, long Floating point: float, double ...
- ES系列六、ES字段类型及ES内置analyzer分析
一.背景知识 在Es中,字段的类型很关键: 在索引的时候,如果字段第一次出现,会自动识别某个类型,这种规则之前已经讲过了. 那么如果一个字段已经存在了,并且设置为某个类型.再来一条数据,字段的数据不与 ...
- ElasticStack学习(七):ElasticSearch之Mapping初探
一.Mapping的概念 1.Mapping类似于数据库中的Schema的定义,作用如下: 1)定义索引中的字段的名称: 2)定义字段的数据类型,例如字符串.数字.日期.布尔等: 3)对每个字段进行倒 ...
随机推荐
- openpyxl中遇到TypeError: 'generator' object is not subscriptable的问题和解决方案
今天在搭建驱动数据框架用到了一个叫 openpyxl的包用来解析excel数据 随后就出现了TypeError: 'generator' object is not subscriptable的bug ...
- linux netlink通信机制简介
一.什么是Netlink通信机制 Netlink套接字是用以实现用户进程与内核进程通信的一种特殊的进程间通信(IPC) ,也是网络应用程序与内核通信的最常用的接口. Netlink 是一种特殊的 s ...
- 用到的Dos命令总结 持续更新
1.xcopy命令:复制的扩展命令 常用参数:/s:复制空文件夹 不使用可能会造成文件混乱 /y忽略覆盖提示 使用/y会直接覆盖全部 例子:xcopy lark-UI\dist C:\U ...
- 基于Matlab/Simulink的模型开发(连载一)
概述 基于模型的开发将省去繁琐的代码编写步骤,只需要拖动几个模块,就像搭积木一般,轻松搭建您自己的飞控算法.飞控开发人员可以将更多的精力放在算法本身,而不需要过多关注代码实现的细节,这样将大大加快开发 ...
- SQL server数据库系统部分常用的存储过程及说明
--SQL server数据库系统常用的存储过程 exec sp_databases --能看到所有具有权限的数据库名,大小和备注 exec sp_helpdb --数据库名,大小,管理员,创建时间状 ...
- phpstorm 新加入项目的文件--全局搜索不到 ctrl + shift + R
通过文件名查找文件 ,能搜到其他的现有文件,只是新加入的文件,无法出现在搜索到的结果中 . 总不可能在搜索的关键词一直拼写错误吧 , 那能想到的只有缓存出问题了. 新加入的文件,新加入的文件.... ...
- shell数组(四)
[root@ipha-dev71- exercise_shell]# cat test.sh #!/bin/bash my_array=(a b c d) echo "第一个元素为:${my ...
- ESP32 开发之旅② Arduino For ESP32说明
授人以鱼不如授人以渔,目的不是为了教会你具体项目开发,而是学会学习的能力.希望大家分享给你周边需要的朋友或者同学,说不定大神成长之路有博哥的奠基石... QQ技术互动交流群:ESP8266&3 ...
- PHP5底层原理之变量
PHP5底层原理之变量 变量结构 zval 结构体 PHP 所有类型的变量在底层都会以 zval 结构体的形式实现 (源码文件Zend/zend.h) 源码根目录搜索 grep -rin --colo ...
- Spring Boot Security And JSON Web Token
Spring Boot Security And JSON Web Token 说明 流程说明 何时生成和使用jwt,其实我们主要是token更有意义并携带一些信息 https://github.co ...