Quick start
Tips
If you have used Mybatis-Plus, you can basically use it directly without reading this document. Easy-Es is the replacement version of Mybatis-Plus in Elastic Search.
We will illustrate the power of Easy-Es through a simple demo. Before that, we assume you have:
- Have a Java development environment and corresponding IDE
- Familiar with MySQL
- Familiar with Spring Boot (version 2.5.x+ recommended)
- Familiar with Maven
- Understand the basic concepts of Es or have read Avoid pit guide (strongly recommended)
- Installed Es recommended version 7.x+ (you can use Baidu tutorial if you don't have it installed, it is recommended to install an es-head plug-in for visual verification), the lower version may have API incompatibility or other unknown situations, because the underlying Use RestHighLevelClient instead of RestLowLevelClient, this Demo uses Es version 7.10.0
Recommended
It is recommended that you refer to this Springboot integration demo, which can help you save more time.
special attention
Since springboot is built-in associated with the es version, different springboot versions will cause the es dependency version introduced in the actual project to be too low or too high. However, the compatibility of different versions of es is relatively poor, causing many users to step on the pit of some compatibility problems. I hereby remind users that if the version of es that your project actually depends on is not For version 7.14.0, we strongly recommend that you specify that the es-dependent jar package version is 7.14.0, which is consistent with the es-dependent version we use at the bottom, so the compatibility is the best, It is not easy to step on the pit. The reason why the bottom layer adopts 7.14.0 is also after multiple investigations, and a stable version without security vulnerabilities was selected, which was scanned by Murphy to be safe and reliable. As for the ES client version, the 7.10+ measured compatibility is very good. It doesn't matter if the jar package version in the dependency does not match the client version, the focus is on the dependent Jar version.
# Initialize the project
Create an empty Spring Boot project
Tips
You can use Spring Initializer (opens new window) to quickly initialize a Spring Boot project
# add dependencies
Maven:
<dependency>
<groupId>cn.easy-es</groupId>
<artifactId>easy-es-boot-starter</artifactId>
<version>Latest Version</version>
</dependency>
2
3
4
5
Gradle:
compile group: 'cn.easy-es', name: 'easy-es-boot-starter', version: 'Latest Version'
# ✨Latest Version: (opens new window)
# configure
Add the necessary configuration for EasyEs to the application.yml configuration file:
easy-es:
enable: true #The default is true, if it is false, the framework is not considered to be enabled
address : 127.0.0.1:9200 # The connection address of es must contain the port. If it is a cluster, it can be separated by commas. For example: 127.0.0.1:9200,127.0.0.2:9200
username: elastic #If none, you can omit this line of configuration
password: WG7WVmuNMtM4GwNYkyWH #If there is none, you can omit this line of configuration
2
3
4
5
Other configurations can be omitted for the time being, and the following chapters will introduce the configuration of EasyEs in detail
Add the @EsMapperScan annotation to the Spring Boot startup class to scan the Mapper folder:
@SpringBootApplication
@EsMapperScan("com.xpc.easyes.sample.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2
3
4
5
6
7
8
9
# background
There is a Document document table. With the expansion of the data volume, its query efficiency can no longer meet the product requirements. The table structure is as follows. We plan to migrate the content of this table to the Es search engine to improve query efficiency.
id | title | content |
---|---|---|
primary key | title | content |
# encoding
Write the entity class Document.java (Lombok (opens new window) is used here to simplify the code)
@Data
public class Document {
/**
* unique id in es
*/
private String id;
/**
* document title
*/
private String title;
/**
* Document content
*/
private String content;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Tips
- The above field names and underscores are converted to automatic hump, the storage type of the field in ES, the tokenizer, etc. can be configured, which will be introduced in the subsequent chapters.
- String type will be created as keyword type by EE by default, and keyword type supports precise query, etc.
- If you need to query by word segmentation, you can add the @TableField annotation to the field and specify that the field type is text, and specify the tokenizer as in the above content.
Write the Mapper class DocumentMapper.java
public interface DocumentMapper extends BaseEsMapper<Document> {
}
2
# Pre-operation
Start the project, and Easy-Es will automatically create an index for you (equivalent to a table in a database such as MySQL). Only with an index can you perform subsequent CRUD operations. After the index is successfully hosted, you can see in the console: ===> Congratulations auto process index by Easy-Es is done!
Tips
- If the index is updated, index reconstruction, update, data migration and other tasks will be automatically completed by EE by default, of course, you can also disable automatic index hosting through configuration, manual maintenance through the API provided by EE or plug-ins such as es-head maintain.
- Automatic hosting mode (supported by version 0.9.9+), the related configuration and detailed introduction can be seen in the following chapters, here you can just hand over these annoying steps to EE for automatic processing.
- If your EE version is lower than this version, you can manually maintain the index through the API provided by EE
# Getting started (CRUD)
Add a test class for functional testing:
Test addition: add a new piece of data (equivalent to the Insert operation in MySQL)
@Test
public void testInsert() {
// test insert data
Document document = new Document();
document.setTitle("hello");
document.setContent("my content");
int successCount = documentMapper.insert(document);
System.out.println(successCount);
}
2
3
4
5
6
7
8
9
Test query: query the specified data according to the conditions (equivalent to the Select operation in MySQL)
@Test
public void testSelect() {
// test query
String title = "hello";
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.eq(Document::getTitle,title);
Document document = documentMapper.selectOne(wrapper);
System.out.println(document);
Assert.assertEquals(title,document.getTitle());
}
2
3
4
5
6
7
8
9
10
Test update: update data (equivalent to the Update operation in MySQL)
@Test
public void testUpdate() {
// Test update There are two cases of update as follows:
// case1: Known id, updated according to id (for the convenience of demonstration, this id is copied from the previous query, and the actual business can be queried by itself)
String id = "krkvN30BUP1SGucenZQ9";
String title1 = "hello";
Document document1 = new Document();
document1.setId(id);
document1.setTitle(title1);
documentMapper.updateById(document1);
// case2: id is unknown, update according to condition
LambdaEsUpdateWrapper<Document> wrapper = new LambdaEsUpdateWrapper<>();
wrapper.eq(Document::getTitle,title1);
Document document2 = new Document();
document2.setTitle("hello1");
document2.setContent("my content1");
documentMapper.update(document2,wrapper);
// Regarding case2, there is another simple way of omitting the entity, which is not demonstrated here. It will be introduced in the following chapters. The syntax is consistent with MP.
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Test delete: delete data (equivalent to the Delete operation in MySQL)
@Test
public void testDelete() {
// Test delete data There are two cases for deletion: delete according to id or delete according to conditions
// Since it is too simple to delete according to the id, here we only demonstrate the deletion according to the conditions, and delete it in the name of Lao Li, so that the psychological balance of Lao Li can be balanced.
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
String title = "hello1";
wrapper.eq(Document::getTitle,title);
int successCount = documentMapper.delete(wrapper);
System.out.println(successCount);
}
2
3
4
5
6
7
8
9
10
Tips
Please move to the complete code sample above: Easy-Es-Sample (opens new window)
# Summary
Through the above simple steps, we have realized the index creation and CRUD functions of Document, and finally helped Lao Li to clean up. From the above steps, we can see that the integration of Easy-Es is very simple. We only need to introduce the starter project and configure the mapper scan path. But the power of Easy-Es is much more than these functions. Want to learn more about the powerful functions of Easy-Es? Then keep reading!