目录
1.快速入门
1.1 核心概念介绍
名称 | 介绍 |
索引(index) | 一个索引相当于一个关系型数据库; |
类型(type) | 一个type相当于一类表,在7.x版本后废除。 |
映射(mapping) | mapping定义了每个字段的类型等信息,相当于关系型数据库中的表结构。 |
文档(document) | 一个document相当于关系型数据库中的一行记录。 |
字段 | 相当于关系型数据库的字段。 |
集群 | 集群由一个或多个节点组成,一个集群默认名称为elastic search |
节点 | 集群的节点,一台机器或一个进程。 |
分片和副本 |
|
1.2 RESTful风格介绍
1.2.1 概念
- RESTFUL是一种网络应用程序的设计风格和开发方式,基于HTTP,可以使用XML格式定义或JSON格式定义。
- elastic search 基于RESTful风格进行设计。
1.2.2 方法
方法名称 | 介绍 |
HEAD | 只获取某个资源的头部信息 |
PUT | 获取资源 |
POST | 创建或更新资源,用于发布资源 |
GET | 创建或更新资源,用于创建节点 |
DELETE | 删除资源 |
1.3 索引
可使用curl和postman两种方式进行操作,postman为可视化形式,较为简便。如下给出curl指令。postman的请求指令为图中标出的发送请求。
1.3.1 新增索引(PUT)
新增索引均采用PUT指令
-
请求
# curl指令 curl -X PUT "localhost:9200/lyf" # postman指令 PUT <http://localhost/9200/lyf>
-
响应
{ "acknowledged": true, "shards_acknowledged": true, "index": "lyf" }
1.3.2 获取索引(GET)
索引获取方式均采用GET指令
-
获取单个索引
指定索引名即可获取单个索引信息
-
请求
# curl指令 curl -X GET "localhost:9200/lyf" # postman指令 GET <http://localhost/9200/lyf>
-
响应
{ "lyf": { "aliases": {}, "mappings": {}, "settings": { "index": { "routing": { "allocation": { "include": { "_tier_preference": "data_content" } } }, "number_of_shards": "1", "provided_name": "lyf", "creation_date": "1630293470150", "number_of_replicas": "1", "uuid": "As3J8NWLSLO0xO2Ck-Po2w", "version": { "created": "7140099" } } } } }
-
-
批量获取索引
将指定多个索引名通过“,”分隔开,即可获取多个索引信息
-
请求
# curl指令 curl -X GET "localhost:9200/lyf,wjj" # postman指令 GET <http://localhost/9200/lyf,wjj>
-
响应
{ "lyf": { "aliases": {}, "mappings": {}, "settings": { "index": { "routing": { "allocation": { "include": { "_tier_preference": "data_content" } } }, "number_of_shards": "1", "provided_name": "lyf", "creation_date": "1630293470150", "number_of_replicas": "1", "uuid": "As3J8NWLSLO0xO2Ck-Po2w", "version": { "created": "7140099" } } } }, "wjj": { "aliases": {}, "mappings": {}, "settings": { "index": { "routing": { "allocation": { "include": { "_tier_preference": "data_content" } } }, "number_of_shards": "1", "provided_name": "wjj", "creation_date": "1630293693626", "number_of_replicas": "1", "uuid": "DOze7sXOTGOm6Y5cO_0WQQ", "version": { "created": "7140099" } } } } }
-
-
获取所有索引
第一种获取所有索引的方式为:在url后面加入“_all”指令,该方式获得索引的详细信息
-
请求
# curl指令 curl -X GET "localhost:9200/_all" # postman指令 GET <http://localhost/9200/_all>
-
响应
{ "lyf": { "aliases": {}, "mappings": {}, "settings": { "index": { "routing": { "allocation": { "include": { "_tier_preference": "data_content" } } }, "number_of_shards": "1", "provided_name": "lyf", "creation_date": "1630293470150", "number_of_replicas": "1", "uuid": "As3J8NWLSLO0xO2Ck-Po2w", "version": { "created": "7140099" } } } }, "wjj": { "aliases": {}, "mappings": {}, "settings": { "index": { "routing": { "allocation": { "include": { "_tier_preference": "data_content" } } }, "number_of_shards": "1", "provided_name": "wjj", "creation_date": "1630293693626", "number_of_replicas": "1", "uuid": "DOze7sXOTGOm6Y5cO_0WQQ", "version": { "created": "7140099" } } } } }
-
-
获取所有索引
第二中获取所有索引的方式为在url后加入“_cat/indices”指令,该方式获取索引的简略信息
-
请求
# curl指令 curl -X GET "http:localhost/9200/_cat/indices" # postman指令 GET <http://localhost/9200/_cat/indices>
-
响应
green open .geoip_databases LtZCua40SDWsH2uySlq1FQ 1 0 42 0 40.8mb 40.8mb yellow open wjj DOze7sXOTGOm6Y5cO_0WQQ 1 1 0 0 208b 208b yellow open lyf As3J8NWLSLO0xO2Ck-Po2w 1 1 0 0 208b 208b
-
1.3.3 删除索引(DELETE)
使用DELETE指令删除索引。
-
请求
# curl指令 curl -X DELETE "localhost:9200/wjj" # postman指令 DELETE <http://localhost/9200/wjj>
-
响应
{ "acknowledged": true }
1.3.4 判断索引是否存在(HEAD)
-
请求
# curl指令 # 存在的索引 curl -I DELETE "localhost:9200/lyf" # 不存在的索引 curl -I "localhost:9200/wjj" # postman指令 # 存在的索引 DELETE <http://localhost/9200/lyf> # 不存在的索引 DELETE <http://localhost/9200/wjj>
-
响应
# 存在索引响应 HTTP/1.1 200 OK # 不存在索引响应 HTTP/1.1 404 Not Found
1.3.5 关闭索引(POST)
-
请求
# curl指令 curl -X POST "localhost:9200/lyf" # postman指令 POST <http://localhost/9200/lyf>
-
响应
{ "acknowledged": true, "shards_acknowledged": true, "indices": { "lyf": { "closed": true } } }
1.3.6 开启索引(POST)
-
请求
# curl指令 curl -X POST "localhost:9200/lyf" # postman指令 POST <http://localhost/9200/lyf>
-
响应
{ "acknowledged": true, "shards_acknowledged": true }
1.4 映射
1.4.1 新增映射
映射相当于关系型数据库中的表,每一个映射中包含的字段即表的属性。具体新增操作为:
-
发送请求
PUT <http://localhost/9200/lyf/_mapping>
-
点击Body,选择row,之后选择JSON
-
填写JSON格式的映射属性
{ "properties":{ "name":{ "type":"text" }, "No":{ "type":"text" }, "age":{ "type":"keyword" }, "sex":{ "type":"keyword" }, "address":{ "type":"text" } } }
-
得到请求
{ "acknowledged": true }
1.4.2 获取映射
-
获取单一映射
-
发送GET请求
GET <http://localhost:9200/students/_mapping>
-
得到响应
{ "students": { "mappings": { "properties": { "No": { "type": "text" }, "address": { "type": "text" }, "age": { "type": "keyword" }, "name": { "type": "text" }, "sex": { "type": "keyword" } } } } }
-
-
批量获取映射
-
发送GET请求,用","将多个索引隔开
<http://localhost:9200/students,lyf/_mapping>
-
得到响应
{ "students": { "mappings": { "properties": { "No": { "type": "text" }, "address": { "type": "text" }, "age": { "type": "keyword" }, "name": { "type": "text" }, "sex": { "type": "keyword" } } } }, "lyf": { "mappings": {} } }
-
-
获取所有映射
-
发送GET请求,在根路径下输入_mapping或者输入_all/mapping
<http://localhost:9200/_mapping> <http://localhost:9200/_all/_mapping>
-
得到相应
{ "students": { "mappings": { "properties": { "No": { "type": "text" }, "address": { "type": "text" }, "age": { "type": "keyword" }, "name": { "type": "text" }, "sex": { "type": "keyword" } } } }, "lyf": { "mappings": {} } }
-
1.4.3 更新映射
映射的更新只能新增映射字段,而无法修改映射字段。即,缺少某一字段可以添加,但若字段设置错误,则无法修改。
更新映射类似新增映射,在已有映射基础上进行字段的添加。
-
发送POST请求
POST <http://localhost:9200/lyf/_mapping>
-
点击Body,选择row,之后选择JSON
-
填写JSON格式的映射属性
{ "properties":{ "name":{ "type":"text" }, "No":{ "type":"text" }, "age":{ "type":"keyword" }, "sex":{ "type":"keyword" }, "address":{ "type":"text" }, // 新增字段 "phone":{ "type":"text" } } }
-
得到响应
1.5 文档
1.5.1 新增文档(PUT、POST)
-
指定Id的方式
-
发送PUT请求
在指定索引后加入_doc/id方式,新增一个文档,例如为索引students新增一个id为1的文档
PUT <http://localhost/9200/students/_doc/1>
-
输入新增内容
输入新增文档内容,其字段应与映射的字段相对应,若索引不存在映射,则新增文档后,默认生成索引映射,且字段与新增文档中的字段一致。
{ "name":"许嵩", "No":"2020131566", "age":"30", "sex":"男", "address":"安徽省" }
-
得到相应信息
{ "_index": "students", "_type": "_doc", "_id": "1", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }
-
-
自动创建文档(不指定id)
该方式与指定id新增文档方式步骤相同,但是文档id由系统自动生成,请求方式为POST请求。
-
发送PUT请求
在指定索引后加入_doc/id方式,新增一个文档,例如为索引students新增一个id为1的文档
PUT <http://localhost/9200/students/_doc>
-
输入新增内容
输入新增文档内容,其字段应与映射的字段相对应,若索引不存在映射,则新增文档后,默认生成索引映射,且字段与新增文档中的字段一致。
{ "name":"周杰伦", "No":"2020131567", "age":"40", "sex":"男", "address":"台湾省" }
-
得到相应信息
{ "_index": "students", "_type": "_doc", // 系统生成id "_id": "NtQ8lnsBlAiSLOzxFcXJ", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 1, "_primary_term": 1 }
-
-
防止重复id覆盖文档
在新建文档的过程中,若id相同,内容不同,则之前保存的内容会被覆盖,为防止文档覆盖,在指定文档id后加入指令 ⇒ ?op_type=create,如下所示:
PUT <http://localhost:9200/students/_doc/NtQ8lnsBlAiSLOzxFcXJ?op_type=create>
1.5.2 查找文档(GET)
-
指定文档id查找
-
使用GET请求,根据指定文档id获取详细信息
GET <http://localhost:9200/students/_doc/NtQ8lnsBlAiSLOzxFcXJ>
-
得到详细信息
{ "_index": "students", "_type": "_doc", "_id": "NtQ8lnsBlAiSLOzxFcXJ", "_version": 1, "_seq_no": 1, "_primary_term": 1, "found": true, "_source": { "name": "周杰伦", "No": "2020131567", "age": "40", "sex": "男", "address": "台湾省" } }
-
-
查找多个文档(法1)
-
发送GET请求,在根路径后输入 _mget
GET <http://localhost:9200/_mget>
-
输入限定信息,以精确查找需要的信息
{ "docs":[ { // 索引 "_index":"students", // 类型 "_type":"_doc", // 文档id "_id":"1" }, { "_index":"students", "_type":"_doc", "_id":"NtQ8lnsBlAiSLOzxFcXJ" } ] }
-
得到详细信息
{ "docs": [ { "_index": "students", "_type": "_doc", "_id": "1", "_version": 1, "_seq_no": 0, "_primary_term": 1, "found": true, "_source": { "name": "许嵩", "No": "2020131566", "age": "30", "sex": "男", "address": "安徽省" } }, { "_index": "students", "_type": "_doc", "_id": "NtQ8lnsBlAiSLOzxFcXJ", "_version": 1, "_seq_no": 1, "_primary_term": 1, "found": true, "_source": { "name": "周杰伦", "No": "2020131567", "age": "40", "sex": "男", "address": "台湾省" } } ] }
还可以将索引和类型指定在GET请求中,如下:
GET <http://localhost:9200/students/_mget> // 填写内容去掉_index字段 { "docs":[ { // "_index":"students", "_type":"_doc", "_id":"1" }, { // "_index":"students", "_type":"_doc", "_id":"NtQ8lnsBlAiSLOzxFcXJ" } ] } GET <http://localhost:9200/students/_doc/_mget> // 填写内容去掉_index和_type字段 { "docs":[ { // "_index":"students", // "_type":"_doc", "_id":"1" }, { // "_index":"students", // "_type":"_doc", "_id":"NtQ8lnsBlAiSLOzxFcXJ" } ] } // 当填写内容仅有id字段时,可以简写如下: GET <http://localhost:9200/students/_doc/_mget> { "ids":["1", "NtQ8lnsBlAiSLOzxFcXJ"] } // 效果一样
-
1.5.3 更新文档
-
修改字段信息(指定文档id)
-
发送POST请求
POST <http://localhost:9200/students/_update/1>
-
填写修改信息
{ "doc":{ "name":"许嵩", "No":"2020131566", // 修改年龄 "age":"32", "sex":"男", "address":"安徽省" } }
-
得到响应信息
{ "_index": "students", "_type": "_doc", "_id": "1", "_version": 2, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 2, "_primary_term": 1 }
-
-
新增文档字段(指定文档id)
-
发送POST请求
POST <http://localhost:9200/students/_update/1>
-
填写增加字段信息
{ "script":"ctx._source.phone = [15524587588L]" }
-
得到响应信息
{ "_index": "students", "_type": "_doc", "_id": "1", "_version": 3, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 3, "_primary_term": 1 }
-
-
删除文档字段(指定文档id)
-
发送POST请求
POST <http://localhost:9200/students/_update/1>
-
填写修改信息
{ "script":"ctx._source.remove(\\"phone\\")" }
-
得到响应信息
{ "_index": "students", "_type": "_doc", "_id": "1", "_version": 4, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 4, "_primary_term": 1 }
-
-
根据文档参数值,更新指定文档字段
-
发送POST请求
POST <http://localhost:9200/students/_update/1>
-
填写修改信息
{ "script":"ctx._source.remove(\\"phone\\")" }
-
得到响应信息
{ "_index": "students", "_type": "_doc", "_id": "1", "_version": 4, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 4, "_primary_term": 1 }
-
1.5.4 删除文档
删除文档,指定文档id使用DELETE删除
// 删除文档id为1的文档
DELETE <http://localhost/9200/students/_doc/1>