1 介绍
主要介绍索引请求的基础API操作,使用postman进行请求,接口请求的前缀地址统一为elasticsearch 部署IP地址+端口号(例如 http://192.168.51.4:9200 。下方提供postman测试使用的接口地址JSON导出文件:
postman 接口集合下载地址 https://download.csdn.net/download/qq_15769939/15492332
2 文档基础操作
首先创建一个索引 (doc_api_demo),用来做文档API的测试。
2.1 创建文档
请求方式 | 接口地址 | 备注 |
---|---|---|
POST | /doc_api_demo/_doc/1 | doc_api_demo:索引名称 1:索引在ES的ID |
传递的JSON参数
{
"id": 1001,
"name": "auskat-1",
"desc": "tic is a good man 1",
"create_date": "2019-12-24"
}
{
"id": 1002,
"name": "auskat-2",
"desc": "tic 是个好人",
"create_date": "2019-12-25"
}
{
"id": 1003,
"name": "auskat-3",
"desc": "tic 要成为一个好人",
"create_date": "2020-12-26"
}
{
"id": 1004,
"name": "auskat-5",
"desc": "tic 真是一个好人",
"create_date": "2020-12-27"
}
请求结果
{
"_index": "doc_api_demo",
"_type": "_doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
这个ID属性并不是这条记录的ID,而是这条索引记录的ID,如果不设置,会默认生成一个唯一的字符串ID。实际的使用过程中,一般与数据的唯一ID保持一致。
- 如果索引没有手动建立mappings,那么当插入文档数据的时候,会根据文档类型自动设置属性类型。这个就是es的动态映射,帮我们在index索引库中去建立数据结构的相关配置信息。
- “fileds”:{“type”:“keyword”} 对一个字段设置多种索引模式,使用text类型做全文检索,也可使用keyword类型做聚合和排序。
- “ignore_above”:256 设置字段索引和存储的长度最大值,超过则会被忽略。
2.2 文档修改
2.2.1 局部修改
请求方式 | 接口地址 | 备注 |
---|---|---|
POST | /doc_api_demo/_doc/1004/_update | doc_api_demo:索引名称 1004:索引的ID |
传递JSON数据
{
"doc": {
"name": "我是tic 我要单独改名为 ttd"
}
}
请求结果
{
"_index": "doc_api_demo",
"_type": "_doc",
"_id": "1004",
"_version": 3,
"result": "updated",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 6,
"_primary_term": 1
}
2.2.2 全量替换
请求方式 | 接口地址 | 备注 |
---|---|---|
PUT | /doc_api_demo/_doc/1004 | doc_api_demo:索引名称 1004:索引的ID |
传递JSON数据
{
"id": 1004,
"name": "tic tic tic",
"desc": "tic 真的要变成一个好人了",
"create_date": "2020-12-28"
}
请求结果
{
"_index": "doc_api_demo",
"_type": "_doc",
"_id": "1004",
"_version": 4,
"result": "updated",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 7,
"_primary_term": 1
}
_version 代表文档的版本号,没修改一次都会发生累加改变。
2.3 文档查询
2.3.1 单个文档查询
请求方式 | 接口地址 | 备注 |
---|---|---|
GET | /doc_api_demo/_doc/1004 | doc_api_demo:索引名称 1004:索引的ID |
请求结果
{
"_index": "doc_api_demo",
"_type": "_doc",
"_id": "1004",
"_version": 4,
"_seq_no": 7,
"_primary_term": 1,
"found": true,
"_source": {
"id": 1004,
"name": "tic tic tic",
"desc": "tic 真的要变成一个好人了",
"create_date": "2020-12-28"
}
}
2.3.2 查询全量文档数据
请求方式 | 接口地址 | 备注 |
---|---|---|
GET | /doc_api_demo/_doc/_search | doc_api_demo:索引名称 1004:索引的ID |
请求结果
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "my_doc",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"doc": {
"name": "我是阿苏卡DDD"
},
"name": "我是阿苏卡CC"
}
},
{
"_index": "my_doc",
"_type": "_doc",
"_id": "okcQzncBZX7VGcys4RIc",
"_score": 1.0,
"_source": {
"id": 1009,
"name": "auskat-1",
"desc": "tic is a good man 1",
"create_date": "2019-12-24"
}
},
{
"_index": "my_doc",
"_type": "_doc",
"_id": "3",
"_score": 1.0,
"_source": {
"id": 1003,
"name": "auskat-3",
"desc": "tic is a good man 3",
"create_date": "2019-12-24"
}
},
{
"_index": "my_doc",
"_type": "_doc",
"_id": "6",
"_score": 1.0,
"_source": {
"name": "auskat-8",
"desc": "tic is a good man 8",
"create_date": "2019-12-24"
}
}
]
}
}
2.3.3 指定要查询的字段
请求方式 | 接口地址 | 备注 |
---|---|---|
GET | /doc_api_demo/_doc/1004?_source=id,name | doc_api_demo:索引名称 1004:索引的ID |
GET | /doc_api_demo/_doc/_search?_source=id,name | doc_api_demo:索引名称 |
请求结果
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "my_doc",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"name": "我是阿苏卡CC"
}
},
{
"_index": "my_doc",
"_type": "_doc",
"_id": "okcQzncBZX7VGcys4RIc",
"_score": 1.0,
"_source": {
"name": "auskat-1",
"id": 1009
}
},
{
"_index": "my_doc",
"_type": "_doc",
"_id": "3",
"_score": 1.0,
"_source": {
"name": "auskat-3",
"id": 1003
}
},
{
"_index": "my_doc",
"_type": "_doc",
"_id": "6",
"_score": 1.0,
"_source": {
"name": "auskat-8"
}
}
]
}
}
_index
:文档数据所属的那个索引,理解为数据库的某张表即可。_type
:文档数据属于那个类型,新版本用__doc
_id
:文档数据的唯一标识,类似于数据库的主键。可以自动生成或者手工指定_score
:查询相关度,是否契合用户匹配,分数越高用户的搜索体验越高_version
:版本号_source
:文档数据,JSON格式
2.3.4 判断文档是否存在
请求方式 | 接口地址 | 备注 |
---|---|---|
HEAD | /doc_api_demo/_doc/1004 | doc_api_demo:索引名称 1004:索引的ID |
请求结果
{
"_index": "doc_api_demo",
"_type": "_doc",
"_id": "1004",
"_version": 4,
"_seq_no": 7,
"_primary_term": 1,
"found": true,
"_source": {
"id": 1004,
"name": "tic tic tic",
"desc": "tic 真的要变成一个好人了",
"create_date": "2020-12-28"
}
}
2.4 文档删除
请求方式 | 接口地址 | 备注 |
---|---|---|
DELETE | /index_api_demo/1004 | doc_api_demo:索引名称 1004:索引的ID |
返回结果
{
"_index": "doc_api_demo",
"_type": "_doc",
"_id": "4",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 1
}
文档删除不是立即删除,文档还是保存在磁盘上的,索引增长越来越多,才会把那些曾经标识过删除的,进行清理,从磁盘上移出去。
3 相关信息
- 博文不易,辛苦各位猿友点个关注和赞,感谢