Elasticsearch Index and document operations example tutorials

Source: Internet
Author: User
Tags kibana
    • Elasticsearch Version: 5.4

    • Elasticsearch QuickStart 1th: Getting Started with Elasticsearch

    • Elasticsearch QuickStart 2nd: Elasticsearch and Kibana installation

    • Elasticsearch QuickStart 3rd: Elasticsearch Index and document operations

    • Elasticsearch QuickStart 4th article: Elasticsearch document Query

List all indexes

GET/_cat/indices?v

The return content is as follows:

Health status Index   UUID                   pri rep docs.count docs.deleted store.size pri.store.sizeyellow open   . Kibana Xyzpr5xgqgwj8ylyz1et_w   1   1          1            0      3.1kb          3.1kb

You can see that there is an index in the cluster

Create an index

Now let's create an index called customer , and then list all the indexes again

Put/customer?prettyget/_cat/indices?v

The execution of the first line returns the following, where we use the put verb to create an index called customer , followed by pretty to return data with formatted JSON if there is data returned

{  "acknowledged": true,  "shards_acknowledged": true}

The execution of the second line returns the following, which tells us that an index named customer has been created with 5 primary shards and one copy shard (1 by default), and there are no documents in this index.

Health status Index    UUID                   pri rep docs.count docs.deleted store.size pri.store.sizeyellow open   . Kibana  Xyzpr5xgqgwj8ylyz1et_w   1   1          1            0      3.1kb          3.1kbyellow Open   Customer M8i1zxhsqjqk7homoa7c_q   5   1          0            0       650b           650b

Perhaps you have noticed that the health value of the Customer index is marked as yellow , recalling what we discussed earlier, yellow that the replicated shard (copy) of the index has not been assigned. This index occurs because Elasticsearch creates 1 copies of the index by default, and since we have only 1 nodes at this point, the copy cannot be allocated (for high availability) until another node is added to the cluster later. Once the copy is assigned to another node, the health status of the index becomes Green .

Indexing and querying documents

Next we put something into the customer index. As mentioned before, in order to index a document, we must tell Elasticsearch which type of the index this document should belong to, below we index a simple document to the Customer index, the type name is External , and the ID is 1

put/customer/external/1?pretty{  "name": "John Doe"}

The return content is as follows:

{  "_index": "Customer",  "_type": "External",  "_id": "1",  "_version": 1,  "result": "Created",  "_shards": {"Total": 2, "successful": 1, "failed": 0  },  "created": true}

As can be seen from the above, a new client document is successfully indexed to the extenal type of the Customer Index, and we specify the internal ID value of the document at index 1.

It is important to note that Elasticsearch does not need to explicitly create an index before you index the document to an index. For example, if the customer index does not exist, Elasticsearch will automatically create the index.

And look at the document we just indexed.

Get/customer/external/1?pretty

The return content is as follows:

{  "_index": "Customer",  "_type": "External",  "_id": "1",  "_version": 1,  "found": true,  "_source": {"name": "John Doe"}  }

The Special One here is the found field, which shows that we have found a document with ID 1 and another special field _source, which holds the document indexed in the previous step.

Delete Index

Now let's delete the index we just created and look at all the indexes again.

Delete/customer?prettyget/_cat/indices?v

The first line returns the following content:

{  "acknowledged": true}

The second line returns the following:

Health status Index   UUID                   pri rep docs.count docs.deleted store.size pri.store.sizeyellow open   . Kibana Xyzpr5xgqgwj8ylyz1et_w   1   1          1            0      3.1kb          3.1kb

From the above, we can see that our Customer index has been deleted.

Before continuing with your study, let's take a quick look at the API commands in this section

put/customerput/customer/external/1{  "name": "John Doe"}get/customer/external/1delete/customer

If you study the above command carefully, you should find the pattern used by Elasticsearch to access the data, as summarized below:

<rest verb>/<index>/<type>/<id>

Using rest access mode is very common in all API commands, and if you can simply remember it, you've already opened a good head for mastering Elasticsearch .

modifying data

Elasticsearch has the ability to manipulate and query data in near real time, by default, from your index, update or delete your data to the user can search for new results This process takes approximately 1 seconds (based on the refresh frequency). Unlike a platform like SQL, SQL data takes effect immediately after the transaction is complete, with no delay.

Index/Replace Document

We've already demonstrated how to index individual documents, and then review them:

put/customer/external/1?pretty{  "name": "John Doe"}

The above command will index the external type of the specified document to the Customer Index, and the document ID value is 1. If we execute the above command again with different document content (or the same), Elasticsearch will replace the old document with a new one (that is, rebuilding the index).

put/customer/external/1?pretty{  "name": "Jane Doe"}

The above action changes the Name field of the document with ID 1 from "John Doe" to "Jane Doe". On the other hand, if we execute the above command with a different ID, a new document will be created and the old document will remain intact.

put/customer/external/2?pretty{  "name": "Jane Doe"}

The above operations index a new ID of 2 document.

