Index wrapper
# LambdaEsIndexWrapper
提示
The relevant conditions in the CRUD of the index depend on this constructor for wrapping
# API description
LambdaEsIndexWrapper provides the following APIs for use by users
mapping(R column, FieldType fieldType);
mapping(R column, FieldType fieldType, Boolean fieldData);
mapping(R column, FieldType fieldType, Float boost);
mapping(R column, FieldType fieldType, Boolean fieldData, Float boost);
mapping(R column, FieldType fieldType, String dateFormat);
mapping(R column, FieldType fieldType, String analyzer, String searchAnalyzer);
mapping(R column, FieldType fieldType, String analyzer, String searchAnalyzer, String dateFormat);
mapping(R column, FieldType fieldType, String analyzer, String searchAnalyzer, Float boost);
mapping(R column, FieldType fieldType, String analyzer, String searchAnalyzer, String dateFormat, Boolean fieldData, Float boost);
mapping(Map<String, Object> mapping);
createAlias(String aliasName);
settings(Settings settings);
settings(Integer shards, Integer replicas);
maxResultWindow(Integer maxResultWindow);
indexName(String... indexNames);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Use cases
/**
* LambdaEsIndexWrapper constructor makes test case one: creating indexes, created by out-of-the-box methods
*/
@Test
public void testCreateIndex() {
LambdaEsIndexWrapper<Document> wrapper = new LambdaEsIndexWrapper<>();
// For simplicity's sake, keep the index name the same as the entity class name, lowercase.
wrapper.indexName(Document.class.getSimpleName().toLowerCase());
// here the title of the article mapped to keyword type (does not support word separation), the document content mapped to text type (support for word separation query)
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);
// set the sharding and replica information, can be defaulted
wrapper.settings(3, 2);
// set alias information, can be defaulted
String aliasName = "daily";
wrapper.createAlias(aliasName);
// set the parent-child information, if there is no parent-child document relationship is not necessary to set
wrapper.join("joinField", "document", "comment");
// create an index
boolean isOk = documentMapper.createIndex(wrapper);
Assertions.assertTrue(isOk);
}
/**
* LambdaEsIndexWrapper constructor makes test case two: create index, by custom map The most difficult to use but also the most flexible
*/
@Test
public void testCreateIndexByMap() {
// demonstrate the creation of indexes by custom map, the most flexible, if option 1 can not meet this method
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);
}
/**
* LambdaEsIndexWrapper constructor to make test case three: update index (not recommended, because index changes ES will rebuild the index, there are other better ways, see the section on index hosting later)
*/
@Test
public void testUpdateIndex() {
// testUpdateIndex
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);
}
1
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
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
Help us improve this document (opens new window)
Last update: 2024/03/29