Elasticsearch

一.概念 官网文档与下载地址
elasticsearch-7.8.0-windows-x86_64(1234)
Elaticsearch简称为 ES,是一个开源的高扩展的分布式全文搜索引擎
1.正排索引与倒排索引 正排索引 根据id查询content
### 倒排索引
根据关键字查询id,再根据id查询content
2.Elasticsearch与Mysql相关数据库对比 Elasticsearch 是面向文档型数据库,一条数据在这里就是一个文档 。
Elasticsearch 7.X 中, Type 的概念已经被删除 。
二.使用 1.启动 解压windows版本的后,点击bin目录下的elasticsearch.bat启动ES服务 。
9300 端口为 Elasticsearch 集群间组件的通信端口;
9200 端口为浏览器访问的http协议 RESTful 端口 。
地址栏输入:http://localhost:9200/
{"name": "DESKTOP-MQLJB6K","cluster_name": "elasticsearch","cluster_uuid": "sga8uL5QQaSklnUXQndiqw","version": {"number": "7.8.0","build_flavor": "default","build_type": "zip","build_hash": "757314695644ea9a1dc2fecd26d1a43856725e65","build_date": "2020-06-14T19:35:50.234439Z","build_snapshot": false,"lucene_version": "8.5.1","minimum_wire_compatibility_version": "6.8.0","minimum_index_compatibility_version": "6.0.0-beta1"},"tagline": "You Know, for Search"} 2.基本请求 【PUT】创建user索引:http://localhost:9200/user {"acknowledged": true,"shards_acknowledged": true,"index": "user"} 【GET】查看所有索引:http://localhost:9200/_cat/indices?v health status indexuuidpri rep docs.count docs.deleted store.size pri.store.sizeyellow openfirstindex DP5zxFH7Q9ef7Y-qw6m4kg11104.1kb4.1kbyellow openuserFoZ9k6J3TQGVO7AnXoRhFw1100208b208b
【GET】查看指定索引:http://localhost:9200/user {"user": {"aliases": {},"mappings": {},"settings": {"index": {"creation_date": "1648169719549","number_of_shards": "1","number_of_replicas": "1","uuid": "FoZ9k6J3TQGVO7AnXoRhFw","version": {"created": "7080099"},"provided_name": "user"}}}} 【DELETE】删除索引:http://localhost:9200/user {"acknowledged": true} 【POST】向索引中创建文档并添加数据(类似于向mysql某个数据库中添加数据):http://localhost:9200/user/_doc 请求参数:
{"name":"jiang","age":25} 返回结果:
{"_index": "user","_type": "_doc","_id": "RRC7vn8BLPflpYqYAQB9","_version": 1,"result": "created","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 0,"_primary_term": 1} 【POST】向索引中创建文档并添加数据时指定id:http://localhost:9200/user/_doc/123 {"_id": "123",} 【PUT】如果增加数据时明确数据主键,请求方式也可以为 PUT:http://localhost:9200/user/_doc/123 会覆盖掉之前post请求传的数据 。
【GET】查看指定索引的指定文档(必须指定id):http://localhost:9200/user/_doc/123 {"_index": "user","_type": "_doc","_id": "123","_version": 3,"_seq_no": 3,"_primary_term": 1,"found": true,"_source": {"name": "ming","age": 55}} 【GET】查看指定索引的所有数据:http://localhost:9200/user/_search {"took": 195,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 2,"relation": "eq"},"max_score": 1.0,"hits": [{"_index": "user","_type": "_doc","_id": "RRC7vn8BLPflpYqYAQB9","_score": 1.0,"_source": {"name": "jiang","age": 25}},{"_index": "user","_type": "_doc","_id": "123","_score": 1.0,"_source": {"name": "ming","age": 55}}]}} 【POST】局部修改文档中的数据,如只修改age,不修改name:http://localhost:9200/user/_update/123 请求参数:
{"doc":{"age":56}} 返回值:
{"_index": "user","_type": "_doc","_id": "123","_version": 4,"result": "updated","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 4,"_primary_term": 1} 【DELETE】删除文档中指定id的数据(不会立即从磁盘上移除,它只是被标记成已删除):http://localhost:9200/user/_doc/123 【GET】条件查询,查询name=jiang的文档:http://localhost:9200/user/_search?q=name:jiang (将索引、_doc、id想象成表、行、字段)
原数据:
"hits": [{"_index": "user","_type": "_doc","_id": "RRC7vn8BLPflpYqYAQB9","_score": 1.0,"_source": {"name": "jiang","age": 25}},{"_index": "user","_type": "_doc","_id": "123","_score": 1.0,"_source": {"name": "mei","age": 55}},{"_index": "user","_type": "_doc","_id": "345","_score": 1.0,"_source": {"name": "ming","age": 18}},{"_index": "user","_type": "_doc","_id": "789","_score": 1.0,"_source": {"name": "shuang","age": 32}}] 返回结果:
{"took": 159,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 1,"relation": "eq"},"max_score": 1.2039728,"hits": [{"_index": "user","_type": "_doc","_id": "RRC7vn8BLPflpYqYAQB9","_score": 1.2039728,"_source": {"name": "jiang","age": 25}}]}} 【GET】请求体带参的条件查询,查询name为shuang的:http://localhost:9200/user/_search 请求参数:
{"query":{"match":{"name":"shuang"}}}查询所有:"match_all":{} 返回结果:
{"took": 17,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 1,"relation": "eq"},"max_score": 1.2039728,"hits": [{"_index": "user","_type": "_doc","_id": "789","_score": 1.2039728,"_source": {"name": "shuang","age": 32}}]}} 【GET】只查询字段name的值:http://localhost:9200/user/_search 请求参数:
{"query":{"match_all":{}},"_source":["name"]} 返回结果:
{"took": 8,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 4,"relation": "eq"},"max_score": 1.0,"hits": [{"_index": "user","_type": "_doc","_id": "RRC7vn8BLPflpYqYAQB9","_score": 1.0,"_source": {"name": "jiang"}},{"_index": "user","_type": "_doc","_id": "123","_score": 1.0,"_source": {"name": "mei"}},{"_index": "user","_type": "_doc","_id": "345","_score": 1.0,"_source": {"name": "ming"}},{"_index": "user","_type": "_doc","_id": "789","_score": 1.0,"_source": {"name": "shuang"}}]}} 【GET】分页查询,子查询前两条记录:http://localhost:9200/user/_search 请求参数:
{"query":{"match_all":{}},"from":0,"size":2} 返回结果:
{"took": 2,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 4,"relation": "eq"},"max_score": 1.0,"hits": [{"_index": "user","_type": "_doc","_id": "RRC7vn8BLPflpYqYAQB9","_score": 1.0,"_source": {"name": "jiang","age": 25}},{"_index": "user","_type": "_doc","_id": "123","_score": 1.0,"_source": {"name": "mei","age": 55}}]}} 【GET】查询排序,按照age降序排序:http://localhost:9200/user/_search 请求参数:
{"query":{"match_all":{}},"sort":{"age":{"order":"desc"}}} 返回结果:
{"took": 88,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 4,"relation": "eq"},"max_score": null,"hits": [{"_index": "user","_type": "_doc","_id": "123","_score": null,"_source": {"name": "mei","age": 55},"sort": [55]},{"_index": "user","_type": "_doc","_id": "789","_score": null,"_source": {"name": "shuang","age": 32},"sort": [32]},{"_index": "user","_type": "_doc","_id": "RRC7vn8BLPflpYqYAQB9","_score": null,"_source": {"name": "jiang","age": 25},"sort": [25]},{"_index": "user","_type": "_doc","_id": "345","_score": null,"_source": {"name": "ming","age": 18},"sort": [18]}]}} 【GET】多条件查询,查询条件是name=jiang,age=25的数据:http://localhost:9200/user/_search must 相当于 &&
请求参数:
{"query":{"bool":{"must":[{"match":{"name":"jiang"}},{"match":{"age":25}}]}}} 返回结果:
{"took": 28,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 1,"relation": "eq"},"max_score": 2.2039728,"hits": [{"_index": "user","_type": "_doc","_id": "RRC7vn8BLPflpYqYAQB9","_score": 2.2039728,"_source": {"name": "jiang","age": 25}}]}} 【GET】范围查询,查询name为jiang或者shuang,age>10的数据:http://localhost:9200/user/_search should 相当于 ||
这块查询有问题,第一个条件:name为jiang或者shuang没有生效,也不知道为什么!
请求参数:
{"query":{"bool":{"should":[{"match":{"name":"jiang"}},{"match":{"name":"shuang"}}],"filter":{"range":{"age":{"gt":10}}}}}} 返回结果:
{"took": 10,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 4,"relation": "eq"},"max_score": 1.2039728,"hits": [{"_index": "user","_type": "_doc","_id": "RRC7vn8BLPflpYqYAQB9","_score": 1.2039728,"_source": {"name": "jiang","age": 25}},{"_index": "user","_type": "_doc","_id": "789","_score": 1.2039728,"_source": {"name": "shuang","age": 32}},{"_index": "user","_type": "_doc","_id": "123","_score": 0.0,"_source": {"name": "mei","age": 55}},{"_index": "user","_type": "_doc","_id": "345","_score": 0.0,"_source": {"name": "ming","age": 18}}]}} 【GET】模糊匹配,搜索jiang shuang时,会返回name=jiang与name=shuang的数据:http://localhost:9200/user/_search 请求参数:
{"query":{"match":{"name":"jiang shuang"}}} 返回结果:
{"took": 4,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 2,"relation": "eq"},"max_score": 1.2039728,"hits": [{"_index": "user","_type": "_doc","_id": "RRC7vn8BLPflpYqYAQB9","_score": 1.2039728,"_source": {"name": "jiang","age": 25}},{"_index": "user","_type": "_doc","_id": "789","_score": 1.2039728,"_source": {"name": "shuang","age": 32}}]}} 【GET】精确匹配,再次搜索jiang shuang时:http://localhost:9200/user/_search 请求参数:
{"query":{"match_phrase":{"name":"jiang shuang"}}} 返回结果:
{"took": 21,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 0,"relation": "eq"},"max_score": null,"hits": []}} 【GET】查询后的name字段高亮显示:http://localhost:9200/user/_search 请求参数:
{"query":{"match":{"name":"jiang shuang"}},"highlight":{"fields":{"name":{}}}} 返回结果:
{"took": 119,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 2,"relation": "eq"},"max_score": 1.2039728,"hits": [{"_index": "user","_type": "_doc","_id": "RRC7vn8BLPflpYqYAQB9","_score": 1.2039728,"_source": {"name": "jiang","age": 25},"highlight": {"name": ["