When you index a new document, the ID value is optional. If not specified, elasticsearch will generate a random ID for the document. The actual generated ID will be saved in the return result of the calling API interface.

The following example shows how the document is indexed when the document ID is not specified:

post/customer/external?pretty{  "name": "Jane Doe"}

The return content is as follows:

{  "_index": "Customer",  "_type": "External",  "_id": "AVYC9L6DTGHKSQXKPTLM",  "_version": 1,  " Result ":" Created ","  _shards ": {" Total ": 2," successful ": 1," failed ": 0  },  " created ": true}

Note that in the above example, because there is no ID specified, we need to replace the previous put verb with the post verb.

Update document

In addition to indexing and replacing documents, we can also update documents. Note that Elasticsearch does not update on the original document, and whenever an update is made, elasticsearch deletes the old document and then indexes the new document. The following example shows how to update a document to change the name field of the previous ID 1 to "Jane Doe":

post/customer/external/1/_update?pretty{  "Doc": {"name": "Jane Doe"}}

The following example shows how to update a document with a previous ID of 1, changing the Name field to "Jane Doe" while adding the age field

post/customer/external/1/_update?pretty{  "Doc": {"name": "Jane Doe", "Age": 20}}

You can also use a simple script to perform the update. The following example uses a script to increase the age by 5:

post/customer/external/1/_update?pretty{  "script": "Ctx._source.age + = 5"}

In the example above, Ctx._source refers to the source document that is currently being updated. Note that only one document can be updated at a time when this article is written. In the future, Elasticsearch may provide the ability to update multiple documents through query criteria, such as SQL UPDATE-WHERE statements.

Delete a document

Deleting a document is straightforward, and the following example shows how to delete a document with ID 2 under the Customer index, and the delete by Query API deletes all documents that match a particular query. It is important to note that deleting an entire index directly is more efficient than deleting all documents through the query API.

Delete/customer/external/2?pretty

Batch Processing

In addition to being able to index, update, and delete individual documents, Elasticsearch also provides the ability to perform any of these operations in bulk using the _bulk API . This feature is important because it provides a very efficient mechanism for doing multiple operations as quickly as possible and minimizing network round trips. As an example, the following will index two documents in a bulk operation:

post/customer/external/_bulk?pretty{"index": {"_id": "1"}}{"name": "John Doe"} {"index": {"_id": "2"}}{"name": "Jane Doe "}

The return content is as follows:

{  "took": "  Errors": false,  "items": [{"      index": {"_index": "Customer", "_type": "External", " _id ":" 1 "," _version ": 1," result ":" Created "," _shards ": {          " total ": 2,          " successful ": 1,          " failed ": 0}," Created ": True," status ": 201  }    },    {"      index ": {" _index ":" Customer "," _type ":" external "," _id ":" 2 ", "_version": 1, "result": "Created", "_shards": {          "total": 2,          "successful": 1,          "failed": 0}, "created": True, "status": 201  }    }  ]}

The following example updates the first document within one operation and deletes the second document:

post/customer/external/_bulk?pretty{"Update": {"_id": "1"}}{"Doc": {"name": "John Doe becomes Jane Doe"}} {"Delete": {"_ ID ":" 2 "}}

The return content is as follows:

{  "took": "  Errors": false,  "items": [{"      Update": {"_index": "Customer", "_type": "External", "_id": "1", "_version": 2, "result": "Updated", "_shards": {          "total": 2,          "successful": 1,          "failed": 0}, " Status ": $  }    },    {"      delete ": {" found ": true," _index ":" Customer "," _type ":" external "," _id ":" 2 "," _version ": 2," result ":" deleted "," _shards ": {          " total ": 2,          " successful ": 1,          " failed ": 0}," status ": 200< c15/>}    }  ]}

Note that the above delete operation, after which there is no corresponding source document, because only the ID of the document can be deleted.

If an operation fails for some reason, it does not affect subsequent operations, and it continues with the rest of the operation. When the API returns results, each operation provides the state (and the order in which it was received), which allows you to check whether the operation was successful.

Summarize

Simple index operations

1. View the index in the cluster, GET/_cat/indices?v

2. CREATE index put/product?pretty . (es automatically establishes index and type, does not need to be created in advance, and Es defaults to an inverted index for each field in the document so that it can be searched)

3, delete the index, delete/test_index?pretty

CRUD Operations for documents

1. New Products

put/product/goods/1{"goods_id": "Ten", "Goods_name": "Sony Ericsson C702c", "Createtime": "2016-12-21", "Goods_type": ["Huawei", "Le Vision", " Xiaomi "]}

2, inquiry products, get/product/goods/1

3. Modify the Product

Mode 1: Replace the document (as created, all fields must be written in full)

put/product/goods/4{"goods_id": "Max", "Goods_name": "Lenovo Notebook", "Createtime": "2017-05-21", "Goods_type": ["PC"]}

The field does not write the whole situation

Method 2: Update the document

post/product/goods/1/_update{  "Doc": {"Goods_name": "iphone"  }}

Compare create, update, and replace documents to return results:

4, delete goods, delete/product/goods/4

Related Article

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.