ElasticSearch7.8.0-window版本
1、下载与安装
下载地址:Index of elasticsearch-local,下载的是window的压缩包版本。
启动之后ES会有两个端口号分别是9300(ES集群间组件通信的端口号)和9200(浏览器访问的端口号),浏览器访问结果如下:
出现代表启动成功。同时需要注意的是:ES7.8版本需要JDK1.8以上。
2、倒排索引
倒排索引和正排索引:
倒排索引(反向索引):通过value查询key
正排索引(正向索引):通过key去查询value
3、索引操作
1、创建索引:ES中的索引相当于mysql中的数据库,创建索引则就像是创建一个数据库,使用put请求方式。
http://localhost:9200/shopping(创建索引),在postman中进行请求,如果显示以下内容表示创建成功
{ "acknowledged": true, "shards_acknowledged": true, "index": "shopping" }
2、查询和删除索引
查询索引和创建索引的方式是一样的,只需要把请求方式变成get请求即可。
删除索引:需要将请求方式换成delete
4、文档操作
1、插入文档,文档相当于数据库中的行,相当于是插入行数据
http://localhost:9200/shopping/_doc 其中shopping是索引名称,_doc是插入文档的意思,还可以是_create。 { "title":"小米手机", "category":"小米", "price":3999.00 } // 插入的格式为json格式 //返回的结果 { "_index": "shopping", "_type": "_doc", "_id": "I94bVIABdrlLWOJuwTMV", id是唯一的标识 "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 2 }
id可以是自己定义,比如:http://localhost:9200/shopping/_doc/1001 ,插入文档的格式有四种:
PUT :localhost:9200/shopping/_doc/1001 POST :localhost:9200/shopping/_doc/ PUT :localhost:9200/shopping/_create/1001 POST :localhost:9200/shopping/_create/1001
2、主键查询和全部查询
主键查询路径:通过get请求-http://localhost:9200/shopping/_doc/1001
全部查询路径:通过get请求-http://localhost:9200/shopping/_saerch
3、全量和局部数据更新
全量数据更新:采用put的请求方式,http://localhost:9200/shopping/_doc/1001,将更新的数据以json的形式书写。
局部数据更新:采用post请求方式,http://localhost:9200/shopping/_update/1001,将更新其中的某个属性,请求体如下:
{ "doc":{ "title":"华为手机" } }
4、删除
请求方式转变成delete。
5、查询:通过body体进行查询(get请求)
根据字段查询:
http://localhost:9200/shopping/_search //查询的body体 { "query":{ "match":{ "category":"小米" } } }
全部查询:
http://localhost:9200/shopping/_search { "query":{ "match_all":{ } } }
条件查询:
http://localhost:9200/shopping/_search { "query":{ "match_all":{ } }, "from":0, //从第几页开始 "size":2, //每页显示几条 "_source":["title"], //控制显示的内容,如结果中只显示title内容 "sort":{ //进行排序 "price":{ "order":"asc" } } }
多条件查询:同时成立使用must,单个成立就可以使用should,范围查询使用filter
http://localhost:9200/shopping/_search { "query":{ "bool":{ "must":[ //必须成立 { //第一个匹配条件 "match":{ "category":"小米" } },{ //第二个匹配条件 "match":{ "price":3999.00 } } ], "filter":{ //范围查询 "range":{ "price":{ "gt":5000 } } } } } }
聚合查询:
http://localhost:9200/shopping/_search { "aggs":{ //聚合查询 "price_group":{ //操作名称 可以随意起名 "terms":{ //进行分组 "field":"price" //分组字段 } } }, "size":0 //只显示统计结果 不看原始数据 } // 如果使用其他聚合操作比如avg等可以通过文档查询进行相应操作
映射关系: