目录
- 一、新增文档(Document)
- 1.1、put方式
- 1.2、post方式
- 二、查询文档
- 三、修改文档
- 3.1、全量更新
- 3.2、部分更新
- 3.3、检查更新
- 四、删除文档
- 4.1、单条删除
- 五、bulk 批量增删改
一、新增文档(Document)
1.1、put方式
格式:PUT /index_name/type_name/id{field_name:field_value}
(需手动指定id)
示例:
PUT /test_index/my_type/1
{
"name":"test_doc_01",
"remark":"first test elastic put",
"order_no":1
}
PUT /test_index/my_type/2
{
"name":"test_doc_02",
"remark":"second test elastic put",
"order_no":2
}
PUT /test_index/my_type/3
{
"name":"test_doc_03",
"remark":"third test elastic put",
"order_no":3
}
返回结果:
{
"_index" : "test_index",
"_type" : "my_type",
"_id" : "1",
"_version" : 1, # 版本号,从1开始递增,每次写操作都会+1
"result" : "created", # 操作 created创建、updated修改、deleted删除
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 2
}
这是使用6.3.1
版本测试,如果是7.x
版本为有如果警告提示:#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
原因:ElasticSearch在7.X版本去掉了type(为何要去掉?),所以创建索引和mapping的方式也变了,主要有如下几点变化:
1、索引操作由 PUT {index}/{type}/{id}
变成PUT {index}/_doc/{id}
2、Mapping 操作由 PUT {index}/{type}/_mapping
变成 PUT {index}/_mapping
3、所有增删改查搜索操作返回结果里面的关键字_type
都将被移除
4、父子关系使用 join
字段来构建
1.2、post方式
格式:POST /index_name/type_name{fieldname:fieldvalue}
(自动生成id)
示例:
POST /test_index/my_type
{
"name":"test_doc_04",
"remark":"forth test elastic post",
"order_no":4
}
返回结果
{
"_index": "test_index",
"_type": "my_type",
"_id": "jmryrXQB6rNRajHJwW21",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
二、查询文档
格式:GET /index_name/type_name/id
示例:
GET /test_index/my_type/1
返回结果
{
"_index": "test_index",
"_type": "my_type",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "test_doc_01",
"remark": "first test elastic put",
"order_no": 1
}
}
三、修改文档
3.1、全量更新
格式:PUT /index_name/type_name/id{field_name:new_field_value}
,和put新增语法一样,要求新数据的字段信息和原数据的字段信息一致。也就是必须包括 Document 中的所有 field 才行。本操作相当于覆盖操作。全量替换的过程中,ES 不会真的修改 Document 中的 数据,而是标记 ES 中原有的 Document 为 deleted 状态,再创建一个新的 Document 来存储 数据,当 ES 中的数据量过大时,ES 后台回收 deleted 状态的 Document。
示例:
PUT /test_index/my_type/1
{
"name":"new_test_doc_01",
"remark":"first test elastic put",
"order_no":1
}
返回结果
{
"_index": "test_index",
"_type": "my_type",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
3.2、部分更新
格式:POST /index_name/type_name/id{field_name:field_value_for_update}
,只更新某 Document 中的部分字段。这种更新方式也是标记原有数据为 deleted 状态, 创建一个新的 Document 数据,将新的字段和未更新的原有字段组成这个新的 Document, 并创建。对比全量替换而言,只是操作上的方便,在底层执行上几乎没有区别。
示例:
POST /test_index/my_type/1/_update
{
"doc":{
"name":" test_doc_01_for_update"
}
}
返回结果:
{
"_index": "test_index",
"_type": "my_type",
"_id": "1",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
3.3、检查更新
如果使用 PUT 语法对同一个 Document 执行多次操作。是一种全量替换操作。如果需要 ES 辅助检查 PUT 的 Document 是否已存在,可以使用强制新增语法。使用强制新增语法时, 如果 Document 的 id 在 ES 中已存在,则会报错。(version conflict, document already exists)
格式1:PUT /index_name/type_name/id/_create
格式2:PUT /index_name/type_name/id?op_type=create
示例:
PUT /test_index/my_type/1/_create
{
"name":"new_test_doc_01",
"remark":"first test elastic put",
"order_no":1
}
返回结果:
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[my_type][1]: version conflict, document already exists (current version [3])",
"index_uuid": "dbTzijLlTde7rvhambY64w",
"shard": "3",
"index": "test_index"
}
],
"type": "version_conflict_engine_exception",
"reason": "[my_type][1]: version conflict, document already exists (current version [3])",
"index_uuid": "dbTzijLlTde7rvhambY64w",
"shard": "3",
"index": "test_index"
},
"status": 409
}
四、删除文档
4.1、单条删除
ES 中执行删除操作时,ES 先标记 Document 为 deleted 状态,而不是直接物理删除。当 ES 存储空间不足或工作空闲时,才会执行物理删除操作。标记为 deleted 状态的数据不会被 查询搜索到。
ES 中删除 index,也是标记。后续才会执行物理删除。所有的标记动作都是为了 NRT 实现(近实时)。
格式:DELETE /index_name/type_name/id
示例:
DELETE /test_index/my_type/1
返回结果:
{
"_index": "test_index",
"_type": "my_type",
"_id": "1",
"_version": 4,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1
}
五、bulk 批量增删改
使用 bulk 语法执行批量增删改
格式:
POST /_bulk
{ "action_type" : { "metadata_name" : "metadata_value" } } { document datas | action datas }
语法中的 action_type 可选值为:create
: 强制创建,相当于 PUT /index_name/type_name/id/_createindex
: 普通的 PUT 操作,相当于创建 Document 或全量替换update
: 更新操作(partial update),相当于POST/index_name/type_name/id/_updatedelete
: 删除操作
示例:create
POST /_bulk { "create" : { "_index" : "test_index" , "_type" : "my_type", "_id" : "1" } } { "field_name" : "field value" }
index
POST /_bulk { "index" : { "_index" : "test_index", "_type" : "my_type" , "_id" : "2" } } { "field_name" : "field value 2" }
update
POST /bulk { "update" : { "_index" : "test_index", "_type" : "my_type" , "_id" : 2", "_retry_on_conflict" : 3 } } { "doc" : { "field_name" : "partial update field value" } }
delete
POST /_bulk { "delete" : { "_index" : "test_index", "_type" : "my_type", "_id" : "2" } }