ElasticSearch 四
字段的数据类型
简单类型
text / keyword
date
integer / floating
boolean
IPv4 & IPv6
复杂类型
对象类型 / 嵌套类型
特殊类型
geo_point & geo_shape (地理位置) / percolator
查看索引字段的类型
GET <index的名字>/_mapping
初始化数据
# dynamic mapping
PUT mapping_test/_doc/1
{
"属性名" : 值,
"属性名" : 值,
"属性名" : 值
}
dynamic mappings | “true” | “false” | “strict” |
---|---|---|---|
文档可索引 | YES | YES | NO |
字段可索引 | YES | NO | NO |
Mapping被更新 | YES | NO | NO |
// 修改dynamic mapping
PUT dynamic_mapping_test/_mapping
{
"dynamic" : false
}
# 文档可以被index,之后新增的字段不能被搜索,可通过查询索引 在source中看新增字段的值
# _mapping也无法查询
PUT dynamic_mapping_test/_mapping
{
"dynamic" : strict
}
# 不可被index,字段不能被搜索,不能新增字段,
Mapping 自定义
PUT users
{
"mappings" : {
"properties" : {
# copy_to 作用: 将值拷贝到目标字段中, 目标不需要在mapping中定义, 目标字段不会出现在_source中
# 7.0 之前类似功能是 _all, 7.0之后改为了 copy_to
"firstName" : {
"type" : "text",
"copy_to" : "fullName"
},
"lastName" : {
"type" : "text",
"copy_to" : "fullName"
},
"mobile" : {
"type" : "text",
# index 默认为true,false表示不可被搜索
"index" : false
},
"bio" : {
"type" : "text",
# index options配置, 有4种
# docs - 记录doc id
# freqs - 记录doc id 和 term frequencies
# positions - 记录 doc id / term frequencies / term position
# offsets - doc id / term frequencies / term position / character offects
# text 类型默认记录 positions,其他默认为 docs
"index_options" : "offsets"
},
# keyword 类型支持 null_value,只有keyword类型才能搜索null值
"mobile" : {
"type" : "keyword",
"null_value" : "NULL"
}
}
}
}
数组类型
ES 不提供专门的数组类型,但任何字段都可以包含多个相同类型的数值
PUT users/_doc/2
{
"name" : "twobirds",
"interests" : ["reading", "music"]
}
GET ...
{
"_source" : {
"name" : "twobirds",
"interests" : ["reading", "music"]
}
}
多字段类型
PUT users
{
"mappings" : {
"properties" : {
"company" : {
"type" : "text",
# 实现精确匹配, 增加一个keyword字段
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"comment" : {
"type" : "text",
# 使用不同的 analyzer
"fields" : {
"english_comment" : {
"type" : "text",
// 不同语言
"analyzer" : "english",
// 支持搜索和索引指定不同的 analyzer
"search_analyzer" : "english"
}
}
}
}
}
}
Exact Values vs Full Text
Exact Value: 精确查找,包括数字 / 日期 / 具体一个字符串 (例 “Apple Store”),必须指定 text 的 keyword。精确查找不需要被分词,在索引时,不需要做特殊的分词处理
Full Text: 全文本匹配,非结构化的文本数据,指 Elasticsearch的text
Index Template
帮助设定 Mappings 和Settings,并按照规则自动匹配到新的索引上
- 模板仅在索引被创建时,才会产生作用,修改模板不会影响已创建的索引
- 可以设定多个索引模板,会被 merge 到一起
- 可以指定 order 数值,控制 merging 过程
PUT /_template/template_default
{
"index_patterns" : ["*"],
"order" : 0,
"version" : 1,
"settings" : {
// 主分片 1
"number_of_shards" : 1,
// 副本分片 1
"number_of_replicas" : 1
}
}
PUT /_template/template_test
{
// 所有 test 开头的index
"index_patterns" : ["test*"],
"order" : 1,
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 2
},
"mappings" : {
// 关闭字符串转日期
"date_detection" : false,
// 开启字符串转数字
"numeric_detection" : true
}
}
Index Template 工作方式
当索引被创建时
- 应用默认的 settings 和 mappings
- 先应用order值低的设定,再应用高的设定,之前的设定会被覆盖
- 应用创建索引时,用户所指定的 settings 和 mappings 会覆盖模板中的设定
Dynamic Template
根据 ES识别数据类型,结合字段名称,动态设定index的数据类型,比如:
- 所有字符串都设定成keyword, 或关闭 keyword字段
- is 开头的字段都设成 boolean
- long_ 开头的都设成 long类型
- Dynamic Template 定义在索引的Mapping中
- Template有一个名称
- 匹配规则是一个数组
- 为匹配到字段设置Mapping
例如:
PUT my_index
{
"mappings" : {
"dynamic_templates" : [
{
"strings_as_boolean" : {
"match_mapping_type" : "string",
"match" : "is*",
"mapping" : {
"type" : "boolean"
}
}
},
{
"string_as_keywords" : {
"match_mapping_type" : "string",
"mapping" : {
"type" : "keyword"
}
}
}
]
}
}
# 查看设定
GET my_index/_mapping
# 第二例
PUT my_index
{
"mappings" : {
"dynamic_templates" : [
{
"full_name" : {
// 匹配name字段下的属性
"path_match" : "name.*",
// 忽略xxx.middle字段下的属性
"path_unmatch" : "*.middle",
// 将匹配到的值追加到新字段中
"mapping" : {
"type" : "text",
"copy_to" : "full_name"
}
}
},
]
}
}
PUT my_index/_doc/1
{
"name" : {
"first" : "John",
"middle" : "Winston",
"last" : "Lennon"
}
}
# 查看
GET my_index/_search?q=full_name:John