In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how Elasticsearch implements document manipulation. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Elasticsearch Review
In the previous four articles, we discussed what es is, what it is used for, how to install it, how to build an index library, and how to add mapping mapping to an index library. In this article, we begin to discuss how to add a document, how to modify it and how to delete it.
Document
What is an es document? in fact, mapping is the data field constraint of es. We define an index and define a mapping mapping, which is equivalent to defining the table structure of this table in a relational database. That is, our index will use those columns. A document in es is actually a row of data records under mapping constraints. An index can be thought of as an optimized collection of documents, and each document is a collection of fields that are key-value pairs that contain data. By default, Elasticsearch indexes all data in each field, and each index field has a dedicated optimized data structure. I hope my friends can understand this.
Maping mapping structure
In the last article, we created an index called movie and created the mapping of the index. Review the commands for querying the index.
Curl-X GET "localhost:9200/movie/_mapping?pretty"
As you can see, if you return a json string, you can clearly see the mapping we defined. We will not post it here, and those who do not know can check it out in the previous article.
Add
Next, we add our document according to the defined mapping, specifically stating that our subsequent operations on es will be operated by a tool postman, and friends who do not know it yet can learn by themselves.
As we all know, the PUT command is used to add documents, so you need to execute the following command to add documents. For more information, please see the figure below, the operation of postman
Http://ip:9200/movie/_doc/1
❝
The data we are going to add is written in the form of json key-value pairs, put it in the body input box, click raw, and select the format of JSON
The author of _ doc currently uses version 7.8 of es. Es7.0 used to support multiple types, but after 7.0, multiple types have been removed, and the _ docl type is supported by default.
❞
If the request is successful, the following content will be returned:
{"_ index": "movie", "_ type": "_ doc", "_ id": "1", "_ version": 3, "result": "created", "_ shards": {"total": 2, "successful": 1, "failed": 0}, "_ seq_no": 2, "_ primary_term": 2} ❝
According to the above, we can see that the document is the created "result": "created", which belongs to the _ doc type of the movie index and its version number.
❞id automatic assignment
In the above example, when we add a document, we specify that the id of a document is 1, so what if we don't specify id? At this time, es will automatically generate an id for us, please see the following operation.
❝
You can see that there is an error when you let the system assign id automatically and add documents with PUT, and you can see that you are prompted to use post rather than PUT. Then we switch to post to make the request, and the result is as follows. You can see that the id assigned to us by the system is a string.
❞{"_ index": "movie", "_ type": "_ doc", "_ id": "9XmceHQByHcRbTF_z1TT", "_ version": 1, "result": "created", "_ shards": {"total": 2, "successful": 1, "failed": 0}, "_ seq_no": 4, "_ primary_term": 3} modification
The modification of es is not a real modification, its mechanism is to delete the original document, then create a new one, and add the version number by 1. Then what we will modify is as follows:
{"films": "eight hundred", "name": "eight hundred", "release_area": "China", "release_time": "August 21", "theme": "praise small potatoes"}
Use the PUT command to make the request again, and the result is as follows:
{"_ index": "movie", "_ type": "_ doc", "_ id": "1", "_ version": 4, "result": "updated", "_ shards": {"total": 2, "successful": 1, "failed": 0}, "_ seq_no": 3, "_ primary_term": 2} ❝
As you can see, at this time "result": "updated" is an operation of updated`` rather than created, and the version numbers are accumulated.
❞deletion
It's easy to delete a document. Just specify the id of the document you want to delete, as follows:
Query
Let's add the document we just deleted. Let's take a look at how to query the document.
Query a single document
We query the document with id 1 as follows:
Batch query
For batch queries, we will use a _ mget instruction, as follows:
Http://121.36.55.57:9200/_mget
The request body is
{"docs": [{"_ index": "movie", "_ type": "_ doc", "_ id": "1"}, {"_ index": "movie", "_ type": "_ doc", "_ id": "2"}]}
Query result
{"docs": [{"_ index": "movie", "_ type": "_ doc", "_ id": "1", "_ version": 1, "_ seq_no": 6, "_ primary_term": 3, "found": true "_ source": {"films": "eight hundred", "name": "eight hundred", "release_area": "China", "release_time": "August 21", "theme": "praise small potatoes"}} {"_ index": "movie", "_ type": "_ doc", "_ id": "2", "_ version": 1, "_ seq_no": 7, "_ primary_term": 3, "found": true "_ source": {"films": "Creed", "name": "Creed", "release_area": "United States" Britain "," release_time ":" September 4th "," theme ":" Space-time counterattack "}]}
You can also query these queries, specify the index, specify the type, and directly provide id.
Http://121.36.55.57:9200/movie/_doc/_mget
The body of the request is as follows: it can also be found.
{"ids": ["1", "2"]} Thank you for reading! On "Elasticsearch how to achieve document operation" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.