CRUD in Elasticsearch

Source: Internet
Author: User
I briefly introduced Elasticsearch in play with elasticsearch. In this article, we still do some basic learning about how to perform CRUD in Elasticsearch? Suppose we are creating an application similar to Weibo. Let's call it kiwi first. Kiwi is composed of messages. In kiwi, a message is called a ksay

I briefly introduced Elasticsearch in play with elasticsearch. In this article, we still do some basic learning about how to perform CRUD in Elasticsearch? Suppose we are creating an application similar to Weibo. Let's call it kiwi first. Kiwi is composed of messages. In kiwi, a message is called a ksay

I briefly introduced Elasticsearch in play with elasticsearch. In this article, we still do some basic learning about how to perform CRUD in Elasticsearch?

Suppose we are creating an application similar to Weibo. Let's call it kiwi first. Kiwi is composed of messages.

In kiwi, messages are called ksay. There are two parts. One is the author (author), but the message itself (message ).

Create

Curl-x post http: // localhost: 9200/kiwi/ksay/-d' {"author": "rococojie", "message": "I am beautiful "}'

Return: {"_ index": "kiwi", "_ type": "ksay", "_ id": "aaX3P2LJSP-dDYVy0USv7Q", "_ version": 1, "created": true}

We noticed that elasticsearch does not generate IDS by default in auto-increment mode. Instead, a 22-bit URL-safe _ id is automatically generated. As in the previous example, the returned _ id is the aaX3P2LJSP-dDYVy0USv7Q. To use a custom _ id, perform the following operations:

Curl-x post http: // localhost: 9200/kiwi/ksay/1-d' {"author": "jerry", "message": "I hate Tom "}'

Return Value: {"_ index": "kiwi", "_ type": "ksay ","_ Id": "1", "_ Version": 1, "created": true}

Read

Here we only want to use the id value

Curl-x get http: // localhost: 9200/kiwi/ksay/1

Return Value: {"_ index": "kiwi", "_ type": "ksay", "_ id": "1", "_ version": 1, "found": true, "_ source": {"author": "jerry", "message": "I hate Tom "}}

If we want to return the original data

Curl-x get http: // localhost: 9200/kiwi/ksay/1/_ source

Return Value: {"author": "jerry", "message": "I hate Tom "}

Curl-x get http: // localhost: 9200/kiwi/ksay/10000

{"_ Index": "kiwi", "_ type": "ksay", "_ id": "10000", "found": false }, the stored ksay is not found.

Update

Curl-x put http: // localhost: 9200/kiwi/ksay/1-d' {"author": "jerry", "message": "I love Tom "}'

Return Value: {"_ index": "kiwi", "_ type": "ksay", "_ id": "1", "_ version": 2, "created": false}

We noticed that the _ version here is changed to 2, and the knowledge has changed because of ksay. The return value of created is false, indicating that no new document is created, but the document is updated.

Although Elasticsearch supports document updates, we need to know that the documents stored in Elasticsearch are immutable ). This so-called update is actually an illusion. Inside Elasticsearch, we first mark the old data as "deleted ", then index the new data. (Retrieve-change-reindex)

Partial update

Curl-x post http: // localhost: 9200/kiwi/ksay/1/_ update-d' {"doc": {"message": "I hate Tom, again "}}'

Return Value: {"_ index": "kiwi", "_ type": "ksay", "_ id": "1", "_ version": 3}

"Doc" is the field to be updated. Elasticsearch will include the latest field "merge" to the original document. Here is the information of this ksay.

Curl-x get http: // localhost: 9200/kiwi/ksay/1

Return Value: {"_ index": "kiwi", "_ type": "ksay", "_ id": "1", "_ version": 3, "found": true, "_ source": {"author": "jerry", "message": "I hate Tom, again "}}

Delete

Curl-x delete http: // localhost: 9200/kiwi/ksay/1

Return Value: {"found": true, "_ index": "kiwi", "_ type": "ksay", "_ id": "1 ", "_ version": 4}

Try again to get ksay:

Curl-x get http: // localhost: 9200/kiwi/ksay/1

Return Value: {"_ index": "kiwi", "_ type": "ksay", "_ id": "1 ","Found": false}

Cannot be accessed. The value of found is false.

After learning the most basic CRUD of Elasticsearch, we can find some other users to play.

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.