映射
映射类似于关系型数据库的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:全文检索类型
复杂数据类型
数组
ES没有专门的数组类型
在Elasticsearch中,不需要特别指定一个字段为数组类型,任何字段都可以包含数组值。
动态映射
存储一个文档时,如果没有定义索引,那么es会自动匹配字段类型从而创建映射。
静态映射
静态映射(也称为显式映射)是指在使用Elasticsearch时,用户提前手动定义索引的字段类型、属性和映射关系。