Elasticsearch 与传统的 SQL数据库的一个明显的不一样点是,Elasticsearch 是一个 非结构化 的数据库,或者说是一个 无模式 的数据库。Elasticsearch 中数据最重要的三要素当属:索引、类型、文档,其中索引这个概念很是重要,咱们能够粗略地将其类比到传统SQL数据库中的 数据表。本文就从 Elasticsearch 的索引映射如何配置开始讲起。数据库
注: 本文首发于 My Personal Blog,欢迎光临 小站 !json
本文内容脑图以下:文章共1540字,阅读本文大约须要5分钟 !bash
建立索引时,能够自定义索引的结构,好比 建立一个保存用户信息数据的 users
索引,其典型的结构以下:app
id
:惟一表示符name
:姓名birthday
:出生日期hobby
:爱好为此咱们能够建立一个 json 格式的索引模式映射文件:users.jsoncurl
{
"mappings" : {
"user" : {
"properties" : {
"id" : {
"type" : "long",
"store" : "yes"
},
"name" : {
"type" : "string",
"store" : "yes",
"index" : "analyzed"
},
"birthday" : {
"type" : "date",
"store" : "yes"
},
"hobby" : {
"type" : "string",
"store" : "no",
"index" : "analyzed"
}
}
}
}
}
复制代码
上面的 json代码意义以下:工具
users
的 Indexuser
的 Typeuser
有四个 field而后咱们来执行以下命令来新建一个索引:post
curl -X PUT http://47.98.43.236:9200/users -d @users.json
复制代码
结果以下,索引 users
、类型 user
、以及 四个字段 都已经顺利插入:性能
关于字段的 可选类型,有以下几种:学习
string
:字符串number
:数字date
:日期boolean
:布尔型binary
:二进制ip
:IP地址token_count
类型关于每种类型有哪些 属性,可参考官方文档,因为内容太多,此处再也不赘述。测试
分析器是一种用于 分析数据 或者按照用户想要的方式 处理数据 的工具,对于 字符串类型 的字段,Elasticsearch 容许用户自定义分析器。
{
"settings" : {
"index" : {
"analysis" : {
"analyzer" : {
"myanalyzer" : {
"tokenizer" : "standard",
"filter" : [
"asciifolding",
"lowercase",
"myFilter"
]
}
},
"filter" : {
"myFilter" : {
"type" : "kstem"
}
}
}
}
},
"mappings" : {
"user" : {
"properties" : {
"id" : {
"type" : "long",
"store" : "yes"
},
"name" : {
"type" : "string",
"store" : "yes",
"index" : "analyzed",
"analyzer" : "myanalyzer"
},
"birthday" : {
"type" : "date",
"store" : "yes"
},
"hobby" : {
"type" : "string",
"store" : "no",
"index" : "analyzed"
}
}
}
}
}
复制代码
上述 json代码中,用户定义了一个名为 myanalyzer 的分析器,该分析器包含 一个分词器 + 三个过滤器,分别以下:
standard
asciifolding
lowercase
myFilter
(自定义过滤器,其本质是 kstem
)能够经过相似以下的 Restful接口来测试 analyze API 的工做状况:
curl -X GET 'http://47.98.43.236:9200/users/_analyze?field=user.name' -d 'Cars Trains'
复制代码
可见咱们输入的时一行字符串普通"Cars Trains"
,而输出为:car
和 train
,这说明短语 "Cars Trains"
被分红了两个词条,而后所有转为小写,最后作了词干提取的操做,由此证实咱们上面自定义的分析器已然生效了!
Elasticsearch 容许为索引模式映射文件中的不一样字段指定不一样的 类似度得分 计算模型,其用法例析以下:
"mappings" : {
"user" : {
"properties" : {
"id" : {
"type" : "long",
"store" : "yes"
},
"name" : {
"type" : "string",
"store" : "yes",
"index" : "analyzed",
"analyzer" : "myanalyzer",
"similarity" : "BM25"
},
"birthday" : {
"type" : "date",
"store" : "yes"
},
"hobby" : {
"type" : "string",
"store" : "no",
"index" : "analyzed"
}
}
}
}
复制代码
上述 json文件中,咱们为
name
字段使用了BM25
这种类似度模型,添加的方法是使用similarity
属性的键值对,这样一来 Elasticsearch 将会为name
字段使用BM25
类似度计算模型来计算类似得分。
Elasticsearch 支持为每一个字段指定信息格式,以知足经过改变字段被索引的方式来提升性能的条件。Elasticsearch 中的信息格式有以下几个:
default
:默认信息格式,其提供了实时的对存储字段和词向量的压缩pulsing
:将 重复值较少字段 的信息列表 编码为词条矩阵,可加快 该字段的查询速度direct
:该格式在读过程当中将词条加载到未经压缩而存在内存的矩阵中,该格式能够提高经常使用字段的性能,但损耗内存memory
:该格式将全部的数据写到磁盘,而后须要FST来读取词条和信息列表到内存中bloom_default
:默认信息格式的扩展,增长了把 bloom filter
写入磁盘的功能。读取时 bloom filter
被读取并存入内存,以便快速检查给定的值是否存在bloom_pulsing
:pulsing
格式的扩展,也加入 bloom filter
的支持信息格式字段(postings_format
)能够在 任何一个字段上 进行设置,配置信息格式的示例以下:
"mappings" : {
"user" : {
"properties" : {
"id" : {
"type" : "long",
"store" : "yes",
"postings_format" : "pulsing"
},
"name" : {
"type" : "string",
"store" : "yes",
"index" : "analyzed",
"analyzer" : "myanalyzer"
},
"birthday" : {
"type" : "date",
"store" : "yes"
},
"hobby" : {
"type" : "string",
"store" : "no",
"index" : "analyzed"
}
}
}
}
复制代码
在该例子之中,咱们手动配置改变了
id
字段的信息格式为pulsing
,所以可加快该字段的查询速度。
文档值 这个字段属性做用在于:其容许将给定字段的值被写入一个更高内存效率的结构,以便进行更加高效的排序和搜索。咱们一般能够将该属性加在 须要进行排序 的字段上,这样能够 提效。
其配置方式是 经过属性 doc_values_format
进行,有三种经常使用的 doc_values_format
属性值,其含义从名字中也能猜个大概:
default
:默认格式,其使用少许的内存但性能也不错disk
:将数据存入磁盘,几乎无需内存memory
:将数据存入内存举个栗子吧:
"mappings" : {
"user" : {
"properties" : {
"id" : {
"type" : "long",
"store" : "yes"
},
"name" : {
"type" : "string",
"store" : "yes",
"index" : "analyzed",
"analyzer" : "myanalyzer"
},
"birthday" : {
"type" : "date",
"store" : "yes"
},
"hobby" : {
"type" : "string",
"store" : "no",
"index" : "analyzed"
},
"age" : {
"type" : "integer",
"doc_values_format" : "memory"
}
}
}
}
复制代码
上述 json配置中,咱们给类型
user
添加了一个age
字段,假如咱们想对年龄字段进行排序,那么给该字段设置文档值格式的属性是能够提高效率的。
因为能力有限,如有错误或者不当之处,还请你们批评指正,一块儿学习交流!
可 长按 或 扫描 下面的 当心心 来订阅 CodeSheep,获取更多 务实、能看懂、可复现的 原创文 ↓↓↓