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.

The implementations below are based on algorithms from fzf.

Implementations

FuzzyMatchV2Search

Port of fzf FuzzyMatchV2Search algorithm. Does a fast fuzzy search and is good for quickly finding paths.

ExactMatchNaive

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

SearchMatch

Algorithms and default syntax are hidden inside package-private classes as we don’t want to fully open these until we know APIs are good to go for longer support. You need to construct SearchMatch via its built-in builder.

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 normalized before search happens. Normalization is handy when different languages have slight variations for same type of characters.

Search algorithm is selected based on a search syntax shown in below table.

Table 1. Search syntax
Token Match type Description

hell

fuzzy-match

Items that match hello

'stuff

exact-match

Items that include stuff

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