零基础学习Elasticsearch系列【一】
一、介绍
Elasticsearch 是一个开源的搜索引擎,建立在一个全文搜索引擎库 Apache Lucene™ 基础之上
- 一个分布式的实时文档存储,每个字段 可以被索引与搜索
- 一个分布式实时分析搜索引擎
- 能胜任上百个服务节点的扩展,并支持 PB 级别的结构化或者非结构化数据
可以通过程序与它提供的简单的 RESTful API 进行通信, 可以使用自己喜欢的编程语言充当 web 客户端,甚至可以使用命令行
二、下载启动
下载地址:www.elastic.co/cn/download…
作者选择的是7.8.0 版本,windows版本
下载后,解压即安装
目录 | 含义 |
---|---|
bin | 可执行脚本目录 |
config | 配置目录 |
jdk | 内置 JDK |
lib | 类库 |
logs | 日志目录 |
modules | 模块目录 |
plugins | 插件目录 |
解压后,进入 bin 文件目录,点击 elasticsearch.bat 文件启动 ES 服务
错误解决:
ElasticsearchException[X-Pack is not supported and Machine Learning is not available for [windows-x86]; you can use the other X-Pack features (unsupported) by setting xpack.ml.enabled: false in elasticsearch.yml]
意思是X-Pack不受我当前的win系统支持,需要在elasticsearch.yml文件中添加配置
xpack.ml.enabled: false
添加后,重新点击启动,正常启动,访问 http://127.0.0.1:9200/,可以看到版本信息
{
"name": "DESKTOP-HO4UMF5",
"cluster_name": "elasticsearch",
"cluster_uuid": "XAP5wJguQrqkL7jZo-SBjA",
"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"
}
三、 Elasticsearch 基本操作
使用postman进行接口调用,Postman 下载:www.getpostman.com/apps
1. 索引操作
-
创建索引
PUT 请求 :http://127.0.0.1:9200/shopping
请求后,服务器返回响应
{ "acknowledged": true, "shards_acknowledged": true, "index": "shopping" }
这样我们就创建了一个索引名称为 shopping 的索引,创建索引库的分片数默认 1 片,创建索引库的分片数默认 1 片
如果重复添加索引,会返回错误信息,如下:
{ "error": { "root_cause": [ { "type": "resource_already_exists_exception", "reason": "index [shopping/dOLlIokkTNqyiOWUm3f_-w] already exists", "index_uuid": "dOLlIokkTNqyiOWUm3f_-w", "index": "shopping" } ], "type": "resource_already_exists_exception", "reason": "index [shopping/dOLlIokkTNqyiOWUm3f_-w] already exists", "index_uuid": "dOLlIokkTNqyiOWUm3f_-w", "index": "shopping" }, "status": 400 }
-
查看所有索引
GET 请求 :http://127.0.0.1:9200/_cat/indices?v
这里的_cat 表示查看的意思,indices 表示索引,类似mysql的show tables
-
查看单个索引
发 GET 请求 :http://127.0.0.1:9200/shopping
服务器响应结果如下:
{ "shopping": { "aliases": {}, "mappings": {}, "settings": { "index": { "creation_date": "1693964363636", "number_of_shards": "1", "number_of_replicas": "1", "uuid": "dOLlIokkTNqyiOWUm3f_-w", "version": { "created": "7080099" }, "provided_name": "shopping" } } } }
-
删除索引
发 DELETE 请求 :http://127.0.0.1:9200/shopping
返回如下:
{ "acknowledged": true }
2. 文档操作
-
创建文档
向 ES 服务器发 POST 请求 :http://127.0.0.1:9200/shopping/_doc
请求类型为json,在postman中,选择 row,然后选择json
内容为:
{ "title":"小米手机", "category":"小米", "images":"http://www.test.com/xm.jpg", "price":3999.00 }
响应结果如下:
{ "_index": "shopping", "_type": "_doc", "_id": "x69YaIoBQf-e3AB8iXge", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }
唯一性标识(ID)在未指定的情况下,服务器会自动生成一个,如果需要指定的话,需要在请求时,在url最后拼接上,如下:
-
查看文档
向 ES 服务器发 GET 请求 :http://127.0.0.1:9200/shopping/_doc/x69YaIoBQf-e3AB8iXge
响应结果如下:
{ "_index": "shopping", "_type": "_doc", "_id": "x69YaIoBQf-e3AB8iXge", "_version": 1, "_seq_no": 0, "_primary_term": 1, "found": true, "_source": { "title": "小米手机", "category": "小米", "images": "http://www.test.com/xm.jpg", "price": 3999.00 } }
-
修改文档
向 ES 服务器发 POST 请求 :http://127.0.0.1:9200/shopping/_doc/x69YaIoBQf-e3AB8iXge
请求体内容为:
{ "title":"华为手机", "category":"华为", "images":"http://www.gulixueyuan.com/hw.jpg", "price":4999.00 }
服务器响应结果:
{ "_index": "shopping", "_type": "_doc", "_id": "x69YaIoBQf-e3AB8iXge", "_version": 2, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 1, "_primary_term": 1 }
我们将这条记录改为了华为,版本号变成了2
-
修改字段
修改数据时,也可以只修改某一个字段
发 POST 请求 :http://127.0.0.1:9200/shopping/_update/x69YaIoBQf-e3AB8iXge
请求体内容为:
{ "doc": { "price":3000.00 } }
响应结果为:
{ "_index": "shopping", "_type": "_doc", "_id": "x69YaIoBQf-e3AB8iXge", "_version": 3, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 2, "_primary_term": 1 }
根据id再次查询文档,get 请求:http://127.0.0.1:9200/shopping/_doc/x69YaIoBQf-e3AB8iXge
{ "_index": "shopping", "_type": "_doc", "_id": "x69YaIoBQf-e3AB8iXge", "_version": 3, "_seq_no": 2, "_primary_term": 1, "found": true, "_source": { "title": "华为手机", "category": "华为", "images": "http://www.test.com/hw.jpg", "price": 3000.0 } }
可以看到,价格已经变成了3000.0
-
删除文档
删除一个文档不会立即从磁盘上移除,它只是被标记成已删除(逻辑删除)
发 DELETE 请求 :http://127.0.0.1:9200/shopping/_doc/x69YaIoBQf-e3AB8iXge
响应如下:
{ "_index": "shopping", "_type": "_doc", "_id": "x69YaIoBQf-e3AB8iXge", "_version": 4, "result": "deleted", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 3, "_primary_term": 1 }
这里显示版本号为4,对文档的所有操作都会更新版本号
删除后,再次查询文档
{ "_index": "shopping", "_type": "_doc", "_id": "x69YaIoBQf-e3AB8iXge", "found": false }
-
条件删除索引
-
首先往索引中增加两条数据,id分别为1,2
put请求
{ "title":"小米手机", "category":"小米", "images":"http://www.gulixueyuan.com/xm.jpg", "price":4000.00 } { "title":"华为手机", "category":"华为", "images":"http://www.gulixueyuan.com/hw.jpg", "price":4000.00 }
-
要删除价格为4000.00的数据
-
向 ES 服务器发 POST 请求
请求:http://127.0.0.1:9200/shopping/_delete_by_query
请求体:
{ "query":{ "match":{ "price":4000.00 } } }
-
响应结果
{ "took": 366, "timed_out": false, "total": 2, "deleted": 2, "batches": 1, "version_conflicts": 0, "noops": 0, "retries": { "bulk": 0, "search": 0 }, "throttled_millis": 0, "requests_per_second": -1.0, "throttled_until_millis": 0, "failures": [] }
响应结果显示,删除了两条
四、 总结
通过以上的学习,已经掌握了es的基本操作,对索引,文档的增删改查等。这些远远不够,具体使用es的过程中会遇到各种各样的问题,只有遇到了才能提高,下一篇将介绍es的高级查询