Quick start
提示
If you have used Mybatis-Plus, you can basically get started without having to read more than this documentation Easy-Es is a flat version of Mybatis-Plus for Elastic Search.
We will illustrate the power of Easy-Es with a simple demo, before that we assume that you have.
- Have a Java development environment and the appropriate IDE
- Familiarity with MySQL
- Familiarity with Spring Boot (recommended version 2.5.x +)
- Familiarity with Maven
- Understand the basic concepts of Es or have read the avoidance guide (highly recommended)
- Es installed recommended version 7.x (not installed by their own Baidu tutorials, it is recommended to install an es-head plug-in, to facilitate visual verification), low version may exist API incompatibility or other unknown cases, because the underlying RestHighLevelClient rather than RestLowLevelClient, the demo uses Es version is 7.14.0
Recommended
We recommend you to refer to this Springboot integration demo, it will help you save more time.
Special Note
Because springboot has a built-in version of es, different versions of springboot may cause the actual project to introduce a low or high version of es dependency, Many users have stepped into some compatibility problems due to conflicting dependencies, so we would like to remind you that if the actual es version that your project depends on is not 7.14.0 version, we strongly recommend that you show the specified es dependency jar package version for 7.14.0, and we use the underlying es dependency version to maintain consistency, so the best compatibility, It is not easy to step in the hole. The reason for the underlying 7.14.0 is also after much research, the choice of a stable version without security vulnerabilities, by Murphy scan the version is safe and reliable. As for the ES client version, 7.10+ tested compatibility are very good, the dependency of the jar package version and the client version does not matter, the focus is on the dependency of the Jar version.
# Initialize the project
Create an empty Spring Boot project
提示
You can use Spring Initializer (opens new window) to quickly initialize a Spring Boot project
# Add dependencies
Maven:
<!-- Introduce dependencies for the latest version of easy-es -->
<dependency>
<groupId>org.dromara.easy-es</groupId>
<artifactId>easy-es-boot-starter</artifactId>
<!-- Here Latest Version refers to the latest version of the dependency, such as 2.0.0, which can be obtained from the image below -->
<version>Latest Version</version>
</dependency>
<!-- exclude the built-in es dependency in springboot to prevent conflicts with the easy-es dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.client</groupId
<artifactId>elasticsearch-rest-high-level-client</artifactId
</exclusion>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.14.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.14.0</version>
</dependency>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Gradle:
compile group: 'org.dromara.easy-es', name: 'easy-es-boot-starter', version: 'Latest Version'
# ✨ Latest Version Latest Version: (opens new window)
# Configuration
In the application.yml configuration file add the relevant configuration that EasyEs must have.
easy-es:
enable: true #default is true, if false then the framework is not enabled
address : 127.0.0.1:9200 # es's connection address, must include the port If it is a cluster, it can be separated by a comma Example: 127.0.0.1:9200,127.0.0.2:9200
username: elastic #If not, you can omit this line
password: WG7WVmuNMtM4GwNYkyWH #If not, then this line can be omitted
2
3
4
5
Other configurations can be omitted for the time being, and there will be a section describing the configuration of EasyEs in detail later
Add the @EsMapperScan annotation to the Spring Boot startup class and scan the Mapper folder for.
@SpringBootApplication
@EsMapperScan("com.xpc.eases.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 |
# Coding
Write entity class Document.java (here we use Lombok (opens new window) to simplify the code)
@Data
@IndexName
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
16
提示
- The above field names and underscores to auto-hump, field storage types in ES, separators, etc. are configurable and will be introduced in the next sections.
- String type will be created by EE as keyword type by default, keyword type supports exact query, etc.
- If you want to split the query, you can add the @IndexField annotation to the field and specify the field type as text, and specify the splitter, just like content above.
Write Mapper class DocumentMapper.java
public interface DocumentMapper extends BaseEsMapper<Document> {
}
2
##Preceding operations
Manually create an index (equivalent to a table in a database such as MySQL), with which subsequent CRUD operations can be carried out.
@Test
public void testCreateIndex() {
// Test to create an index, and the framework will help you generate the index with one click according to the user-defined comments added on the entity classes and fields. Make sure that the index hosting mode is in manual mode (it is in this mode by default), and it will conflict if it is automatic. boolean success = documentMapper.createIndex();
Assertions.assertTrue(success);
}
2
3
4
5
提示
-At present, we provide four ways to create indexes. The above demonstration is the one-click creation mode recommended for beginners. If you are interested in other centralized modes, you can go to the index hosting chapter to check it out. -The smooth mode of automatic transmission mode can automatically sense the index change, automatically adjust the index and smoothly migrate data, but it is not recommended for Xiaobai. It is recommended to use it after understanding the principle and source code. -Of course, you can also maintain the index through several other modes, or through tools such as logstash and es-head.
# Getting Started (CRUD)
Add a test class for functional testing.
Test Add: Add a new data (equivalent to Insert operation in MySQL)
@Test
public void testInsert() {
// Test inserting data
Document document = new Document();
document.setTitle("Old man");
document.setContent("push * technology is too hard");
int successCount = documentMapper.insert(document);
System.out.println(successCount);
}
2
3
4
5
6
7
8
9
test query: query the specified data based on the conditions (equivalent to the Select operation in MySQL)
@Test
public void testSelect() {
// test query write the same as MP can be chained, or not chained according to the use of flexible choice can be
String title = "Old man";
Document document = EsWrappers.lambdaChainQuery(documentMapper)
.eq(Document::getTitle, title)
.one();
System.out.println(document);
}
2
3
4
5
6
7
8
9