Quick start and use of Elasticsearch
1. What is Elasticsearch
Elasticsearch is a free and open distributed search and analytics engine for all types of data including textual, numerical, geospatial, structured and unstructured data, and more.
Elasticsearch is the go-to full-text search engine. It can quickly store , retrieve and analyze massive amounts of data.
2. Basic concepts
1.index (index)
It is equivalent to a database you created in mysql.
2. type (type)
Equivalent to a table you created in mysql.
3. Document (document)
Equivalent to the data in the mysql table (json format)
3. Why Elasticsearch can quickly retrieve data from massive data
Fast retrieval thanks to Elasticsearch's inverted index (stored data will maintain an inverted index table)
Steps of inverted index
Example: Store in order A = "A Journey to the East", B = "Ai Kun Journey to the East", C = "A Journey to the East", D = "Journey to the West"
(1) Participle
① "Dahua Journey to the East" will be divided into "Dahua" and "Dongyou" and belong to A.
②"Ai Kun Da Hua East Journey" will be divided into "Ai Kun", "Da Hua" and "East Journey" and belong to B.
③ "Dahua Lianpian Eastward Journey" will be divided into "Dahua", "Lianpian" and "Eastward Journey" and belong to C.
④ "Journey to the West" will be divided into "Journey to the West" and "Journey to the West" and belong to D.
The inverted index table is as follows:
word | Record |
---|---|
"Big Talk" | A,B,C,D |
"Journey to the East" | A,B,C |
"Love Kun" | B |
"Continuous" | C |
"Journey to the West" | D |
(2) Retrieval
For example: Search "Ai Kun Da Hua Westward Journey"
① Segment the words into "Ai Kun", "Da Hua" and "Journey to the West"
② Go to the reverse index table to check, including the records of these items.
③ Found as follows
word | Record |
---|---|
"Big Talk" | A,B,C,D |
"Love Kun" | B |
"Journey to the West" | D |
④ Then calculate the correlation score
D is the most consistent, because D is divided into two words and both hits (2/2). Then B splits into three words and hits two (2/3). Then A... and finally will be returned according to the relevance score from high to low.
Four. Elasticsearch installation
Elasticsearch official installation article
Download and use elasticsearch-7.3.1 and kibana-7.3.1 extraction code: 0221
1. Unzip
2. run
3. If the following content is displayed, the startup is successful 1
4. Kibana visualization software installation
Installation tutorial Note: The version number should correspond to
5. Getting started (basic operation)
Apifox-2.3.0 self-test tool installation package
Note: Because es encapsulates commands into RestApi, we can just send the request directly.
1. View es basic information
GET/_cat/nodes //查看所有的节点
GET/_cat/health //查看es健康情况
GET/_cat/master //查看所有的节点
GET/_cat/indices //查看所有索引(相当于查看所有数据库)
下面截图举例查询es健康情况:
2. Index a document (add a piece of data)
Example: http://localhost:9200/kunkun/ikun/2.5
Meaning: We save the data with id 2.5 under the ikun type under the kunkun index. (Equivalent to adding a data with an id of 2.5 to the ikun table under the kunkun database, and json data is stored in es)
Similarities and differences:
①POST and PUT requests are newly added. If the request is repeated, it will be updated if it exists.
②If POST and PUT are repeatedly sent to request an update, the original data will not be compared, and the version number will change.
③POST requests without id will automatically generate a unique id for you. A PUT request without an id will report an error.
3. Query a document (query a piece of data)
4. Update a document (update data)
Not applicable: large concurrent updates.
Applicable: Occasional updates for large concurrent queries.
Note: When the POST request is updated with _update
the keyword it will compare the original data. If there is no change in the data, no operation will be performed, and the version number will not change.
5. Delete index or document (delete database or data)
(1) Delete document
(2) Delete index
6. Batch operation
Note: We use Kibana to operate, and the failure of the previous command will not affect the operation of the next command during batch operation.
(1) Simple test
//给kunkun数据库下的ikun表导入两条数据
POST /kunkun/ikun/_bulk
{"index":{"_id":"1"}}
{"name":"chang"}
{"index":{"_id":"2"}}
{"name":"tiao"}
(2) Import test data in batches
Test data extraction code provided by es: 0221
Well, this is the end of the introduction. Please look forward to the follow-up advanced articles.