Elasticsearch关键术语 系列博文 目的只用来了解概念 ; 其中 涉及到的配置和使用 是为了方便日后使用时查询的
Field字段
数据类型
简单类型
-
text
- 概念
- 一段文本 全文本 是非结构化的文本数据 ; 数组类型[]在ES属于Text类型
- 使用时机
- 当一个字段需要全文搜索时
- 作用
- 索引时 分词 (如果想精确匹配 可在mapping时将text类型设置为keyword类型 keyword不会做分词处理 用字段名.keyword搜索可精确匹配)
- 不能排序
- 也很少用于聚合
- 配置
- 不需要存储 默认存储
语法示例"index": "false"
PUT indexname { "mappings": { "blog": { "properties": { "summary": { "type": "text", "index": "false" } } } } }
- 不需要存储 默认存储
- 概念
-
keyword
- 概念
- 包括 数字 日期 一个具体的字符串
- 使用时机
- 当一个字段需要按精确值 进行过滤 排序 聚合等操作时
- 作用
- 索引时 不分词
- 配置
- 不需要存储 默认存储
"index": "true"
PUT indexname { "mappings": { "blog": { "properties": { "tags": {"type": "keyword", "index": "true"} } } } }
- 不需要存储 默认存储
- 概念
-
数字类型
- 使用时机
- 尽可能选择范围小的 索引和搜索效率越高
- 分类 (8种)
- byte – 有符号的8位整数 [-128 ~ 127]
- short – 有符号的16位整数 [-32768 ~ 32767]
- integer – 有符号的32位整数 [ − 2 31 -2^{31} −231 ~ 2 31 2^{31} 231-1]
- long – 有符号的64位整数 [ − 2 63 -2^{63} −263 ~ 2 63 2^{63} 263-1]
- float – 32位单精度浮点数
- double – 64位双精度浮点数
- 使用时机
-
date
-
概念
- 日期类型
-
默认时区
- UTC格式
-
默认日期格式 (接受数据的格式)
- strict_date_optional_timeSolr默认日期格式 或 epoch_millis时间毫秒值
# 语法示例0 : strict_date_optional_time PUT indexname/blog/1 { "pub_date": "2018-10-10T12:00:00Z" }
# 语法示例1 : strict_date_optional_time PUT indexname/blog/2 { "pub_date": "2018-10-10" }
# 语法示例2 : epoch_millis PUT website/blog/13 { "pub_date": "1589584930103" }
- strict_date_optional_timeSolr默认日期格式 或 epoch_millis时间毫秒值
-
配置
- 配置日期格式
# 添加映射 PUT indexname { "mappings": { "blog": { "properties": { "date": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" # 可以接受的日期格式 多个格式用||连接 } } } } }
- 配置日期格式
-
-
boolean
- 真值
- true
- “true”
- “on”
- “yes”
- “1”
- 假值
- false
- “false”
- “off”
- “no”
- “0”
- “”
- 0.0
- 0
- 真值
-
binary
- 使用时机
- 当字段值是Base64编码的字符串时
- 使用时机
-
ip
-
null
复杂类型
-
数组
- 使用时机
- ES没有专门的数组类型 插入数据时 数据是数组 直接使用[]即可 但[]内必须是同一种数据类型
- 注意
- 动态添加数据时 数组中的第一值的类型 就是数组的数据类型
- 数组可包含null值 空数组[]会被当作没有值的字段
- 使用时机
-
Object
- 概念
- 对象类型
- 注意
- 对象可多层嵌套
示例
# 插入对象类型的数据 PUT employee/developer/1 { "name": "ma_shoufeng", "address": { "region": "China", "location": {"province": "GuangDong", "city": "GuangZhou"} } }
# 内部存储方式 { "name": "ma_shoufeng", "address.region": "China", "address.location.province": "GuangDong", "address.location.city": "GuangZhou" }
# 映射结构 PUT employee { "mappings": { "developer": { "properties": { "name": { "type": "text", "index": "true" }, "address": { "properties": { "region": { "type": "keyword", "index": "true" }, "location": { "properties": { "province": { "type": "keyword", "index": "true" }, "city": { "type": "keyword", "index": "true" } } } } } } } } }
- 概念
-
nested
- 概念
- 嵌套类型 (是Object类型的一个特例)
- 使用时机
- 当插入数据为array类型 且需要索引和搜索时
- 本质
- 嵌套对象实质是将每个对象分离出来 作为隐藏文档进行索引
示例0 : array类型的对象 mapping中设置的数据类型是Object 搜索会有问题
```bash # 添加数据 PUT game_of_thrones/role/1 { "group": "stark", "performer": [ {"first": "John", "last": "Snow"}, {"first": "Sansa", "last": "Stark"} ] } ```
# 内部存储结构 { "group": "stark", "performer.first": [ "john", "sansa" ], "performer.last": [ "snow", "stark" ] }
```bash # 存储分析 可以看出 user.first和user.last会被平铺为多值字段 这样一来 John和Snow之间的关联性就丢失了 ```
# 查询问题分析 在查询时 可能出现John Stark的结果
示例1 : 可用nested类型解决object类型的不足
# 添加映射 PUT game_of_thrones { "mappings": { "role": { "properties": { "performer": {"type": "nested" } } } } }
- 概念
# 添加数据
PUT game_of_thrones/role/1
{
"group" : "stark",
"performer" : [
{"first": "John", "last": "Snow"},
{"first": "Sansa", "last": "Stark"}
]
}
# 查询数据
GET game_of_thrones/_search
{
"query": {
"nested": {
"path": "performer",
"query": {
"bool": {
"must": [
{ "match": { "performer.first": "John" }},
{ "match": { "performer.last": "Snow" }}
]
}
},
"inner_hits": {
"highlight": {
"fields": {"performer.first": {}}
}
}
}
}
}
参考
阮一名资料
官方文档
百度