0
点赞
收藏
分享

微信扫一扫

ElasticSearch学习笔记


学习视频链接:​​https://www.bilibili.com/video/BV1hh411D7sb​​

一、Elasticsearch入门

官方地址:​​https://www.elastic.co/cn/​​

官方文档:​​https://www.elastic.co/guide/index.html​​

ElasticSearch7.8下载界面:​​https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-8-0​​

 Windows 版的 Elasticsearch 压缩包,解压即安装完毕,解压后的 Elasticsearch 的目录结构如下 :

目录 

含义

bin 

可执行脚本目录

config

配置目录

jdk

内置JDK目录

lib

类库

logs

日志目录

modules

模块目录

plugins

插件目录





解压后,进入 ​bin​ 文件目录,点击elasticsearch.bat​ 文件启动 ES 服务 。

注意: 9300 端口为 Elasticsearch 集群间组件的通信端口, 9200 端口为浏览器访问的 http协议 RESTful 端口。

打开浏览器,输入地址: http://localhost:9200,测试返回结果,返回结果如下:

{
"name": "ZHIXI",
"cluster_name": "elasticsearch",
"cluster_uuid": "jMVQWiD7RZeoyghlUYF5MQ",
"version": {
"number": "7.6.1",
"build_flavor": "default",
"build_type": "zip",
"build_hash": "aa751e09be0a5072e8570670309b1f12348f023b",
"build_date": "2020-02-29T00:15:25.529771Z",
"build_snapshot": false,
"lucene_version": "8.4.0",
"minimum_wire_compatibility_version": "6.8.0",
"minimum_index_compatibility_version": "6.0.0-beta1"
},
"tagline": "You Know, for Search"
}


1、索引的创建&查询&删除


创建


对比关系型数据库,创建索引就等同于创建数据库。

在 Postman 中,向 ES 服务器发 PUT 请求 : ​​​​​​

请求后,服务器返回响应:

ElasticSearch学习笔记_ElasticSearch


查询所有的索引


在 Postman 中,向 ES 服务器发 GET 请求 : ​​​​​​

这里请求路径中的_cat 表示查看的意思, indices 表示索引,所以整体含义就是查看当前 ES服务器中的所有索引,就好像 MySQL 中的 show tables 的感觉,服务器响应结果如下 :

health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open shopping u9hibY2NTjuhAiYeY24y4A 1 1 0 0 230b 230b


表头

含义

health

当前服务器健康状态: green(集群完整) yellow(单点正常、集群不完整) red(单点不正常)

status

索引打开、关闭状态

index

索引名

uuid

索引统一编号

pri

主分片数量

rep

副本数量

docs.count

可用文档数量

docs.delete

文档删除状态(逻辑删除)

store.size

主分片和副分片整体占空间大小

pri.store.size

主分片占空间大小




查看单个索引


{
"shopping": {//索引名
"aliases": {},//别名
"mappings": {},//映射
"settings": {//设置
"index": {//设置 - 索引
"creation_date": "1617861426847",//设置 - 索引 - 创建时间
"number_of_shards": "1",//设置 - 索引 - 主分片数量
"number_of_replicas": "1",//设置 - 索引 - 主分片数量
"uuid": "J0WlEhh4R7aDrfIc3AkwWQ",//设置 - 索引 - 主分片数量
"version": {//设置 - 索引 - 主分片数量
"created": "7080099"
},
"provided_name": "shopping"//设置 - 索引 - 主分片数量
}
}
}
}



删除索引 


在 Postman 中,向 ES 服务器发 DELETE 请求 : ​​​​​​

返回结果如下:

{
"acknowledged": true
}


再次查看所有索引,​GET http://127.0.0.1:9200/_cat/indices?v​,返回结果如下:

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size


2、文档的创建

假设索引已经创建好了,接下来我们来创建文档,并添加数据。这里的文档可以类比为关系型数据库中的表数据,添加的数据格式为 JSON 格式

在 Postman 中,向 ES 服务器发 POST 请求 :​​​​​​,请求体JSON内容为:

ElasticSearch学习笔记_服务器_02

注意,此处发送请求的方式必须为 POST,不能是 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 服务器会随机生成一个。

如果想要自定义唯一性标识,需要在创建时指定:​​​​​​,请求体JSON内容为:

{
"_index": "shopping",
"_type": "_doc",
"_id": "1",//<------------------自定义唯一性标识
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}


此处需要注意:如果增加数据时明确数据主键,那么请求方式也可以为 PUT。

ElasticSearch学习笔记_ElasticSearch_03

