Elasticsearch Document Management

ES support for near real-time indexing, update, query, delete a document, it means just near real-time data needs to search the index to 1 second, which is the traditional SQL databases in different places.

More ES documentation Reference: elasticsearch official document translation

Index / Replace Document

Before've tried how a document index, and here again refresher:

curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
  "name": "John Doe"
}'

In the above example, we create an index for the customer, type of external, id 1 of the document.

When executing the command again:

curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
  "name": "Jane Doe"
}'

The first document before it is overwritten.

 

If you specify a new document id, then the old documents still exist:

curl -XPUT 'localhost:9200/customer/external/2?pretty' -d '
{
  "name": "Jane Doe"
}'

 

When the index ID is optional, if you do not specify ID, ES will be randomly generated ID, and use this ID index document data.

curl -XPOST 'localhost:9200/customer/external?pretty' -d '
{
  "name": "Jane Doe"
}'

Note that, if you do not specify ID, you need to use the POST command instead of the PUT.

Update Documentation

In addition to index and replace documents, ES also supports the updated document. In fact, the document is updated to delete the old file, and then indexing the new document.

If you want to update the content of the document, can be performed in the following manner:

curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
{
  "doc": { "name": "Jane Doe" }
}'

Since then the index is to delete, so you can add new additional fields:

curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
{
  "doc": { "name": "Jane Doe", "age": 20 }
}'

Of course, also supports the use of scripts update:

curl -XPOS
T 'localhost:9200/customer/external/1/_update?pretty' -d ' { "script" : "ctx._source.age += 5" }'

Ctx._source which represents the current document, the above means that the document on the basis of the current age plus 5.

Delete Document

Deleting a document is very simple, only you need to specify the index of the document, type, ID on the line:

curl -XDELETE 'localhost:9200/customer/external/2?pretty'

 

Batch operations

In addition to the index, replace, update, and delete, ES In order to reduce response information back and forth, you can execute multiple commands one-time, unified finally return the result.

E.g:

curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
'

The above two commands can be inserted into the data simultaneously.

_bulk command only supports a single command to execute multiple, but also a variety of different commands execute multiple.

curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
'

The above command, to update the document with id 1, and then delete the document id 2.

 

If the bulk of a command execution error, we will continue to execute subsequent commands, when the last command returns, it will return the results of each command.

Reproduced in: https: //my.oschina.net/u/204616/blog/545343

Guess you like

Origin blog.csdn.net/weixin_33943836/article/details/91989678