一.简介
Elasticsearch是一个基于Lucence的搜索服务器。它提供了一个分布式的搜索引擎,基于restful web接口。广泛应用于云计算中,能够实时搜索,具有稳定,可靠,快速的特点。
二.为啥要学习Elasticsearch?
它其实也是一种数据库的优化方案,它的强大之处在于模糊查询。如果是普通数据库查询 like%xxxx%,其实是不走索引的。只要你的数据库的量很大,你的查询肯定会是秒级别的。(es搜索速度很快)
用户输入其实没那么精确或者打错,Elasticsearch却能返回用户想要的结果。(es匹配相关性较高)
还有一点是我想在目前参与的资产监控平台加一个es搜索功能,公司资产太多,资产查询模块速度较慢,值得优化。
三.下载
- Elasticsearch服务端
https://www.elastic.co/cn/downloads/elasticsearch http://127.0.0.1:9200/
- Elasticsearch客户端
- https://github.com/mobz/elasticsearch-head/ - 下载后安装依赖 npm install、npm run start; - http://127.0.0.1:9100/ - 由于ES进程跟客户端进程端口号不同,存在跨域问题,所以在- - ES配置下跨域问题(elasticsearch.yml) - 跨域配置: http.cors.enabled: true http.cors.allow-origin:
- 安装Kibana
- https://www.elastic.co/cn/downloads/kibana - http://localhost:5601/
- 安装IK分词器插件
- https://github.com/medcl/elasticsearch-analysis-ik/ - 下载解压后将目录拷贝到Elasticsearch根目录的plugins目录中
四.集群状态查看
- 查看集群健康状态
GET /_cat/health?v
- 查看节点状态
GET /_cat/nodes?v
- 查看所有索引信息
GET /_cat/indices?v
五.索引操作
- 创建索引并查看
PUT /customer GET /_cat/indices?v
- 删除索引并查看
DELETE /customer GET /_cat/indices?v
六.文档操作
- 在索引中添加文档
put /customer/doc/1 { "name":"John Doe" }
- 查看索引的文档
GET /customer/doc/1
- 修改索引的文档
POST /customer/doc/1/_update { "doc":{"name":"Jone Doe"} }
- 删除索引的文档
DELETE /customer/doc/1
- 对索引中的文档执行批量操作
POST /customer/doc/_bulk {"index":{"_id":"1"}} {"name":"John Doe"} {"index":{"_id":"2"}} {"name":"Jane Doe"}
七.数据搜索
导入数据,地址为https://github.com/macrozheng/mall-learning/blob/master/document/json/accounts.json 直接使用批量操作来导入数据
- 查看索引信息
查看索引信息 GET /_cat/indices?v
八.搜索入门
- 搜索全部
GET /bank/_search { "query":{"match_all":{}} }
- 分页搜索
GET /bank/_search { "query":{"match_all":{}}, "from":0, "size":10 }
- 搜索排序,使用sort表示,列如按balance字段降序
GET /bank/_search { "query": { "match_all": {} }, "sort": { "balance": { "order": "desc" } } }
- 搜索并返回指定字段内容
GET /bank/_search { "query": { "match_all": {} }, "_source": ["account_number", "balance"] }
九.条件搜索
- 条件搜索
GET /bank/_search { "query": { "match": { "account_number": 20 } } }
- 文本类型字段
GET /bank/_search { "query": { "match": {(对于数值类型match操作使用的是精确匹配,对于文本类型使用的是模糊匹配) "address": "mill" } }, "_source": [ "address", "account_number" ] }
- 短语匹配搜索
GET /bank/_search { "query": { "match_phrase": { "address": "mill lane" } } }
十.组合搜索
- 组合搜索 使用bool来进行组合
must表示同时满足,例如搜索address字段同时包含mill和lane的文档 GET /bank/_search { "query": { "bool": { "must": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }
- 组合搜索 should
should表示满足其中任意一个,搜索address字段中包含mill或者lane的文档 GET /bank/_search { "query": { "bool": { "should": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }
- 组合搜索 must_not 同时不满足
GET /bank/_search { "query": { "bool": { "must_not": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }
- 组合搜索 must和must_not
GET /bank/_search { "query": { "bool": { "must": [ { "match": { "age": "40" } } ], "must_not": [ { "match": { "state": "ID" } } ] } } }
十一.过滤搜索
- 搜索过滤,使用filter来表示,例如过滤出balance字段在20000~30000的文档;
GET /bank/_search { "query": { "bool": { "must": { "match_all": {} }, "filter": { "range": { "balance": { "gte": 20000, "lte": 30000 } } } } } }
十二.搜索聚合
- 对搜索进行聚合,使用aggs来表示,类似Mysql中的group by
- 例如对state字段进行聚合,统计相同state文档数量
GET /bank/_search { "size": 0, "aggs": { "group_by_state": { "terms": { "field": "state.keyword" } } } }
- 嵌套聚合,例如对state字段进行聚合,统计出相同state的文档数量,再统计出balance的平均值;
GET /bank/_search { "size": 0, "aggs": { "group_by_state": { "terms": { "field": "state.keyword" }, "aggs": { "average_balance": { "avg": { "field": "balance" } } } } } }
- 对聚合搜索的结果进行排序,例如按balance的平均值降序排列;
GET /bank/_search { "size": 0, "aggs": { "group_by_state": { "terms": { "field": "state.keyword", "order": { "average_balance": "desc" } }, "aggs": { "average_balance": { "avg": { "field": "balance" } } } } } }
- 按字段值的范围进行分段聚合,例如分段范围为age字段的[20,30] [30,40] [40,50],之后按gender统计文档个数和balance的平均值;
GET /bank/_search { "size": 0, "aggs": { "group_by_age": { "range": { "field": "age", "ranges": [ { "from": 20, "to": 30 }, { "from": 30, "to": 40 }, { "from": 40, "to": 50 } ] }, "aggs": { "group_by_gender": { "terms": { "field": "gender.keyword" }, "aggs": { "average_balance": { "avg": { "field": "balance" } } } } } } } }