搜索算法

SearchMatch 是一个用于将文本模式进行匹配的接口。匹配结果包含在返回的值 SearchMatchResult 中。匹配结果包含有关匹配位置和匹配总体分数的信息。

fzf.

实现

FuzzyMatchV2Search

fzf FuzzyMatchV2Search 算法的移植。执行快速模糊搜索,非常适合快速查找路径。

ExactMatchNaive

fzf ExactMatchNaive 算法的移植。简单的精确匹配,如果您知道要搜索什么,则可以更准确地工作。

SearchMatch

算法和默认语法隐藏在包保护类中,因为我们不想完全公开这些,直到我们知道 API 对于更长时间的支持是否良好。您需要通过其内置构建器来构造 SearchMatch

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

可以配置大小写敏感性,搜索发生的方向,或者在搜索发生之前是否应规范化文本。当不同语言对相同类型的字符略有变化时,规范化非常方便。

搜索算法是根据下表所示的搜索语法选择的。

表 1. 搜索语法
标记 匹配类型 描述

hell

模糊匹配

hello匹配的项目

'stuff

精确匹配

包含stuff的项目

示例

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