3、主键查询&全查询


主键查询


查看文档时,需要指明文档的唯一性标识,类似于 MySQL 中数据的主键查询

在 Postman 中,向 ES 服务器发 GET 请求 : ​​​​​​

返回结果如下:

{
"_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 请求 : ​​​​​​

返回结果如下:

{
"took": 65,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "QbWRHH8Bmr0OZbMUK5Qv",
"_score": 1.0,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "www.huawei.com",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
}
]
}
}


4、文档的全量修改 & 局部修改 & 删除


全量修改


和新增文档一样,输入相同的 URL 地址请求,如果请求体变化,会将原有的数据内容覆盖

在 Postman 中,向 ES 服务器发 POST 请求 : ​​​​​​

请求体JSON内容为:

{
"title":"一加手机",
"category":"一加",
"images":"https://www.oneplus.com/cn",
"price":1999.00
}


修改成功后,服务器响应结果:

{
"_index": "shopping",
"_type": "_doc",
"_id": "1",
"_version": 2,
"result": "updated",//<-----------updated 表示数据被更新
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}



局部修改


修改数据时,也可以只修改某一给条数据的局部信息

在 Postman 中,向 ES 服务器发 POST 请求 : ​​​​​​

请求体JSON内容为:

{
"title":"一加手机",
"category":"OnePlus",
"images":"https://www.oneplus.com/cn",
"price":5999.00
}


返回结果如下:

{
"_index": "shopping",
"_type": "_doc",
"_id": "1",
"_version": 4,
"result": "updated", ---->表示数据被更新
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 5,
"_primary_term": 2
}


在 Postman 中,向 ES 服务器发 GET请求 : ​​​​​​,查看修改内容:  

{
"_index": "shopping",
"_type": "_doc",
"_id": "1",
"_version": 4,
"_seq_no": 5,
"_primary_term": 2,
"found": true,
"_source": {
"title": "一加手机",
"category": "OnePlus",
"images": "https://www.oneplus.com/cn",
"price": 5999.00
}
}



删除


删除一个文档不会立即从磁盘上移除,它只是被标记成已删除(逻辑删除)。

在 Postman 中,向 ES 服务器发 DELETE 请求 : ​​​​​​

返回结果:

{
"_index": "shopping",
"_type": "_doc",
"_id": "2",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 6,
"_primary_term": 2
}


在 Postman 中,向 ES 服务器发 GET请求 : ​​​​​​,查看是否删除成功:  

{
"_index": "shopping",
"_type": "_doc",
"_id": "2",
"found": false
}


5、条件查询 & 分页查询 & 查询排序


条件查询


假设有以下文档内容,(在 Postman 中,向 ES 服务器发 GET请求 :​​​​​​

{
"took": 851,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "3",
"_score": 1.0,
"_source": {
"title": "红米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://www.huawei.com",
"price": 5999.00
}
}
]
}
}



带参查询  


查找category为小米的文档,在 Postman 中,向 ES 服务器发 GET请求 :​​​​​​

返回结果如下:

{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0.5753642,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "1",
"_score": 0.5753642,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "3",
"_score": 0.5753642,
"_source": {
"title": "红米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
}
]
}
}


上述为URL带参数形式查询,这很容易让不善者心怀恶意,或者参数值出现中文会出现乱码情况。为了避免这些情况,我们可用使用带JSON请求体请求进行查询。  


请求体带参查询


接下带JSON请求体,还是​查找category为小米​的文档,在 Postman 中,向 ES 服务器发 GET请求 : ​​​​​​

附带JSON体如下:

{
"query":{
"match":{
"category":"小米"
}
}
}


ElasticSearch学习笔记_服务器_04

查找所有文档的内容:GET ​​​​​​,附带JSON参数如下

{
"query":{
"match_all":{}
}
}


查询指定字段: 在 Postman 中,向 ES 服务器发 GET请求 : ​​​​​​,附带JSON体如下:

{
"query":{
"match_all":{}
},
"_source":["title"]
}



分页查询  


在 Postman 中,向 ES 服务器发 GET请求 :​​​​​​,附带JSON体如下:  

表示查询所有,从0开始,每页显示2条数据

{
"query":{
"match_all":{}
},
"from":0,
"size":2
}



查询排序  


如果你想通过排序查出价格最高的手机(降序),在 Postman 中,向 ES 服务器发 GET请求 :​​​​​​,附带JSON体如下:  

{
"query":{
"match_all":{}
},
"sort":{
"price":{
"order":"desc"
}
}
}



举报

相关推荐

0 条评论