搜尋演算法

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
© . This site is unofficial and not affiliated with VMware.