示例:
PUT /spring-elasticsearch-demo/person/1 HTTP/1.1
Host: localhost:9200
Content-Type: application/json
cache-control: no-cache
Postman-Token: 1df2c7ea-a830-4778-b7a6-884dffa80f89
{
"id":"1",
"name":"天天向下",
"description":"这是天天向下",
"age":24,
"create":1535760000001
}
{
"_index": "spring-elasticsearch-demo",
"_type": "person",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
保存文档使用PUT方法,请求URL为/{索引名称}/{类型名称}/{id},如果文档不存在则插入,如果文档存在则更新,返回为:
属性 | 描述 |
_index | 索引名称 |
_type | 类型名称 |
_id | 文档ID |
_version | 文档版本,为整数,第一次插入后为1,更新一次增加1 |
result | 操作结果,created:创建成功,updated:更新成功 |
_shards.total | 应在其上执行操作的分片总数,包含主分片和副本分片 |
_shards.successful | 成功操作的分片数 |
如果之前尚未创建索引(索引用于手动创建索引的create index API),则索引操作会自动创建索引,并且如果尚未创建特定类型,则还会自动创建特定类型的动态类型映射(签出 put mapping API用于手动创建类型映射)。
映射本身非常灵活,无需架构。 新字段和对象将自动添加到指定类型的映射定义中。
通过在所有节点的配置文件中将action.auto_create_index设置为false,可以禁用自动索引创建。 通过将index.mapper.dynamic设置为false per-index作为索引设置,可以禁用自动映射创建。
1.指定操作的文档版本
每个索引文档都有一个版本号。 关联的版本号作为对索引API请求的响应的一部分返回。 索引API可选地允许在指定version参数时进行乐观并发控制。 这将控制要针对该操作执行的文档的版本。 用于版本控制的用例的一个很好的例子是执行事务性读取然后更新。 从最初读取的文档中指定版本可确保在此期间未发生任何更改(在读取时为了更新,建议将首选项设置为_primary)
指定操作文档的版本:
PUT /spring-elasticsearch-demo/person/1?version=1 HTTP/1.1
Host: localhost:9200
Content-Type: application/json
cache-control: no-cache
Postman-Token: ad55f513-6a8f-4321-957a-e18dc1cd3bb9
{
"id":"1",
"name":"天天向左",
"description":"这是天天向下",
"age":24,
"create":1535760000001
}
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[person][1]: version conflict, current version [3] is different than the one provided [1]",
"index_uuid": "6tdQkxFpQgi9s-yXTVZpEw",
"shard": "3",
"index": "spring-elasticsearch-demo"
}
],
"type": "version_conflict_engine_exception",
"reason": "[person][1]: version conflict, current version [3] is different than the one provided [1]",
"index_uuid": "6tdQkxFpQgi9s-yXTVZpEw",
"shard": "3",
"index": "spring-elasticsearch-demo"
},
"status": 409
}
默认情况下,使用从1开始的内部版本控制,并随每次更新而增加,包括删除。可选地,版本号可以用外部值补充(例如,如果在数据库中维护)。要启用此功能,应将version_type设置为external。提供的值必须是数字,长值大于或等于0,小于约9.2e + 18。使用外部版本类型时,系统会检查传递给索引请求的版本号是否大于当前存储文档的版本,而不是检查匹配的版本号。如果为true,则将索引文档并使用新版本号。如果提供的值小于或等于存储文档的版本号,则会发生版本冲突,索引操作将失败。
2.指定操作类型
PUT请求为保存文档,即具有插入、更新两个作用的方法,可以强制指定本次操作的类型
PUT /spring-elasticsearch-demo/person/1?op_type=create HTTP/1.1
Host: localhost:9200
Content-Type: application/json
cache-control: no-cache
Postman-Token: 4709cca4-5604-4724-862e-ce626bfbac17
{
"id":"1",
"name":"天天向左",
"description":"这是天天向下",
"age":24,
"create":1535760000001
}
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[person][1]: version conflict, document already exists (current version [3])",
"index_uuid": "6tdQkxFpQgi9s-yXTVZpEw",
"shard": "3",
"index": "spring-elasticsearch-demo"
}
],
"type": "version_conflict_engine_exception",
"reason": "[person][1]: version conflict, document already exists (current version [3])",
"index_uuid": "6tdQkxFpQgi9s-yXTVZpEw",
"shard": "3",
"index": "spring-elasticsearch-demo"
},
"status": 409
}
也可以使用后缀/_create来表示这是创建请求,一共只有两种操作类型create和index,index即可以进行更新,但是没有后缀/_index请求语法
3.自动生成ID
使用POST方法可以自动生成ID
POST /spring-elasticsearch-demo/person/ HTTP/1.1
Host: localhost:9200
Content-Type: application/json
cache-control: no-cache
Postman-Token: 78b3bc4b-2fec-4a03-bdf1-637e84ec5598
{
"id":"1",
"name":"哈哈",
"description":"这是天天向下",
"age":24,
"create":1535760000001
}
{
"_index": "spring-elasticsearch-demo",
"_type": "person",
"_id": "PBV2l2cBwwy3EJq_VivF",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
自动生成的ID一般是一个字符串