Search Algorithms

SearchMatch is an interface to match text with a pattern. Match results are in a returned value SearchMatchResult. Match result contains info about match positions and overall score of a match.spring-doc.cn

Implementations

FuzzyMatchV2Searchspring-doc.cn

Port of fzf FuzzyMatchV2Search algorithm. Does a fast fuzzy search and is good quickly finding paths.spring-doc.cn

ExactMatchNaivespring-doc.cn

Port of fzf ExactMatchNaive algorithm. Simple exact match works more accurately if you know what to search.spring-doc.cn

SearchMatch

Algorithms and default syntax are hidden inside package protected classes as we don’t want to fully open these until we know API’s are good to go for longer support. You need to construct SearchMatch via its build-in builder.spring-doc.cn

SearchMatch searchMatch = SearchMatch.builder()
	.caseSensitive(false)
	.normalize(false)
	.forward(true)
	.build();

It’s possible to configure case sensitivity, on what direction search happens or if text should be normilized before search happens. Normalization is handy when different languages have sligh variation for same type of characters.spring-doc.cn

Search algorithm is selected based on a search syntax shown in below table.spring-doc.cn

Table 1. Search syntax
Token Match type Description

hellspring-doc.cn

fuzzy-matchspring-doc.cn

Items that match hellospring-doc.cn

'stuffspring-doc.cn

exact-matchspring-doc.cn

Items that include stuffspring-doc.cn

Examples

SearchMatch searchMatch = SearchMatch.builder()
	.caseSensitive(false)
	.normalize(false)
	.forward(true)
	.build();

SearchMatchResult result = searchMatch.match("foo bar baz", "fbb");

result.getStart();
// 0 - start position inclusive
result.getEnd();
// 9 - end position exclusive
result.getPositions();
// 0,4,8 - positions, inclusive
result.getScore();
// 112 - score
result.getAlgorithm();
// FuzzyMatchV2SearchMatchAlgorithm - resolved algo