此博客用于个人学习,来源于网上,对知识点进行一个整理。
1. Elasticaearch 介绍:
用户访问我们的首页,一般都会直接搜索来寻找自己想要购买的商品。而商品的数量非常多,而且分类繁杂。如何能正确的显示出用户想要的商品,并进行合理的过滤,尽快促成交易,是搜索系统要研究的核心。面对这样复杂的搜索业务和数据量,使用传统数据库搜索就显得力不从心,一般我们都会使用全文检索技术,比如今天讲到的:Elasticsearch。
1.1 简介:
Elaticsearch,简称为 es, es 是一个开源的高扩展的分布式全文检索引擎,它可以近乎实时的存储、检索数据;本 身扩展性很好,可以扩展到上百台服务器,处理 PB 级别的数据。es 也使用 Java 开发并使用 Lucene 作为其核心来实现所有索引和搜索的功能,但是它的目的是通过简单的 RESTful API 来隐藏 Lucene 的复杂性,从而让全文搜索变得简单。
1.2 特点:
Elasticsearch具备以下特点:
- 分布式,无需人工搭建集群(solr 就需要人为配置,使用 Zookeeper 作为注册中心)
- Restful 风格,一切 API 都遵循 Rest 原则,容易上手
- 近实时搜索,数据更新在 Elasticsearch 中几乎是完全同步的
1.3 kibana:
Kibana 是一个基于 Node.js 的 Elasticsearch 索引库数据统计工具,可以利用 Elasticsearch 的聚合功能,生成各种图表,如柱形图,线状图,饼图等。而且还提供了操作 Elasticsearch 索引数据的控制台,并且提供了一定的API 提示。
2. 操作索引:
2.1 基本概念:
Elasticsearch 也是基于 Lucene 的全文检索库,本质也是存储数据,很多概念与 MySQL 类似的。
Elasticsearch | MySQL |
---|---|
索引(indices) | 数据库(Databases) |
类型(type) | 数据表(Table) |
文档(Document) | 行(Row) |
字段(Field) | 列(Columns) |
详细说明:
概念 | 说明 |
---|---|
索引库(indices) | indices是index的复数,代表许多的索引, |
类型(type) | 类型是模拟mysql中的table概念,一个索引库下可以有不同类型的索引,比如商品索引,订单索引,其数据格式不同。不过这会导致索引库混乱,因此未来版本中会移除这个概念 |
文档(document) | 存入索引库原始的数据。比如每一条商品信息,就是一个文档 |
字段(field) | 文档中的属性 |
映射配置(mappings) | 字段的数据类型、属性、是否索引、是否存储等特性 |
在 Elasticsearch 也有一些集群相关的概念:
- 索引集(Indices,index 的复数):逻辑上的完整索引
- 分片(shard):数据拆分后的各个部分
- 副本(replica):每个分片的复制
要注意的是:Elasticsearch 本身就是分布式的,因此即便你只有一个节点,Elasticsearch 默认也会对你的数据进行分片和副本操作,当你向集群添加新数据时,数据也会在新加入的节点中进行平衡。
2.2 创建索引:
Elasticsearch 采用 Rest 风格 API,因此其 API 就是一次 http 请求。
创建索引的请求格式:
-
请求方式:PUT
-
请求路径:/索引库名
-
请求参数:json 格式:
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
}
}
-
settings:索引库的设置
- number_of_shards:分片数量
- number_of_replicas:副本数量
kibana 的控制台,可以对 http 请求进行简化,语法:PUT /索引库名
2.3 查看索引设置:
Get 请求可以帮我们查看索引信息,语法:
GET /索引库名
也可以使用*来查询所有索引库配置。
2.4 删除索引:
删除索引使用 DELETE 请求,语法:
DELETE /索引库名
2.5 映射配置:
索引有了,接下来肯定是添加数据。但是,在添加数据之前必须定义映射。
映射是定义文档的过程,文档包含哪些字段,这些字段是否保存,是否索引,是否分词等。
1)创建映射字段:
语法:
PUT /索引库名/_mapping/类型名称
{
"properties": {
"字段名": {
"type": "类型",
"index": true,
"store": true,
"analyzer": "分词器"
}
}
}
- 类型名称:就是前面将的 type 的概念,类似于数据库中的不同表
- 字段名:任意填写 ,可以指定许多属性,例如:
- type:类型,可以是 text、long、short、date、integer、object 等
- index:是否索引,默认为 true
- store:是否存储,默认为 false
- analyzer:分词器,这里的 ik_max_word 即使用 ik 分词器
例如:
PUT heima/_mapping/goods
{
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
"images": {
"type": "keyword",
"index": "false"
},
"price": {
"type": "float"
}
}
}
2)查看映射关系:
语法:
GET /索引库名/_mapping
3)字段属性详解:
-
type:
Elasticsearch 中支持的数据类型非常丰富:
其中几个关键的:
-
String 类型,又分两种:
- text:可分词,不可参与聚合
- keyword:不可分词,数据会作为完整字段进行匹配,可以参与聚合
-
Numerical:数值类型,分两类
- 基本数据类型:long、interger、short、byte、double、float、half_float
- 浮点数的高精度类型:scaled_float
- 需要指定一个精度因子,比如10或100。elasticsearch 会把真实值乘以这个因子后存储,取出时再还原
-
Date:日期类型
elasticsearch 可以对日期格式化为字符串存储,但是建议存储为毫秒值,存储为 long,节省空间
-
index:
index 影响字段的索引情况。
- true:字段会被索引,则可以用来进行搜索。默认值就是 true
- false:字段不会被索引,不能用来搜索
index 的默认值就是 true,也就是说不进行任何配置,所有字段都会被索引,但是有些字段是我们不希望被索引的,比如商品的图片信息,就需要手动设置 index 为 false。
- store:
是否将数据进行额外存储。
在 Elasticsearch 中,即便 store 设置为 false,也可以搜索到结果。原因是 Elasticsearch 在创建文档索引时,会将文档中的原始数据备份,保存到一个叫做 _source 的属性中。而且我们可以通过过滤 _source 来选择哪些要显示,哪些不显示。
而如果设置 store 为 true,就会在 _source 以外额外存储一份数据,多余,因此一般我们都会将 store 设置为 false,事实上,store的默认值就是false。
2.6 新增数据:
1)随机生成 id:
通过 POST 请求,可以向一个已经存在的索引库中添加数据。
语法:
POST /索引库名/类型名
{
"key":"value"
}
示例:
POST /heima/goods/{
"title":"小米手机",
"images":"http://image.leyou.com/12479122.jpg",
"price":2699.00
}
通过 kibana 查看数据:
get _search
{
"query":{
"match_all":{}
}
}
{
"_index": "heima",
"_type": "goods",
"_id": "r9c1KGMBIhaxtY5rlRKv",
"_version": 1,
"_score": 1,
"_source": {
"title": "小米手机",
"images": "http://image.leyou.com/12479122.jpg",
"price": 2699
}
}
- _source :源文档信息,所有的数据都在里面
- _id :这条文档的唯一标示,与文档自己的 id 字段没有关联
2)自定义 id:
如果想要自己新增的时候指定 id,可以这么做:
POST /索引库名/类型/id值
{
...
}
示例:
POST /heima/goods/2
{
"title":"大米手机",
"images":"http://image.leyou.com/12479122.jpg",
"price":2899.00
}
得到的数据:
{
"_index": "heima",
"_type": "goods",
"_id": "2",
"_score": 1,
"_source": {
"title": "大米手机",
"images": "http://image.leyou.com/12479122.jpg",
"price": 2899
}
}
3)智能判断:
事实上 Elasticsearch 非常智能,你不需要给索引库设置任何 mapping 映射,它也可以根据你输入的数据来判断类型,动态添加数据映射。
POST /heima/goods/3
{
"title":"超米手机",
"images":"http://image.leyou.com/12479122.jpg",
"price":2899.00,
"stock": 200,
"saleable":true
}
额外添加了 stock 库存,和 saleable 是否上架两个字段,再看下索引库的映射关系:
{
"heima": {
"mappings": {
"goods": {
"properties": {
"images": {
"type": "keyword",
"index": false
},
"price": {
"type": "float"
},
"saleable": {
"type": "boolean"
},
"stock": {
"type": "long"
},
"title": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
}
}
stock 和 saleable 都被成功映射了。
2.7 修改数据:
把新增的请求方式改为 PUT,就是修改了,修改必须指定 id
- id 对应文档存在,则修改
- id 对应文档不存在,则新增
2.8 删除数据:
删除使用 DELETE 请求,同样,需要根据 id 进行删除,语法:
DELETE /索引库名/类型名/id值
3. 查询:
3.1 基本查询:
基本语法:
GET /索引库名/_search
{
"query":{
"查询类型":{
"查询条件":"查询条件值"
}
}
}
这里的 query 代表一个查询对象,里面可以有不同的查询属性
- 查询类型:
- 例如: match_all ,match,term,range 等等
- 查询条件:查询条件会根据类型的不同,写法也有差异
1)查询所有(match_all):
示例:
GET /heima/_search
{
"query":{
"match_all": {}
}
}
- query :代表查询对象
- match_all :代表查询所有
结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "heima",
"_type": "goods",
"_id": "2",
"_score": 1,
"_source": {
"title": "大米手机",
"images": "http://image.leyou.com/12479122.jpg",
"price": 2899
}
},
{
"_index": "heima",
"_type": "goods",
"_id": "r9c1KGMBIhaxtY5rlRKv",
"_score": 1,
"_source": {
"title": "小米手机",
"images": "http://image.leyou.com/12479122.jpg",
"price": 2699
}
}
]
}
}
- took:查询花费时间,单位是毫秒
- time_out:是否超时
- _shards:分片信息
- hits:搜索结果总览对象
- total:搜索到的总条数
- max_score:所有结果中文档得分的最高分
- hits:搜索结果的文档对象数组,每个元素是一条搜索到的文档信息
- _index:索引库
- _type:文档类型
- _id:文档 id
- _score:文档得分
- _source:文档的源数据
2)匹配查询(match):
- or 关系
match 类型查询,会把查询条件进行分词,然后进行查询,多个词条之间是 or 的关系
GET /heima/_search
{
"query":{
"match":{
"title":"小米电视"
}
}
}
结果:
"hits": {
"total": 2,
"max_score": 0.6931472,
"hits": [
{
"_index": "heima",
"_type": "goods",
"_id": "tmUBomQB_mwm6wH_EC1-",
"_score": 0.6931472,
"_source": {
"title": "小米手机",
"images": "http://image.leyou.com/12479122.jpg",
"price": 2699
}
},
{
"_index": "heima",
"_type": "goods",
"_id": "3",
"_score": 0.5753642,
"_source": {
"title": "小米电视4A",
"images": "http://image.leyou.com/12479122.jpg",
"price": 3899
}
}
]
}
在上面的案例中,不仅会查询到电视,而且与小米相关的都会查询到,多个词之间是 or 的关系。
- and 关系
某些情况下,我们需要更精确查找,我们希望这个关系变成 and ,可以这样做:
GET /heima/_search
{
"query":{
"match": {
"title": {
"query": "小米电视",
"operator": "and"
}
}
}
}
结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.5753642,
"hits": [
{
"_index": "heima",
"_type": "goods",
"_id": "3",
"_score": 0.5753642,
"_source": {
"title": "小米电视4A",
"images": "http://image.leyou.com/12479122.jpg",
"price": 3899
}
}
]
}
}
本例中,只有同时包含小米和电视的词条才会被搜索到。
- or 和 and 之间
在 or 与 and 间二选一有点过于非黑即白。 如果用户给定的条件分词后有 5 个查询词项,想查找只包含其中 4 个词的文档,该如何处理?将 operator 操作符参数设置成 and 只会将此文档排除。有时候这正是我们期望的,但在全文搜索的大多数应用场景下,我们既想包含那些可能相关的文档,同时又排除那些不太相关的。换句话说,我们想要处于中间某种结果。
match 查询支持 minimum_should_match 最小匹配参数, 这让我们可以指定必须匹配的词项数用来表示一个文档是否相关。我们可以将其设置为某个具体数字,更常用的做法是将其设置为一个百分数,因为我们无法控制用户搜索时输入的单词数量:
GET /heima/_search
{
"query":{
"match":{
"title":{
"query":"小米曲面电视",
"minimum_should_match": "75%"
}
}
}
}
本例中,搜索语句可以分为3个词,如果使用 and 关系,需要同时满足3个词才会被搜索到。这里我们采用最小品牌数:75%,那么也就是说只要匹配到总词条数量的75%即可,这里3*75% 约等于2。所以只要包含2个词条就算满足条件了。
3)多字段查询(multi_match):
multi_match 与 match 类似,不同的是它可以在多个字段中查询
GET /heima/_search
{
"query":{
"multi_match": {
"query": "小米",
"fields": [ "title", "subTitle" ]
}
}
}
4)词条匹配(term):
term 查询被用于精确值匹配,这些精确值可能是数字、时间、布尔或者那些未分词的字符串
GET /heima/_search
{
"query":{
"term":{
"price":2699.00
}
}
}
结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "heima",
"_type": "goods",
"_id": "r9c1KGMBIhaxtY5rlRKv",
"_score": 1,
"_source": {
"title": "小米手机",
"images": "http://image.leyou.com/12479122.jpg",
"price": 2699
}
}
]
}
}
5)多词条精确匹配(terms):
terms 查询和 term 查询一样,但它允许指定多值进行匹配。如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件:
GET /heima/_search
{
"query":{
"terms":{
"price":[2699.00,2899.00,3899.00]
}
}
}
结果:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "heima",
"_type": "goods",
"_id": "2",
"_score": 1,
"_source": {
"title": "大米手机",
"images": "http://image.leyou.com/12479122.jpg",
"price": 2899
}
},
{
"_index": "heima",
"_type": "goods",
"_id": "r9c1KGMBIhaxtY5rlRKv",
"_score": 1,
"_source": {
"title": "小米手机",
"images": "http://image.leyou.com/12479122.jpg",
"price": 2699
}
},
{
"_index": "heima",
"_type": "goods",
"_id": "3",
"_score": 1,
"_source": {
"title": "小米电视4A",
"images": "http://image.leyou.com/12479122.jpg",
"price": 3899
}
}
]
}
}
3.2 结果过滤:
默认情况下,elasticsearch 在搜索的结果中,会把文档中保存在 _source 的所有字段都返回。如果我们只想获取其中的部分字段,我们可以添加 _source 的过滤。
1)直接指定字段:
示例:
GET /heima/_search
{
"_source": ["title","price"],
"query": {
"term": {
"price": 2699
}
}
}
返回的结果:
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "heima",
"_type": "goods",
"_id": "r9c1KGMBIhaxtY5rlRKv",
"_score": 1,
"_source": {
"price": 2699,
"title": "小米手机"
}
}
]
}
}
2)指定 includes 和 excludes:
也可以通过:
- includes:来指定想要显示的字段
- excludes:来指定不想要显示的字段
二者都是可选的,以下两种情况都是一样的:
GET /heima/_search
{
"_source": {
"includes":["title","price"]
},
"query": {
"term": {
"price": 2699
}
}
}
GET /heima/_search
{
"_source": {
"excludes": ["images"]
},
"query": {
"term": {
"price": 2699
}
}
}
3.3 高级查询:
1)布尔组合(bool):
bool 把各种其它查询通过 must(与)、must_not(非)、should(或)的方式进行组合
GET /heima/_search
{
"query":{
"bool":{
"must": { "match": { "title": "大米" }},
"must_not": { "match": { "title": "电视" }},
"should": { "match": { "title": "手机" }}
}
}
}
2)范围查询(range):
range 查询找出那些落在指定区间内的数字或者时间
GET /heima/_search
{
"query":{
"range": {
"price": {
"gte": 1000.0,
"lt": 2800.00
}
}
}
}
range 查询允许以下字符:
操作符 | 说明 |
---|---|
gt | 大于 |
gte | 大于等于 |
lt | 小于 |
lte | 小于等于 |
3)模糊查询(fuzzy):
fuzzy 查询是 term 查询的模糊等价。它允许用户搜索词条与实际词条的拼写出现偏差,但是偏差的编辑距离不得超过2,可以通过 fuzziness 来指定允许的编辑距离:
GET /heima/_search
{
"query": {
"fuzzy": {
"title": {
"value":"appla",
"fuzziness":1
}
}
}
}
3.4 过滤(filter):
1)条件查询中进行过滤:
所有的查询都会影响到文档的评分及排名。如果我们需要在查询结果中进行过滤,并且不希望过滤条件影响评分,那么就不要把过滤条件作为查询条件来用。而是使用 filter 方式:
GET /heima/_search
{
"query":{
"bool":{
"must":{ "match": { "title": "小米手机" }},
"filter":{
"range":{"price":{"gt":2000.00,"lt":3800.00}}
}
}
}
}
注意:filter 中还可以再次进行 bool 组合条件过滤。
2)无查询条件,直接过滤
如果一次查询只有过滤,没有查询条件,不希望进行评分,我们可以使用 constant_score 取代只有 filter 语句的 bool 查询。
GET /heima/_search
{
"query":{
"constant_score": {
"filter": {
"range":{"price":{"gt":2000.00,"lt":3000.00}}
}
}
}
3.5 排序:
1)单子段排序:
sort 可以让我们按照不同的字段进行排序,并且通过 order 指定排序的方式
GET /heima/_search
{
"query": {
"match": {
"title": "小米手机"
}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
2)多字段排序:
假定我们想要结合使用 price 和 _score(得分) 进行查询,并且匹配的结果首先按照价格排序,然后按照相关性得分排序:
GET /goods/_search
{
"query":{
"bool":{
"must":{ "match": { "title": "小米手机" }},
"filter":{
"range":{"price":{"gt":200000,"lt":300000}}
}
}
},
"sort": [
{ "price": { "order": "desc" }},
{ "_score": { "order": "desc" }}
]
}