1.ElasticSearch概述
1.1 ElasticSearch是什么
Elaticsearch,简称为 ES,ES 是一个开源的高扩展的分布式全文搜索引擎,是整个Elastic Stack 技术栈的核心。它可以近乎实时的存储、检索数据;本身扩展性很好,可以扩展到上 百台服务器,处理PB级别的数据。
1.2 全文搜索引擎
全文搜索引擎指的是目前广泛应用的主流搜索引擎。它的工作原理是计算机索引程序通过扫描文章中的每一个词,对每一个词建立一个索引,指明该词在文章中出现的 次数和位置,当用户查询时,检索程序就根据事先建立的索引进行查找,并将查找的结果反 馈给用户的检索方式。这个过程类似于通过字典中的检索字表查字的过程。
2.ElasticSearch入门
2.1 ElasticSearch安装
参考这篇博客:https://blog.csdn.net/weixin_44308006/article/details/130544345?fromshare=blogdetail&sharetype=blogdetail&sharerId=130544345&sharerefer=PC&sharesource=qq_51313170&sharefrom=from_link
2.2 ElasticSearch基本操作
2.2.1 RESTful
REST 指的是一组架构约束条件和原则。满足这些约束条件和原则的应用程序或设计就 是 RESTful。Web 应用程序最重要的 REST 原则是,客户端和服务器之间的交互在请求之 间是无状态的。从客户端到服务器的每个请求都必须包含理解请求所必需的信息。如果服务 器在请求之间的任何时间点重启,客户端不会得到通知。此外,无状态请求可以由任何可用 服务器回答,这十分适合云计算之类的环境。客户端可以缓存数据以改进性能。
在服务器端,应用程序状态和功能可以分为各种资源。资源是一个有趣的概念实体,它 向客户端公开。资源的例子有:应用程序对象、数据库记录、算法等等。每个资源都使用 URI (Universal Resource Identifier) 得到一个唯一的地址。所有资源都共享统一的接口,以便在客 户端和服务器之间传输状态。使用的是标准的 HTTP 方法,比如 GET、PUT、POST 和 DELETE。
在 REST 样式的 Web 服务中,每个资源都有一个地址。资源本身都是方法调用的目 标,方法列表对所有资源都是一样的。这些方法都是标准方法,包括 HTTP GET、POST、 PUT、DELETE,还可能包括 HEAD 和 OPTIONS。简单的理解就是,如果想要访问互联 网上的资源,就必须向资源所在的服务器发出请求,请求体中必须包含资源的网络路径,以 及对资源进行的操作(增删改查)。
2.2.2 数据格式
Elasticsearch 是面向文档型数据库,一条数据在这里就是一个文档。为了方便大家理解, 我们将Elasticsearch 里存储文档数据和关系型数据库MySQL存储数据的概念进行一个类比
ES 里的Index可以看做一个库,而Types相当于表,Documents则相当于表的行。 这里Types的概念已经被逐渐弱化,Elasticsearch 6.X中,一个index下已经只能包含一个 type,Elasticsearch 7.X 中, Type 的概念已经被删除了。
2.2.3 HTTP操作
2.2.3.1 索引操作
创建索引
对比关系型数据库,创建索引就等同于创建数据库
在Postman 中,向 ES 服务器发 PUT 请求 http://127.0.0.1:9200/shopping
{"acknowledged": true, # 响应结果,true:操作成功"shards_acknowledged": true, # 分片结果,true:分片操作成功"index": "shopping" # 索引名称
}
注意:创建索引库的分片数默认1片,在7.0.0之前的ElasticSearch版本中,默认5片。
如果重复添加索引,会返回错误信息。
查看所有索引
在Postman 中,向 ES 服务器发 GET 请求 http://127.0.0.1:9200/_cat/indices?v
这里请求路径中的_cat 表示查看的意思, indices 表示索引,所以整体含义就是查看当前 ES服务器中的所有索引,就好像 MySQL 中的 show tables 的感觉,服务器响应结果如下
查看单个索引
在Postman 中,向 ES 服务器发 GET 请求 http://127.0.0.1:9200/shopping
{"shopping": { # 索引名"aliases": {}, # 别名"mappings": {}, # 映射"settings": { # 设置"index": { # 设置-索引"creation_date": "1733230880479", # 设置-索引-创建时间"number_of_shards": "1", # 设置-索引-主分片数量"number_of_replicas": "1", # 设置-索引-副分片数量"uuid": "P8N_RyNkTbmJAiBAxydpPA", # 设置-索引-唯一标识"version": { # 设置-索引-版本"created": "7080099"},"provided_name": "shopping" # 设置-索引-名称}}}
}
删除索引
在Postman 中,向 ES 服务器发 DELETE 请求 http://127.0.0.1:9200/shopping
重新访问索引时,服务器返回响应:索引不存在
2.2.3.2 文档操作
创建文档
索引已经创建好了,接下来我们来创建文档,并添加数据。这里的文档可以类比为关系型数
据库中的表数据,添加的数据格式为 JSON 格式
在Postman 中,向 ES 服务器发 POST 请求 http://127.0.0.1:9200/shopping/_doc
请求体内容为:
{"title":"小米手机","category":"小米","images":"http://www.gulixueyuan.com/xm.jpg","price":3999.00
}
注意:这里的请求方式不能用PUT,因为上述请求发出多次返回的_id不同,说明POST操作不是幂等性,然而PUT是幂等性,所以这里不能使用PUT
服务器响应结果如下:
{"_index": "shopping",//索引"_type": "_doc",//类型-文档"_id": "ANQqsHgBaKNfVnMbhZYU",//唯一标识,可以类比为 MySQL 中的主键,随机生成"_version": 1,//版本"result": "created",//结果,这里的 create 表示创建成功"_shards": {//"total": 2,//分片 - 总数"successful": 1,//分片 - 总数"failed": 0//分片 - 总数},"_seq_no": 0,"_primary_term": 1
}
上面的数据创建后,由于没有指定数据唯一性标识(ID),默认情况下, ES 服务器会随机生成一个。
如果想要自定义唯一性标识,需要在创建时指定: http://127.0.0.1:9200/shopping/_doc/1
,请求体JSON内容为:
{"title":"小米手机","category":"小米","images":"http://www.gulixueyuan.com/xm.jpg","price":3999.00
}
返回结果如下:
{"_index": "shopping","_type": "_doc","_id": "1",//<------------------自定义唯一性标识"_version": 1,"result": "created","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 1,"_primary_term": 1
}
此处需要注意:如果增加数据时明确数据主键,那么请求方式也可以为 PUT。
查看文档
查看文档时,需要指明文档的唯一性标识,类似于 MySQL 中数据的主键查询
在 Postman 中,向 ES 服务器发 GET 请求 : http://127.0.0.1:9200/shopping/_doc/1
。
返回结果如下:
{"_index": "shopping","_type": "_doc","_id": "1","_version": 1,"_seq_no": 1,"_primary_term": 1,"found": true,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999}
}
查找不存在的内容,向 ES 服务器发 GET 请求 : http://127.0.0.1:9200/shopping/_doc/1001。
返回结果如下:
查看索引下所有数据,向 ES 服务器发 GET 请求 : http://127.0.0.1:9200/shopping/_search。
返回结果如下:
{"took": 133,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 2,"relation": "eq"},"max_score": 1,"hits": [{"_index": "shopping","_type": "_doc","_id": "ANQqsHgBaKNfVnMbhZYU","_score": 1,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999}},{"_index": "shopping","_type": "_doc","_id": "1","_score": 1,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999}}]}
}
修改文档
全量修改
和新增文档一样,输入相同的 URL 地址请求,如果请求体变化,会将原有的数据内容覆盖
在 Postman 中,向 ES 服务器发 PUT 请求 : http://127.0.0.1:9200/shopping/_doc/1
请求体JSON内容为:
{"title":"华为手机","category":"华为","images":"http://www.gulixueyuan.com/hw.jpg","price":1999.00
}
返回结果如下:
{"_index": "shopping","_type": "_doc","_id": "1","_version": 4,"result": "updated","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 3,"_primary_term": 1
}
局部修改
修改数据时,也可以只修改某一给条数据的局部信息
在 Postman 中,向 ES 服务器发 POST 请求 : http://127.0.0.1:9200/shopping/_update/1
。
请求体JSON内容为:
{"doc": {"title":"小米手机","category":"小米"}
}
返回结果如下:
{"_index": "shopping","_type": "_doc","_id": "1","_version": 5,"result": "updated","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 4,"_primary_term": 2
}
在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_doc/1
,查看修改内容:
{"_index": "shopping","_type": "_doc","_id": "1","_version": 3,"_seq_no": 3,"_primary_term": 1,"found": true,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/hw.jpg","price": 1999}
}
删除文档
删除一个文档不会立即从磁盘上移除,它只是被标记成已删除(逻辑删除)。
在Postman 中,向 ES 服务器发 DELETE 请求 http://127.0.0.1:9200/shopping/_doc/1
返回结果如下:
{"_index": "shopping","_type": "_doc","_id": "1","_version": 6,"result": "deleted","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 5,"_primary_term": 2
}
如果删除一个并不存在的文档,会返回如下结果:
{"_index": "shopping","_type": "_doc","_id": "1","_version": 7,"result": "not_found","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 6,"_primary_term": 2
}
条件查询文档
URL带参查询
在 Postman 中,向 ES 服务器发 GET请求 :http://127.0.0.1:9200/shopping/_search?q=category:小米
返回结果如下:
{"took": 1,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 1,"relation": "eq"},"max_score": 1.3862942,"hits": [{"_index": "shopping","_type": "_doc","_id": "1001","_score": 1.3862942,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999.00}}]}
}
带请求体方式的查找所有内容
在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search
,附带JSON体如下:
{"query" : {"match" : {"category" : "小米"}}
}
返回结果如下:
{"took": 0,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 1,"relation": "eq"},"max_score": 1.3862942,"hits": [{"_index": "shopping","_type": "_doc","_id": "1001","_score": 1.3862942,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999.00}}]}
}
全量查询
在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search
,附带JSON体如下:
{"query" : {"match_all" : {}}
}
返回结果如下:
{"took": 0,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 1,"relation": "eq"},"max_score": 1.0,"hits": [{"_index": "shopping","_type": "_doc","_id": "1001","_score": 1.0,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999.00}}]}
}
分页查询
在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search
,附带JSON体如下:
{"query" : {"match_all" : {}},"from" : 0,"size" : 2
}
返回结果如下:
{"took": 1,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 1,"relation": "eq"},"max_score": 1.0,"hits": [{"_index": "shopping","_type": "_doc","_id": "1001","_score": 1.0,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999.00}}]}
}
查询指定字段
在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search
,附带JSON体如下:
{"query" : {"match_all" : {}},"from" : 0,"size" : 2,"_source" : ["title"]
}
返回的结果如下:
{"took": 900,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 6,"relation": "eq"},"max_score": 1.0,"hits": [{"_index": "shopping","_type": "_doc","_id": "1001","_score": 1.0,"_source": {"title": "小米手机"}},{"_index": "shopping","_type": "_doc","_id": "1002","_score": 1.0,"_source": {"title": "小米手机"}}]}
}
查询排序
如果你想通过排序查出价格最高的手机,在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search
,附带JSON体如下:
{"query" : {"match_all" : {}},"from" : 0,"size" : 2,"_source" : ["title"],"sort" : {"price" : {"order" : "desc"}}
}
返回的结果如下:
{"took": 316,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 7,"relation": "eq"},"max_score": null,"hits": [{"_index": "shopping","_type": "_doc","_id": "1007","_score": null,"_source": {"title": "小米手机"},"sort": [4999.0]},{"_index": "shopping","_type": "_doc","_id": "1001","_score": null,"_source": {"title": "小米手机"},"sort": [3999.0]}]}
}
多条件查询
假设想找出小米牌子,价格为3999元的。(must相当于数据库的&&)
在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search
,附带JSON体如下:
{"query" : {"bool" : {"must" : [{"match" : {"category" : "小米"}},{"match" : {"price" : 4999.00}}]}}
}
返回的结果如下:
{"took": 1,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 1,"relation": "eq"},"max_score": 1.1290771,"hits": [{"_index": "shopping","_type": "_doc","_id": "1007","_score": 1.1290771,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 4999.00}}]}
}
范围查询
假设想找出小米和华为的牌子,价格大于2000元的手机。
在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search
,附带JSON体如下:
{"query" : {"bool" : {"should" : [{"match" : {"category" : "小米"}},{"match" : {"category" : "华为"}}],"filter" : {"range" : {"price" : {"gt" : 4000}}}}}
}
返回的结果如下:
{"took": 1,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 3,"relation": "eq"},"max_score": 3.583519,"hits": [{"_index": "shopping","_type": "_doc","_id": "1001","_score": 3.583519,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 4999.00}},{"_index": "shopping","_type": "_doc","_id": "1007","_score": 0.6508448,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 4999.00}},{"_index": "shopping","_type": "_doc","_id": "1008","_score": 0.0,"_source": {"title": "华为手机","category": "荣耀","images": "http://www.gulixueyuan.com/xm.jpg","price": 4999.00}}]}
}