赞
踩
PUT /<target>/_doc/<_id>
POST /<target>/_doc/
PUT /<target>/_create/<_id>
POST /<target>/_create/<_id>
创建新的文档,建议使用 _create
_id 是当前文档对唯一标识,我们文档内容中的id,为存储的数据到字段信息,他们不是必须的对应关系。
常用,意味着包含但不限于如下的参数
PUT /person/_create/1
{
"id": 1,
"name":"张三",
"age": 18,
"height": 1.75,
"birthday":"1998-09-10 00:00:00",
"sex" : 1
}
正常返回结果
{
"_index": "person",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
如果创建id相同的文档,会返回如下结果
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, document already exists (current version [1])",
"index_uuid": "X2YU2E_dRoGfCdSHYtHYiA",
"shard": "0",
"index": "person"
}
],
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, document already exists (current version [1])",
"index_uuid": "X2YU2E_dRoGfCdSHYtHYiA",
"shard": "0",
"index": "person"
},
"status": 409
}
_seq_no 序列号和_primary_term主要术语的作用:当创建、更新或删除文档时,必须将文档的新版本复制到集群中的其他节点。但这些操作是异步的,可能会乱序到达目的地。序列号随着每个操作而增加,因此新操作保证比旧操作具有更高的序列号。elastic 使用序列号和主要术语来唯一标识这些更改。确保较新的文档版本永远不会被分配给它的序列号较小的更改覆盖。
GET <index>/_doc/<_id>
HEAD <index>/_doc/<_id>
GET <index>/_source/<_id>
HEAD <index>/_source/<_id>
external或者external_gt
如果给定版本严格高于存储文档的版本或者不存在现有文档,则仅索引文档。给定的版本将用作新版本,并将与新文档一起存储。提供的版本必须是一个非负长整数。
external_gte
如果给定版本等于或高于存储文档的版本,则 仅索引文档。如果不存在现有文档,操作也会成功。给定的版本将用作新版本,并将与新文档一起存储。提供的版本必须是一个非负长整数。
GET /person/_doc/1
返回结果
{
"_index": "person",
"_id": "1",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"id": 1,
"name": "张三",
"age": 18,
"height": 1.75,
"birthday": "1988-09-10 00:00:00",
"sex": 1
}
}
GET /person/_source/1
结果
{
"id": 1,
"name": "张三",
"age": 18,
"height": 1.75,
"birthday": "1988-09-10 00:00:00",
"sex": 1
}
可知,_source 的结果就是 _doc 结果对象的 _source 字段
GET /person/_source/1?_source_includes=id,name
响应结果
{
"id": 1,
"name": "张三"
}
POST /<index>/_update/<_id>
POST /person/_update/1
{
"script" : {
"source": "ctx._source.age += params.skip",
"lang": "painless",
"params" : {
"skip" : 1
}
}
}
此方式为使用脚本进行更新,脚本非常简单,就是对 age 属性进行 += skip操作,skip为我们为脚本定义的参数,示例值为1。整个脚本就是为 年龄 + 1的更新操作。
响应结果
{
"_index": "person",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
重新获取文档后,age 会是 age + 1的值。
POST /person/_update/1
{
"doc" : {
"birthday": "2013-09-10 00:00:00"
}
}
此方式为直接修改文档,示例只修改了一个属性,当然也可以修改其他属性。
响应结果
{
"_index": "person",
"_id": "1",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
如果更新的值与原值一样,会返回 “result”: “noop”,并且version也不会新增。这很有用,我们一般不会禁用此功能。
{
"_index": "person",
"_id": "1",
"_version": 3,
"result": "noop",
"_shards": {
"total": 0,
"successful": 0,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
请设置请求体为如下
{
"doc" : {
"birthday": "2013-09-10 00:00:00"
},
"detect_noop" : false
}
GET /_mget
GET /<index>/_mget
GET /_mget
{
"docs": [{
"_id": "1",
"_index":"person"
},{
"_id": "1",
"_index":"users"
}]
}
{
"docs": [
{
"_index": "person",
"_id": "1",
"_version": 5,
"_seq_no": 4,
"_primary_term": 1,
"found": true,
"_source": {
"id": 1,
"name": "张三",
"age": 19,
"height": 1.75,
"birthday": "2013-09-10 00:00:00",
"sex": 1
}
},
{
"_index": "users",
"_id": "1",
"error": {
"root_cause": [
{
"type": "index_not_found_exception",
"reason": "no such index [users]",
"resource.type": "index_or_alias",
"resource.id": "users",
"index_uuid": "_na_",
"index": "users"
}
],
"type": "index_not_found_exception",
"reason": "no such index [users]",
"resource.type": "index_or_alias",
"resource.id": "users",
"index_uuid": "_na_",
"index": "users"
}
}
]
}
如果对应的索引不存在,则会返回错误信息
如果索引存在,但是文档不存在,则返回如下信息
{
"docs": [
{
"_index": "person",
"_id": "1",
"_version": 5,
"_seq_no": 4,
"_primary_term": 1,
"found": true,
"_source": {
"id": 1,
"name": "张三",
"age": 19,
"height": 1.75,
"birthday": "2013-09-10 00:00:00",
"sex": 1
}
},
{
"_index": "users",
"_id": "1",
"found": false
}
]
}
GET /person/_mget
{
"ids": ["1","2"]
}
DELETE /<index>/_doc/<_id>
在文档上执行的每个写操作,包括删除,都会导致其版本增加。已删除文档的版本号在删除后的短时间内仍然可用,以允许控制并发操作。已删除文档的版本保持可用的时间长度由index.gc_deletes索引设置决定,默认为 60 秒。
我们可以使用 if_seq_no + if_primary_term 来控制并发删除。如果检测到不匹配,该操作将产生VersionConflictException 409 状态代码。
DELETE /person/_doc/1?if_seq_no=2&if_primary_term=1
我们的person 1 目前的 _seq_no = 4,_primary_term = 1,所以当前的删除请求是失败的
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, required seqNo [2], primary term [1]. current document has seqNo [4] and primary term [1]",
"index_uuid": "X2YU2E_dRoGfCdSHYtHYiA",
"shard": "0",
"index": "person"
}
],
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, required seqNo [2], primary term [1]. current document has seqNo [4] and primary term [1]",
"index_uuid": "X2YU2E_dRoGfCdSHYtHYiA",
"shard": "0",
"index": "person"
},
"status": 409
}
DELETE /person/_doc/1?version=2&version_type=external
我们文档当前版本为version = 4,当前的示例请求也是会失败的
响应结果
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, current version [5] is higher or equal to the one provided [2]",
"index_uuid": "X2YU2E_dRoGfCdSHYtHYiA",
"shard": "0",
"index": "person"
}
],
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, current version [5] is higher or equal to the one provided [2]",
"index_uuid": "X2YU2E_dRoGfCdSHYtHYiA",
"shard": "0",
"index": "person"
},
"status": 409
}
DELETE /person/_doc/1
索引重建,类似于表的拷贝,在更新现有索引映射类型的时候常常需要索引重建。因为修改现有索引映射字段的类型时,可能会使已编制索引的数据无效。但有时我们不得不修改现有索引映射字段类型时,可以通过索引重建的方式来防止已有数据的索引无效的问题。
POST /_reindex
conflicts
(可选,枚举)设置为proceed继续重建索引,即使存在冲突。默认为abort.
max_docs
(可选,整数)要重建索引的最大文档数。如果conflicts等于 proceed,则 reindex 可能会尝试从源中重新索引更多的文档,直到max_docs它成功地将文档索引max_docs到目标中,或者它已经遍历了源查询中的每个文档。
source
dest 来源
script
索引重建,不会从源或其关联模板复制设置。因此,须提前配置映射、分片计数、副本等。所以我们先创建一个目标索引,设置好相应的设置,然后再 _reindex
PUT /person1
{
"mappings": {
"properties": {
"age": {
"type": "integer"
},
"birthday": {
"type": "date",
"format":"yyyy-MM-dd HH:mm:ss"
},
"name": {
"type": "text"
},
"height": {
"type": "float"
},
"id": {
"type": "long"
},
"sex":{
"type": "byte"
}
}
}
}
此处我们设置了新的映射,该映射name属性映射为了text类型。
POST /_reindex
{
"source": {
"index": "person"
},
"dest": {
"index": "person1"
}
}
{
"took": 296,
"timed_out": false,
"total": 1,
"updated": 0,
"created": 1,
"deleted": 0,
"batches": 1,
"version_conflicts": 0,
"noops": 0,
"retries": {
"bulk": 0,
"search": 0
},
"throttled_millis": 0,
"requests_per_second": -1,
"throttled_until_millis": 0,
"failures": []
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。