Common additions, deletions, and changes to ES documents

1. New document (Document)

1.1, put method

Format: PUT /index_name/type_name/id{field_name:field_value}(Need to manually specify the id)
Example:

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 
}

Return result:

{
    
    
  "_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
}

This is a 6.3.1version test. If it is a version, if 7.xthere is a warning prompt:
#! 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}).
Reason: ElasticSearch removed type in version 7.X ( why remove it? ), so the method of creating index and mapping has also changed. The main changes are as follows :
1, from the indexing operation PUT {index}/{type}/{id}into PUT {index}/_doc/{id}
2, Mapping the operation PUT {index}/{type}/_mappingbecomes PUT {index}/_mapping
3, all CRUD search keyword operation returns the results of which _typewill be removed
4, parent-child relationship using the joinfield to construct

1.2, post method

Format: POST /index_name/type_name{fieldname:fieldvalue}(Auto-generated id)
Example:

POST /test_index/my_type 
{
    
     
  "name":"test_doc_04", 
  "remark":"forth test elastic post", 
  "order_no":4 
}

Return result

{
    
    
  "_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
}

Second, query documents

Format: GET /index_name/type_name/id
Example:

GET /test_index/my_type/1

Return result

{
    
    
  "_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
  }
}

Three, modify the document

3.1, full update

Format: PUT /index_name/type_name/id{field_name:new_field_value}Same as the new syntax of put, the field information of the new data is required to be consistent with the field information of the original data. That is, all fields in the Document must be included. This operation is equivalent to an overwrite operation. During the full replacement process, ES will not really modify the data in the Document, but will mark the original Document in ES as deleted, and then create a new Document to store the data. When the amount of data in ES is too large, ES Documents in the deleted state are recycled in the background.
Example:

PUT /test_index/my_type/1 
{
    
     
  "name":"new_test_doc_01", 
  "remark":"first test elastic put", 
  "order_no":1 
}

Return result

{
    
    
  "_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, partial update

Format:, POST /index_name/type_name/id{field_name:field_value_for_update}only update some fields in a Document. This update method also marks the original data as the deleted state, creates a new Document data, composes the new Document with the new fields and the original fields that have not been updated, and creates it. Compared with full replacement, it is only convenient in operation, and there is almost no difference in bottom-level execution.
Example:

POST /test_index/my_type/1/_update 
{
    
     
  "doc":{
    
     
    "name":" test_doc_01_for_update" 
  } 
}

Return result:

{
    
    
  "_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, check for updates

If you use PUT syntax to perform multiple operations on the same Document. It is a full replacement operation. If you need ES to check whether the PUT Document already exists, you can use the mandatory new syntax. When using mandatory new syntax, if the id of Document already exists in ES, an error will be reported. (Version conflict, document already exists)
Format 1: PUT /index_name/type_name/id/_create
Format 2: PUT /index_name/type_name/id?op_type=create
Example:

PUT /test_index/my_type/1/_create 
{
    
     
  "name":"new_test_doc_01", 
  "remark":"first test elastic put", 
  "order_no":1 
}

Return result:

{
    
    
  "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
}

Fourth, delete documents

4.1, single delete

When performing a delete operation in ES, ES first marks the Document as a deleted state instead of directly physically deleting it. When the ES storage space is insufficient or the work is free, the physical deletion operation is performed. Data marked as deleted will not be searched by the query.
Delete index in ES, which is also a mark. The physical deletion will be performed later. All marking actions are implemented for NRT (near real time).
Format: DELETE /index_name/type_name/id
Example:

DELETE /test_index/my_type/1

Return result:

{
    
    
  "_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
}

Five, bulk add, delete and modify

Use bulk syntax to perform batch additions, deletions and modifications
Format:

POST /_bulk
{
    
     "action_type" : {
    
     "metadata_name" : "metadata_value" } } {
    
     document datas | action datas }

The optional value of action_type in the syntax is::
createmandatory creation, equivalent to PUT /index_name/type_name/id/_create
index: ordinary PUT operation, equivalent to creating Document or full replacement
update: update operation (partial update), equivalent to POST/index_name /type_name/id/_update
delete: delete operation
example:
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" } }

Guess you like

Origin blog.csdn.net/shaixinxin/article/details/108592026