Index CRUD
# Index CRUD
提示
The index of EE has been introduced in the previous index hosting chapter, which supports automatic processing. However, there are still some scenarios where users expect to operate the index themselves, that is, the manual shift mode mentioned in the previous index hosting chapter. This article focuses on all API capabilities provided in manual transmission mode to help users quickly get started with manual transmission CRUD index.
# Pre-configuration
The API related to the index CRUD belongs to the manual gear category, so we must configure and open the manual gear before executing all the following APIs to avoid conflict with the automatic gear.
easy-es:
global-config:
Process_index_mode: manual # manual shift mode.
2
3
# Create an index
Introduction to API
// 1. The index information generated according to the entity class information corresponding to the current mapper and its annotation configuration is suitable for most scenarios.
Boolean createIndex();
// 2. According to the entity class information corresponding to the current mapper and its annotation configuration, the index information can be generated, and the index name can be specified for creation, which is suitable for the scene of creating indexes by date for scheduled tasks.
Boolean createIndex(String indexName);
// 3. Create an index according to user-defined conditions.
Boolean createIndex(Wrapper<T> wrapper);
2
3
4
5
6
7
8
There are three ways to create an index, and the use difficulty is: way 1 <= way 2 < way 3, flexibility way 3 > way 2 >= way 1.
Use case:
/**
* Mode 1
*/
@Test
public void testCreateIndexByEntity() {
//Simplicity is recommended for most scenarios.
documentMapper.createIndex();
}
/**
* Mode 2
*/
@Test
public void testCreateIndexByEntity() {
//Suitable for the scenario that scheduled tasks create indexes by date.
String indexName = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
documentMapper.createIndex(indexName);
}
/**
* Mode 3
*/
@Test
public void testCreateIndex() {
//use of complex scenes
LambdaEsIndexWrapper<Document> wrapper = new LambdaEsIndexWrapper<>();
//Here, for simplicity, the index name must be consistent with the entity class name, and the chapters after lowercase letters will teach you how to flexibly configure and use the index.
wrapper.indexName(Document.class.getSimpleName().toLowerCase());
//Here, the article title is mapped to keyword type (word segmentation is not supported), and the document content is mapped to text type (word segmentation query is supported).
wrapper.mapping(Document::getTitle, FieldType.KEYWORD, 2.0f)
.mapping(Document::getLocation, FieldType.GEO_POINT)
.mapping(Document::getGeoLocation, FieldType.GEO_SHAPE)
.mapping(Document::getContent, FieldType.TEXT, Analyzer.IK_SMART, Analyzer.IK_MAX_WORD);
//version 0.9.8+adds support for string field names, and @Tablefield(value="wu-la ") must be added to the corresponding field in the Document entity to map the value of this field.
wrapper.mapping("wu-la", FieldType.TEXT, Analyzer.IK_MAX_WORD, Analyzer.IK_MAX_WORD);
//Set the fragmentation and copy information, which can be defaulted.
wrapper.settings(3, 2);
//Set alias information, which can be defaulted.
String aliasName = "daily";
wrapper.createAlias(aliasName);
//Set the parent-child information. If there is no parent-child document relationship, it is not necessary to set it.
wrapper.join("joinField", "document", "comment");
//Create an index
boolean isOk = documentMapper.createIndex(wrapper);
Assertions.assertTrue(isOk);
}
/**
* Variant of Mode 3 is the most difficult to use, but also the most flexible.
*/
@Test
public void testCreateIndexByMap() {
//Demonstrate that creating an index through a custom map is the most flexible and can support all indexing scenarios that es itself can support.
LambdaEsIndexWrapper<Document> wrapper = new LambdaEsIndexWrapper<>();
wrapper.indexName(Document.class.getSimpleName().toLowerCase());
wrapper.settings(3, 2);
Map<String, Object> map = new HashMap<>();
Map<String, Object> prop = new HashMap<>();
Map<String, String> field = new HashMap<>();
field.put("type", FieldType.KEYWORD.getType());
prop.put("this_is_field", field);
map.put("properties", prop);
wrapper.mapping(map);
boolean isOk = documentMapper.createIndex(wrapper);
Assertions.assertTrue(isOk);
}
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Query index
Introduction to API
//Is there an index?
Boolean existsIndex(String indexName);
//Get the index information corresponding to the current mapper
GetIndexResponse getIndex();
//Get the specified index information
GetIndexResponse getIndex(String indexName);
2
3
4
5
6
7
8
Use case:
@Test
public void testExistsIndex() {
//Tests whether an index with the specified name exists.
String indexName = Document.class.getSimpleName().toLowerCase();
boolean existsIndex = documentMapper.existsIndex(indexName);
Assertions.assertTrue(existsIndex);
}
@Test
public void testGetIndex() {
GetIndexResponse indexResponse = documentMapper.getIndex();
//Print the index structure information here, and other pieces of information can be taken from the indexResponse.
indexResponse.getMappings().forEach((k, v) -> System.out.println(v.getSourceAsMap()));
}
2
3
4
5
6
7
8
9
10
11
12
13
14
# Update index
Introduction to API
//Update the index according to the conditions
Boolean updateIndex(Wrapper<T> wrapper);
2
Use case:
/**
:: Update the index
*/
@Test
public void testUpdateIndex() {
//Test update index
LambdaEsIndexWrapper<Document> wrapper = new LambdaEsIndexWrapper<>();
//Specify which index to update.
String indexName = Document.class.getSimpleName().toLowerCase();
wrapper.indexName(indexName);
wrapper.mapping(Document::getCreator, FieldType.KEYWORD);
wrapper.mapping(Document::getGmtCreate, FieldType.DATE);
boolean isOk = documentMapper.updateIndex(wrapper);
Assertions.assertTrue(isOk);
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Delete index
Introduction to API
//Deleting the specified index supports deleting multiple indexes at the same time. Be cautious and take the consequences at your own risk. After deleting the indexes, the data will also be deleted together, similar to deleting the database in Mysql. ...
Boolean deleteIndex(String... indexNames);
2
Use case:
@Test
public void testDeleteIndex() {
//Specify which index to delete.
String indexName = Document.class.getSimpleName().toLowerCase();
boolean isOk = documentMapper.deleteIndex(indexName);
Assertions.assertTrue(isOk);
}
2
3
4
5
6
7
API introduction
// Refresh the index corresponding to the current mapper and return the number of successfully refreshed shards
Integer refresh();
// Refresh the specified index list and return the total number of successfully refreshed shards
Integer refresh(String... indexNames);
2
3
4
5
Use Cases:
@Test
public void testRefresh() {
// Refresh the index corresponding to the current mapper
int successShardsCount = documentMapper.refresh();
}
2
3
4
5