Elasticsearch CRUD

Update operations, generally use this, should not be a lot! ES itself is also a framework that tends to query and retrieve. For this kind of update operation, too frequent is always bad.
However, after reading this article, you can use Script to update all documents, use doc to update some documents, and use upsert to add non-existing documents.

renew

The Update update operation allows ES to obtain a specified document, which can be updated through operations such as scripts. It can be regarded as an atomic operation of first deleting and then indexing, but omitting the return process, which saves network traffic for back and forth transmission and avoids document modification conflicts caused by intermediate time.
Here's an updated example:

curl -XPUT localhost:9200/test/type1/1 -d '{
    "counter" : 1,
    "tags" : ["red"]
}'

script update

Es supports changing the information of the document via script:

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
    "script" : {
        "inline": "ctx._source.counter += count",
        "params" : {
            "count" : 4
        }
    }
}'

The above is to add 4 to each counter through parameters.
You can also add a certain mark:

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
    "script" : {
        "inline": "ctx._source.tags += tag",
        "params" : {
            "tag" : "blue"
        }
    }
}'

In addition to _sourcefields, you can ctxobtain field information such as _index, _type, _id, _version, _parent, _timestamp, and so on._ttl

You can also add a field:

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
    "script" : "ctx._source.name_of_new_field = \"value_of_new_field\""
}'

Remove fields:

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
    "script" : "ctx._source.remove(\"name_of_field\")"
}'

Slightly more complex logic is also supported, such as performing different operations based on a certain tag. For example, if there is a blue tag, delete the document; otherwise, do nothing:

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
    "script" : {
        "inline": "ctx._source.tags.contains(tag) ? ctx.op = \"delete\" : ctx.op = \"none\"",
        "params" : {
            "tag" : "blue"
        }
    }
}'

Update only part of the document

The above script works on all documents, here is how to modify only some documents. Simple recursive merges, inner merges, replacement KVs, and arrays can be implemented using doc.

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
    "doc" : {
        "name" : "new_name"
    }
}'

If both doc and script are used, the operation of doc will be automatically ignored. So it's better to put special operations in scripts as well.

Update detection

If doc is used, it is automatically merged into the existing documentation. If the section defined in the doc is the same as the current document, no action is performed by default. If it is set detect_noop=false, it will be forced to merge into the existing document regardless of whether it is modified or not.

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
    "doc" : {
        "name" : "new_name"
    },
    "detect_noop": false
}'

In the above example, if the name field is new_name, the doc will be merged into the document regardless of whether the current document is the same as the one defined in the doc.

upsert insert

This parameter is mainly used for ES operations when the document does not exist.

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
    "script" : {
        "inline": "ctx._source.counter += count",
        "params" : {
            "count" : 4
        }
    },
    "upsert" : {
        "counter" : 1
    }
}'

In the above example, when the document exists, the script is executed; when the document does not exist, the content in the upsert will be inserted into the corresponding document.

If you want to execute the script operation whether the document exists or not, you can use the parameter scripted_upsert to true.

curl -XPOST 'localhost:9200/sessions/session/dh3sgudg8gsrgl/_update' -d '{
    "scripted_upsert":true,
    "script" : {
        "id": "my_web_session_summariser",
        "params" : {
            "pageViewEvent" : {
                "url":"foo.com/bar",
                "response":404,
                "time":"2014-01-01 12:32"
            }
        }
    },
    "upsert" : {}
}'

Compared with the previous use of Upsert to add content to a non-existing document, the use doc_as_upsertcan insert the content of the doc into the document when the document does not exist.

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
    "doc" : {
        "name" : "new_name"
    },
    "doc_as_upsert" : true
}'

parameter

retry_on_conflict

When indexing and updating are performed, it is possible that another process is performing the update. At this time, a conflict will be caused. This parameter is used to define how long to perform the operation when a conflict is encountered.

routing

Routing is used to route the update request to the right shard and sets the routing for the upsert request if the document being updated doesn’t exist. Can’t be used to update the routing of an existing document.

parent

Parent is used to route the update request to the right shard and sets the parent for the upsert request if the document being updated doesn’t exist. Can’t be used to update the parent of an existing document.

timeout

How long to wait when a shard is unavailable

consistency

The write consistency of the index/delete operation. The write consistency of the index/delete operation
! don't know how to use

refresh

When the operation is performed, the index is automatically refreshed.

fields

After performing the update, the field returned

version & version_type

The update operation uses the version number to determine whether the document has been modified between getting the document and performing the update. Documents can also be updated with a specific version number. If you use force as the version number, then the update operation will not change the version number. Note that this way there is no guarantee that the document has been modified.

external version number

The update operation does not support the external version number, because the external version number is out of the system version control. If the update operation is performed again, it will be completely messed up. If an external version number is used, Index can be used instead of the update operation to reindex the document.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326421234&siteId=291194637