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:
- You understand what a unix/linux command line is.
- 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.
- You understand
curl
the use of commands. - You understand
get
andput
request, 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.
view node list
curl -XGET 'localhost:9200/_cat/nodes?v&pretty'
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.
If there is an index, it will have the following effect.
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.
Add documentation doc
id = 1
Insert 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.
Notice
- If you add a doc to an index that does not exist, the index will be created and the doc will be added successfully.
- If the command is executed repeatedly to the same id, the old doc will be simply replaced by the new doc.
- 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.
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 customer
index 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.
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.
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 -d
in doc
, compare the doc before modification, data
the parameters submitted during modification, and the modified doc, you may understand.
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 = 1
the 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 found
is false
.
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.
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
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.
Finally, verify it again. You can confirm that the execution was successful.
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 GET
we want to get all the documents? Assuming we want to get test_index
all the documents under now, we can execute the following command.
curl -XGET 'localhost:9200/test_index/_search?q=*&pretty&pretty'
We can even add get
parameters sort=_id:asc
to realize that the search results _id
are sorted in ascending order by field.
curl -XGET 'localhost:9200/test_index/_search?q=*&sort=_id:asc&pretty&pretty'
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
size
specify it through parameters. Regarding the search, I will create another blog to share.