Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Elasticsearch Index and document Operation

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the Elasticsearch index and document operation example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

List all indexes GET / _ cat/indices?v

The returned 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 list all the indexes again

PUT / customer?prettyGET / _ cat/indices?v

The first line of execution returns the following. Here we use the PUT predicate to create an index called customer, followed by pretty to indicate that if any data is returned, the formatted JSON will return the data.

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

Executing the second line returns the following, and the result tells us that an index called customer has been created with five main shards and one replication 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

You may have noticed that the health value of the customer index is marked yellow. Reviewing what we discussed earlier, yellow indicates that the replica shard (replica) of the index has not been allocated. The reason for this is that Elasticsearch creates a copy of the index by default, and since we only have one node at this time, this replica 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, let's put something into the customer index. As mentioned earlier, in order to index a document, we have to tell Elasticsearch which type the document should belong to. Let's index a simple document to the customer index with the type name external and ID 1.

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

The returned content is as follows:

{"_ index": "customer", "_ type": "external", "_ id": "1", "_ version": 1, "result": "created", "_ shards": {"total": 2, "successful": 1, "failed": 0}, "created": true}

As you can see from the above, a new customer document was successfully indexed into the extenal type of the customer index, and we specified the document's internal id value as 1 when indexing.

It is worth noting that Elasticsearch does not need to explicitly create an index before you index a document to an index. For example, in the previous example, if the customer index does not exist, Elasticsearch will automatically create it.

Let's take a look at the document we just indexed.

GET / customer/external/1?pretty

The returned content is as follows:

{"_ index": "customer", "_ type": "external", "_ id": "1", "_ version": 1, "found": true, "_ source": {"name": "John Doe"}}

The special field here is the found field, which indicates that we have found a document with an id of 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:

{"acknowledged": true}

The second line returns 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

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

Before we move on, let's take a quick look back at the API commands learned in this section.

PUT / customerPUT / customer/external/1 {"name": "John Doe"} GET / customer/external/1DELETE / customer

If you study the above commands carefully, you should find the pattern used by elasticsearch to access the data, which is summarized as follows:

/ / /

Using the REST access mode is very common in all API commands, and if you can simply remember it, it's a good start for mastering Elasticsearch.

Modify data

Elasticsearch has the ability to manipulate and query data in near real time. By default, it takes about 1 second (based on refresh frequency) from you to index, update or delete your data to users can search for new results. Unlike platforms like SQL, where SQL data takes effect immediately after the transaction is completed, there is no delay.

Index / replace document

Having previously demonstrated how to index a single document, let's review:

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

The above command will index the external type of the specified document to the customer index, with an id value of 1. If we execute the above command again with different document contents (or the same), elasticsearch will replace the old document with a new document (that is, re-index).

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

The above action changes the name field of a document with an id of 1 from "john doe" to "jane doe". On the other hand, if we use a different id to execute the above command, a new document will be created and the old document will remain as it is.

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

The above operation indexes a new id 2 document.

The id value is optional when indexing new documents. If not specified, elasticsearch will generate a random id for the document. The actual generated id will be saved in the returned result of calling the api API.

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

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

The returned 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, since id is not specified, we need to use the POST predicate instead of the previous PUT predicate.

Update document

In addition to indexing and replacing documents, we can also update documents. Note that elasticsearch does not update the original document; whenever an update is made, Elasticsearch deletes the old document and then indexes the new one. The following example shows how to update the document by changing the name field where ID was 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 simple scripts to perform updates. The following example uses a script to increase age by 5:

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

In the above example, ctx._source refers to the source document that is currently being updated. Note that at the time of writing, you can only update a single document at a time. In the future, Elasticsearch may provide the ability to update multiple documents through query criteria, such as SQL UPDATE-WHERE statements.

Delete document

Deleting a document is very simple. The following example shows how to delete a document with an ID of 2 under the customer index, and consult Delete By Query API to delete all documents that match a particular query. It is worth noting that it is more efficient to delete the entire index directly than to delete all documents through query api.

DELETE / customer/external/2?pretty batch processing

In addition to the ability to index, update, and delete individual documents, Elasticsearch also provides the ability to perform any of these operations in batches using _ bulk API. This function is very important because it provides a very effective mechanism to perform multiple operations as quickly as possible and to minimize the round trip of the network. For a simple example, two documents are indexed in a bulk operation:

POST / customer/external/_bulk?pretty {"index": {"_ id": "1"} {"name": "John Doe"} {"index": {"_ id": "2"}} {"name": "Jane Doe"}

The returned content is as follows:

{"took": 27, "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 and deletes the second document within an operation:

POST / customer/external/_bulk?pretty {"update": {"_ id": "1"} {"doc": {"name": "John Doe becomes Jane Doe"} {"delete": {"_ id": "2"}

The returned content is as follows:

{"took": 25, "errors": false, "items": [{"update": {"_ index": "customer", "_ type": "external", "_ id": "1", "_ version": 2, "result": "updated", "_ shards": {"total": 2, "successful": 1, "failed": 0}, "status": 200} {"delete": {"found": true, "_ index": "customer", "_ type": "external", "_ id": "2", "_ version": 2, "result": "deleted", "_ shards": {"total": 2, "successful": 1, "failed": 0}, "status": 200}}]}

Note the deletion above, after which there is no corresponding source document, because only the document's ID is needed to delete it.

If an operation fails for some reason, it will not affect the later operation, and it will continue to perform the rest of the operation. When api returns a result, each operation provides a status (in the same order as it was received), which you can use to check whether the operation was executed successfully.

Summarize simple index operations

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

2. Create index PUT / product?pretty. (es automatically builds index and type, which does not need to be created in advance, and es default builds an inverted index on each field of document so that it can be searched.)

3. Delete the index, DELETE / test_index?pretty

CRUD operation of the document

1. New products

PUT / product/goods/1 {"goods_id": "10", "goods_name": "Sony Ericsson C702c", "createTime": "2016-12-21", "goods_type": ["Huawei", "Letv", "Xiaomi"]}

2. Inquire about goods, GET / product/goods/1

3. Modify the goods

Method 1: replace the document (as with creation, all fields must be written in full)

PUT / product/goods/4 {"goods_id": "40", "goods_name": "Lenovo Notebook", "createTime": "2017-05-21", "goods_type": ["computer"]}

The case that the field is not fully written.

Method 2: update the document

POST / product/goods/1/_update {"doc": {"goods_name": "iphone Mobile"}}

Compare the results of creating, updating, and replacing documents:

Thank you for reading this article carefully. I hope the article "sample Analysis of Elasticsearch Index and document Operation" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report