Quick start with Elasticsearch - adding, deleting, modifying and checking batch processing

foreword

        Because of the resignation of a colleague in the company, I need to take over the project that was originally in charge of him. These include primary and secondary school question banks and question bank searches that contain tens of millions of data. In order to ensure that the normal iteration of this part of the function can be completed efficiently in the future. I had to start to learn the basic use of Elasticsearch, which led to this learning record. Students who don't know this tool may also get some help through this article.

        Please confirm before reading:

  1. You understand what a unix/linux command line is.
  2. You are sure to understand what is a network request and tcp/udp related knowledge, and have implemented these network requests in any programming language.
  3. You understand curlthe use of commands.
  4. You understand getand putrequest, understand REST and RESTful - you can refer to this linked article " What is RESTFUL?" What are the request methods of REST and what are the differences? ".

text

Environmental preparation

JDK8

        Package management tools such as yum, apt, and brew can also be downloaded, compiled and installed through source code, or installed through deb and .exe.

Download and launch ES6

        I am a mac, so just use brew to download directly.

brew install elasticsearch

        After the download is complete, enter the command directly to start.

elasticsearch

command use

health status check

curl -XGET 'localhost:9200/_cat/health?v&pretty'

        The running results are as follows.

insert image description here

view node list

curl -XGET 'localhost:9200/_cat/nodes?v&pretty'

insert image description here

view index list

        Execute the following command.

curl -XGET 'localhost:9200/_cat/indices?v&pretty'

        If there is no index, there will be only one table header, as follows.

insert image description here

        If there is an index, it will have the following effect.

insert image description here

create index

        To create an index named "test_index", you can initiate the following request.

curl -XPUT 'localhost:9200/test_index&pretty'

        Then you can check and confirm through "View Index List" above. The execution effect is as follows.
insert image description here

Add documentation doc

id = 1Insert a doc         into the index "test_index" created above , the content is as follows.

{
    
    
	"content": "Test Data By Funco~~~"
}

        Then the command line is as follows.

curl -XPUT 'localhost:9200/test_index/doc/1?pretty&pretty' -H 'Content-Type: application/json' -d '{"content": "Test Data By Funco~~~"}'

        Then the execution effect is as follows.

insert image description here

Notice

  1. If you add a doc to an index that does not exist, the index will be created and the doc will be added successfully.
  2. If the command is executed repeatedly to the same id, the old doc will be simply replaced by the new doc.
  3. The id is not required. If no id is specified, es will randomly generate an id as an index. Regardless of whether the id is specified, the id can be obtained in the returned result, such as the fields in the above running diagram _id.

get documentation

        Get the doc just added here id = 1.

curl -XGET 'localhost:9200/test_index/doc/1?pretty&pretty'

        The execution effect is as follows.

insert image description here

delete index

        Above, we mentioned that if the selected index does not exist when adding a document, the index will be added. So, now what if we want to remove the index added by mistake? The following commands can be executed. For example, we now want to delete an customerindex named .

curl -XDELETE 'localhost:9200/customer?pretty&pretty'

        The execution effect is as follows, and after the execution is successful, you will find that the index no longer exists in the list through the above command to view the index list.

insert image description here

modify document

        Just now in the "Adding Documents" section, it is mentioned that the new doc will simply replace the old doc, but at this time, what if we want to modify only part of the content instead of replacing it?
        Now suppose we are in the index test_index, there is a doc with id = 1 as follows.

insert image description here

curl -XPOST 'localhost:9200/test_index/doc/1/_update?pretty&pretty' -H 'Content-Type: application/json' -d '{"doc":{"author": "funco"}}'

        The execution effect is as follows. Pay attention to using POST to submit, and the fields -din doc, compare the doc before modification, datathe parameters submitted during modification, and the modified doc, you may understand.

insert image description here

delete document

        There is nothing to say about this. Submit the REST DELETE operation and specify the doc pointed to by an id under the index. For example, I want to delete id = 1the doc in test_index. You can execute the following commands.

curl -XDELETE 'localhost:9200/test_index/doc/1?pretty&pretty'

        The execution effect is as follows. It can be seen that after deleting, get the doc again, and the field of the returned result foundis false.

insert image description here

batch processing

Batch insert documents

curl -XPOST 'localhost:9200/test_index/doc/_bulk?pretty&pretty' -H 'Content-Type: application/json' -d '
{"index":{"_id": 2}}
{"content":"test content2"}
{"index":{"_id": 3}}
{"content":"test content3"}
'

The execution effect is as follows.
insert image description here

Bulk delete and edit

        In the following cases, we can learn how to delete data and modify data, and perform different operations in one request.

        First, confirm the existence ofid = 2 and records before execution.id = 3

insert image description here

        Then, execute the following command.

 curl -XPOST 'localhost:9200/test_index/doc/_bulk?pretty&pretty' -H 'Content-Type: application/json' -d '
{"update":{"_id": 2}}
{"doc":{"author":"funco"}}
{"delete":{"_id": 3}}
'

        The execution effect is as follows.

insert image description here

        Finally, verify it again. You can confirm that the execution was successful.

insert image description here

search

/<index>/doc/<id>Take the previous section "Batch Deletion and Modification" as an example. Before and after we execute the operation, we can only execute two requests         to know the documents with id = 2 and id = 3. What if GETwe want to get all the documents? Assuming we want to get test_indexall the documents under now, we can execute the following command.

curl -XGET 'localhost:9200/test_index/_search?q=*&pretty&pretty'

insert image description here

        We can even add getparameters sort=_id:ascto realize that the search results _idare sorted in ascending order by field.

curl -XGET 'localhost:9200/test_index/_search?q=*&sort=_id:asc&pretty&pretty'

insert image description here

        It should be noted that we can also put parameters in data as in the previous request. The effect is the same as the previous example. This is useful for maintaining a consistent style of processing when we use code to initiate requests.

curl -XGET 'localhost:9200/test_index/_search?pretty&pretty' -H 'Content-Type: application/json' -d '
{
"query": {"match_all":{}},
"sort": {"_id": "asc"}
}
'

        The default search result is 10. If you want to search for more data, you need to sizespecify it through parameters. Regarding the search, I will create another blog to share.

Guess you like

Origin blog.csdn.net/qq_23937195/article/details/98059666