Elasticsearch Managing Documents

Source: Internet
Author: User

ES supports near real-time indexing, updating, querying, and deleting documents, which means that the data just indexed takes 1 seconds to search, which is different from the traditional SQL database.

More ES documentation Reference: Elasticsearch Official document Translation

Index/Replace Document

I've tried to index a document before, so let's review it here:

' Localhost:9200/customer/external/1?pretty ' ' {  "name""John Doe"}' 

In the example above, a document with a type of external,id of 1 is created, indexed to customer.

When you execute the command again:

' Localhost:9200/customer/external/1?pretty ' ' {  "name""Jane Doe"}' 

The first document was overwritten.

If you specify a new document ID, the old document still exists:

' Localhost:9200/customer/external/2?pretty ' ' {  "name""Jane Doe"}' 

The ID of the index is optional, and if you do not specify id,es a random ID is generated and the document data is indexed using this ID.

' Localhost:9200/customer/external?pretty ' ' {  "name""Jane Doe"}' 

It is important to note that if you do not specify an ID, you need to use the post command instead of put.

Update document

In addition to indexing and replacing documents, ES also supports updating documents. Updating a document is a matter of deleting the old document and then indexing the new document.

If you want to update the contents of the document, you can do so in the following ways:

' Localhost:9200/customer/external/1/_update?pretty ' ' {  "doc""name""Jane Doe  "  }}'

Because the re-index is removed first, additional fields can be added:

' Localhost:9200/customer/external/1/_update?pretty ' ' {  "doc""name""Jane Doe  "Age"  }'

It is also supported to use scripts for updates:

Curl-xpos
' Localhost:9200/customer/external/1/_update?pretty ' ' { "script""ctx._source.age + = 5"} '

Where Ctx._source represents the current document, which means that the age plus 5 is based on the current document.

Delete a document

Deleting a document is simple, just specify the index, type, and ID of the document:

' Localhost:9200/customer/external/2?pretty '

Bulk operations

In addition to index, replace, update, and delete, ES can execute multiple commands at once, and finally return the execution result uniformly in order to reduce the response information back and forth.

For example:

Curl-xpost'Localhost:9200/customer/external/_bulk?pretty'-D'{"Index":{"_id":"1"}}{"name":"John Doe" }{"Index":{"_id":"2"}}{"name":"Jane Doe" }'

The above command can insert two data at the same time.

The _bulk command does more than just support a single command to execute multiple lines, but only a number of different commands.

Curl-xpost'Localhost:9200/customer/external/_bulk?pretty'-D'{"Update":{"_id":"1"}}{"Doc": {"name":"John Doe becomes Jane Doe" } }{"Delete":{"_id":"2"}}'

In the above command, update the document with ID 1 First, and then delete the document with ID 2.

If one of the commands in the bulk performs an error, the subsequent command continues, and the result of each command is returned when the command returns.

Elasticsearch Managing Documents

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.