Match&fuzzy match
# Word Segmentation Matching Related Function API
match(boolean condition, R column, Object val);
matchPhase(boolean condition, R column, Object val, Float boost);
matchAllQuery();
matchPhrasePrefixQuery(boolean condition, R column, Object val, int maxExpansions, Float boost);
multiMatchQuery(boolean condition, Object val, Operator operator, int minimumShouldMatch, Float boost, R... columns);
queryStringQuery(boolean condition, String queryString, Float boost);
prefixQuery(boolean condition, R column, String prefix, Float boost);
2
3
4
5
6
7
attention
Note: The index type of the field that needs word segmentation matching must be text or keyword_text (this type is the default if it is not specified), and a word divider must be specified for it. The required word divider must be installed in advance, otherwise the es default word divider will be used, which is not good for Chinese support.
# Fuzzy matching related function API
//Pay attention to the wildcard query of es at the bottom of like, and the passed-in value will be spliced with the * wildcard by default, such as * old man *
like(boolean condition, String column, Object val, Float boost);
// likeLeft will splice the wildcard * such as * old man on the left side of the value.
likeLeft(boolean condition, String column, Object val, Float boost);
// likeRight will splice wildcard characters * such as Old Man * on the right side of the value.
likeRight(boolean condition, String column, Object val, Float boost);
2
3
4
5
6
# Query API and field index type reference
Students who don't know ES can refer to the following table for how to choose the above API and how to correctly establish the corresponding index:
ES native | Easy-Es | keyword type | text type | Does word segmentation support |
---|---|---|---|---|
$ text | EQ | Exact match | Query criteria must all be in the word segmentation, and they cannot be redundant. Multiple word segmentation must be continuous', and the order cannot be reversed | No |
wildcard | like/likeLeft/likeRight | Fuzzy matching like full fuzzy, like left fuzzy and like right fuzzy according to api | Not supported | No |
match | match | Exact match | As long as the word segmentation result of Match and the word segmentation result of text are the same, ` regardless of the order' | Yes |
matchPhrase | matchPhrase | Exact match | The word segmentation results of Matchphrase must be all contained' in the word segmentation of the text field and all in the same order, and must be all continuous `. | Yes |
matchPhrasePrefixquery | MatchPhrasePrefixquery | Not supported | MatchPhrasePrefix is the same as matchPhrase, except that it allows prefix matching on the last word of the text . | is |
MultimatchQuery | MultimatchQuery | Complete Match | Full-field Word Segmentation Matching, which can realize full-text retrieval function | Yes |
queryString query | querystring query | Exact match | At least one word segmentation result in querystring is in the word segmentation result in the text field, regardless of the order | Yes |
prefixQuery | prefixQuery | Exact Match | As long as there are entries in the segmented entries that meet the prefix conditions | Yes |
-Word segmentation matching -example: match("content ","Lao Wang ")-> The content contains the keyword" Lao Wang ".If the granularity of word segmentation is set fine, Lao Wang may be divided into" Lao "and" Wang ",so long as the content contains" Lao "or" Wang ",they can all be searched out. For other APIs, please refer to the following code examples.
# Code examples
@Test
public void testMatch(){
// The input will be divided into words, as long as there is a match in the content of all the words in the division, the data will be queried, regardless of the order of the words
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.match(Document::getContent, "technology");
List<Document> documents = documentMapper.selectList(wrapper);
System.out.println(documents.size());
}
@Test
public void testMatchPhrase() {
// will do the input word, but the results also need to contain all the word, and the same order, otherwise it will not be able to query the results
// For example, if the data in es is technically sound, if the search term is technically sound, the result will not be queried
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.matchPhrase(Document::getContent, "technical");
List<Document> documents = documentMapper.selectList(wrapper);
System.out.println(documents);
}
@Test
public void testMatchAllQuery() {
// Query all data, similar to mysql select all.
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.matchAllQuery();
List<Document> documents = documentMapper.selectList(wrapper);
System.out.println(documents);
}
@Test
public void testMatchPhrasePrefixQuery() {
// prefix match query The last word of the query string can only be used as a prefix
// Prefixes may match thousands of words, which not only consumes a lot of system resources, but also the results are not very useful, so you can provide the reference maxExpansions, if not written, the default is 50
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.matchPhrasePrefixQuery(Document::getCustomField, "Ullabala", 10);
List<Document> documents = documentMapper.selectList(wrapper);
System.out.println(documents);
}
@Test
public void testMultiMatchQuery() {
// Query the data containing Lao Wang from multiple specified fields
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.multiMatchQuery("Old King", Document::getTitle, Document::getContent, Document::getCreator, Document::getCustomField);
// where the default Operator is OR and the default minShouldMatch is 60% Both parameters can be adjusted on demand, which is supported by our api For example:
// where AND means that all search tokens must be matched, OR means that only one token can be matched. minShouldMatch 80 means only query data with a match greater than 80%.
// wrapper.multiMatchQuery("Old King",Operator.AND,80,Document::getCustomField,Document::getContent);
List<Document> documents = documentMapper.selectList(wrapper);
System.out.println(documents.size());
System.out.println(documents);
}
@Test
public void queryStringQuery() {
// query the data containing the keyword old man from all fields
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.queryStringQuery("old man");
List<Document> documents = documentMapper.selectList(wrapper);
System.out.println(documents);
}
@Test
public void prefixQuery() {
// Query all data whose creator starts with "next door", such as the king next door, the man next door, all can be found
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.prefixQuery(Document::getCreator, "next door");
List<Document> documents = documentMapper.selectList(wrapper);
System.out.println(documents);
}
@Test
public void like() {
// equivalent to MySQL like %han push% like old man push... can then be checked out
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.like(Document::getTitle, "HanTui");
List<Document> documents = documentMapper.selectList(wrapper);
System.out.println(documents);
}
@Test
public void likeLeft() {
// Equivalent to like %han in MySQL Like old man can be checked out
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.likeLeft(Document::getTitle, "Han");
List<Document> documents = documentMapper.selectList(wrapper);
System.out.println(documents);
}
@Test
public void likeRight() {
// Equivalent to like old % in MySQL like old man can be checked out
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.likeRight(Document::getTitle, "old");
List<Document> documents = documentMapper.selectList(wrapper);
System.out.println(documents);
}
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99