向 ES 服务器发 PUT 请求 : http://127.0.0.1:9200/shopping
含义创建一个名字是shopping的索引:
postman发送Rest风格的请求之后得到的响应信息
{
"acknowledged"【响应结果】 : true, # true 操作成功
"shards_acknowledged"【分片结果】 : 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
查看索引向 ES 服务器发送的请求路径和创建索引是一致的。但是 HTTP 方法不一致。
这里可以体会一下 RESTful 的意义,请求后,服务器响应结果如下
{
"shopping": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1644401307985",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "loWEmL2FT7amZyTbiKnzvQ",
"version": {
"created": "7060299"
},
"provided_name": "shopping"
}
}
}
}
删除索引{
“shopping”【索引名】 : {
“aliases”【别名】 : {},
“mappings”【映射】 : {},
“settings”【设置】 : {
“index”【设置 - 索引】 : {
“creation_date”【设置 - 索引 - 创建时间】 : “1614265373911”,
“number_of_shards”【设置 - 索引 - 主分片数量】 : “1”,
“number_of_replicas”【设置 - 索引 - 副分片数量】 : “1”,
“uuid”【设置 - 索引 - 唯一标识】 : “eI5wemRERTumxGCc1bAk2A”,
“version”【设置 - 索引 - 版本】 : {
“created”: “7080099”
},
“provided_name”【设置 - 索引 - 名称】 : “shopping”
}
}
}
}
在 Postman 中,向 ES 服务器发 DELETE 请求 : http://127.0.0.1:9200/shopping
文档操作 创建文档{
“acknowledged”: true
}
索引已经创建好了,接下来我们来创建文档,并添加数据。这里的文档可以
类比为关系型数据库中的表数据,添加的数据格式为 JSON 格式
在 Postman 中,向 ES 服务器发 POST 请求 :
http://127.0.0.1:9200/shopping/_doc
请求体内容为:
{
"title":"小米手机",
"category":"小米",
"images":"http://www.gulixueyuan.com/xm.jpg",
"price":3999.00
}
创建文档之后返回的响应数据
{
"_index": "shopping",
"_type": "_doc",
"_id": "JgYS3n4BBchQMTo5WfgL",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
{
"_index"【索引】 : "shopping",
"_type"【类型-文档】 : "_doc",
"_id"【唯一标识】 : "Xhsa2ncBlvF_7lxyCE9G", #可以类比为 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
查询索引中的文档信息 主键查询此处需要注意:如果增加数据时明确数据主键,那么请求方式也可以为 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": 5999.00
}
}
{
"_index"【索引】 : "shopping",
"_type"【文档类型】 : "_doc",
"_id": "1",
"_version": 2,
"_seq_no": 2,
"_primary_term": 2,
"found"【查询结果】 : true, # true 表示查找到, false 表示未查找到
"_source"【文档源信息】 : {
"title": "华为手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/hw.jpg",
"price": 5999.00
}
}
所有数据查询
_search查询全部的数据
127.0.0.1:9200/shopping/_search
返回的json响应数据
{
"took": 202,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "JgYS3n4BBchQMTo5WfgL",
"_score": 1.0,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 5999.00
}
}
]
}
}
文档修改
全局修改测试
和新增文档一样,输入相同的 URL 地址请求,如果请求体变化,会将原有的数据内容覆盖
在 Postman 中,向 ES 服务器发 POST 请求 : http://127.0.0.1:9200/shopping/_doc/1
全局修改之后文档中的信息发生了变化。
在 Postman 中,向 ES 服务器发 POST 请求 : http://127.0.0.1:9200/shopping/_update/1
将价格的信息进行局部的修改
{
"doc": {
"price":3000.00
}
}
再次进行查询可以发现指定的数据部分已经发生了改变
删除一个文档不会立即从磁盘上移除,它只是被标记成已删除(逻辑删除)。
在 Postman 中,向 ES 服务器发 DELETE 请求 : http://127.0.0.1:9200/shopping/_doc/1
返回响应的json数据如下所示
{
"_index": "shopping",
"_type": "_doc",
"_id": "JgYS3n4BBchQMTo5WfgL",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 1
}
含义说明
{
"_index": "shopping",
"_type": "_doc",
"_id": "1",
"_version": 1,
"result"【结果】 : "not_found", # not_found 表示未查找到
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 5,
"_primary_term": 2
}



