ES映射知识
ES映射知识
映射
映射类似于关系型数据库的Schema(模式)。
映射来定义字段列和存储的类型等基础信息。
{
"mappings": {
"properties": {
"username": {
"type": "keyword",
"ignore_above": 256 // 忽略超过256个字符的文本
},
"email": {
"type": "keyword",
"index": false // 不索引此字段
},
"bio": {
"type": "text",
"analyzer": "standard", // 使用标准分析器
"fields": {
"raw": {
"type": "keyword" // 为文本字段添加一个原始(未分析)的keyword版本
}
}
},
"age": {
"type": "integer",
"ignore_malformed": true // 忽略格式不正确的数据
},
"signedup": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis" // 支持多种日期格式
},
"location": {
"type": "geo_point"
},
"accountstatus": {
"type": "boolean"
},
"salary": {
"type": "float",
"coerce": true // 尝试将非浮点数值转换为浮点数
},
"tags": {
"type": "text",
"fielddata": true // 允许在脚本和聚合中使用此字段
},
"attachments": {
"type": "text",
"index_options": "offsets" // 索引偏移量信息,用于高亮显示
},
"settings": {
"type": "object", // 对象类型,用于存储嵌套的JSON对象
"dynamic": "strict" // 严格模式,不允许动态添加新的字段
}
}
}
}
元字段
用于定义关于处理文档的相关元数据,各种元数据都以下划线开头
_index:文档所属索引
_id:文档Id
_source:表示文档正文的原始JSON对象
_size:source字段的大小
_routing:用于将文档路由到指定的分片
_meta:给索引加的注释信息
_tier:data_hot,data_warm,data_cold
数据类型
binary:编码为Base64字符串的二进制类型
boolean:true,false
keyword:精准匹配的keyword,const_keyword,wildcard
number:integer,long,float,double
date:date,date_nanos
text:全文检索类型
对象
在Elasticsearch(简称ES)中,object 类型用于表示JSON对象,允许你存储复杂的数据结构,如嵌套对象或关联对象。
在ES中,当你为索引创建mapping时,可以定义某个字段为 object 类型。
ES的文档就是一个JSON对象,文档就是当作对象来存储的,并且对象内部可以有嵌套对象,通过properties定义嵌套对象。properties关键字用于指定文档中每个字段的类型和其他属性。
PUT /users_index
{
"mappings": {
"properties": {
"user_name": {
"type": "text"
},
"address": {
"properties": {
"street": {
"type": "text"
},
"city": {
"type": "text"
},
"zip_code": {
"type": "keyword"
}
}
}
}
}
}
Nested对象
默认的嵌套的Object对象数组其字段被扁平化,作为外部对象的一个数组。字段互相无关,搜索会返回一个字段值,而不是对象。
就像这样:
{
“title":[invest,money],
"body":[as,investing,money],
"comments.name":[smith,john,william],
"comment.comment":[aaa,bbb,ccc],
"comment.age":[21,43,35]
}
查找名字叫smith年龄是35岁的文档,会返回comment.name=smith,comment.age=35,因为两者没有关联。
当我们的type改为nested。
内部存储:
{
“title":[invest,money],
"body":[as,investing,money],
{
{
"comments.name":[smith],
"comment.comment":[aaa],
"comment.age":[21]
},
{
"comments.name":[john],
"comment.comment":[bbb],
"comment.age":[43]
}
}
ES的普通对象的值之间没有关系,ES将其当作一个简单的字段名称和值列表,Nested会有关联。
简单来讲,Nested是Object的升级版,用来让一个对象数组以彼此独立的方式进行索引。
Join类型
数组
ES没有专门的数组类型
在Elasticsearch中,不需要特别指定一个字段为数组类型,任何字段都可以包含数组值。
动态映射
存储一个文档时,如果没有定义索引,那么es会自动匹配字段类型从而创建映射。
静态映射
静态映射(也称为显式映射)是指在使用Elasticsearch时,用户提前手动定义索引的字段类型、属性和映射关系。