PHP 的模糊搜尋庫
這是很棒的 Fuse.js 專案的 PHP 端口,旨在盡可能提供完整的 API 相容性。
查看他們的演示和範例,以充分了解該庫的功能。
最新相容的 Fuse.js 版本:7.0.0
目錄:
該軟體包可透過 Composer 取得。要將其添加到您的專案中,只需運行:
composer require loilo/fuse
請注意,使用 Fuse 至少需要 PHP 7.4。
這是一個簡單的使用範例:
<?php
require_once ' vendor/autoload.php ' ;
$ list = [
[
' title ' => " Old Man's War " ,
' author ' => ' John Scalzi ' ,
],
[
' title ' => ' The Lock Artist ' ,
' author ' => ' Steve Hamilton ' ,
],
[
' title ' => ' HTML5 ' ,
' author ' => ' Remy Sharp ' ,
],
[
' title ' => ' Right Ho Jeeves ' ,
' author ' => ' P.D Woodhouse ' ,
],
];
$ options = [
' keys ' => [ ' title ' , ' author ' ],
];
$ fuse = new Fuse Fuse ( $ list , $ options );
$ fuse -> search ( ' hamil ' );
這會產生以下結果(其中每個結果的item
引用匹配的條目本身,並且refIndex
提供該項目在原始$list
中的位置):
[
[
' item ' => [
' title ' => ' The Lock Artist ' ,
' author ' => ' Steve Hamilton ' ,
],
' refIndex ' => 1 ,
],
[
' item ' => [
' title ' => ' HTML5 ' ,
' author ' => ' Remy Sharp ' ,
],
' refIndex ' => 2 ,
],
];
Fuse 有很多選項可以優化您的搜尋:
isCaseSensitive
bool
false
指示比較是否應區分大小寫。
includeScore
bool
false
分數是否應包含在結果集中。 0
分錶示完全匹配, 1
分錶示完全不匹配。
includeMatches
bool
false
匹配是否應包含在結果集中。當true
時,結果集中的每筆記錄將包含符合字元的索引。因此,這些可以用於突出顯示的目的。
minMatchCharLength
int
1
僅傳回長度超過該值的符合項。 (例如,如果您想忽略結果中的單一字元匹配,請將其設為2
)。
shouldSort
bool
true
是否按分數對結果清單進行排序。
findAllMatches
bool
false
當為 true 時,匹配函數將繼續到搜尋模式的末尾,即使已在字串中找到完美匹配。
keys
array
[]
將搜尋的鍵列表。這支援嵌套路徑、加權搜尋、字串和物件數組中的搜尋。
location
int
0
確定預期在文本中找到的模式的大概位置。
threshold
float
0.6
匹配演算法在什麼時候放棄。閾值0.0
需要完美匹配(字母和位置),閾值1.0
可以匹配任何內容。
distance
int
100
決定匹配與模糊位置(由location
指定)的接近程度。與模糊位置distance
字元較遠的精確字母匹配將被評分為完全不匹配。 distance
0
要求符合位於指定的精確location
。距離1000
需要使用threshold
0.8
找到location
的800
字元以內的完美匹配。
ignoreLocation
bool
false
當true
時,搜尋將忽略location
和distance
,因此模式出現在字串中的位置並不重要。
提示:預設選項僅搜尋前 60 個字元。如果合理預期匹配在此範圍內,則這應該足夠了。若要修改此行為,請設定
location
、threshold
、distance
(或ignoreLocation
)的適當組合。為了更了解這些選項如何協同工作,請閱讀 Fuse.js 的評分理論。
useExtendedSearch
bool
false
當true
時,它允許使用類 UNIX 搜尋命令。參見範例。
getFn
callable
用於在提供的路徑中檢索物件值的函數。預設情況下也會搜尋嵌套路徑。
sortFn
callable
用於對所有結果進行排序的函數。預設將依相關性分數升序、索引升序排序。
ignoreFieldNorm
bool
false
當true
時,相關性分數(用於排序)的計算將忽略欄位長度範數。
提示:將
ignoreFieldNorm
設為true
唯一有意義的情況是,無論有多少個術語,只要查詢術語存在即可。
fieldNormWeight
float
1
決定欄位長度範數對評分的影響程度。值0
相當於忽略字段長度範數。值為0.5
將大大降低字段長度範數的影響,而值為2.0
將大大增加它。
您可以透過config
方法存取和操作上述所有選項的預設值:
// Get an associative array of all options listed above
Fuse :: config ();
// Merge associative array of options into default config
Fuse :: config ([ ' shouldSort ' => false ]);
// Get single default option
Fuse :: config ( ' shouldSort ' );
// Set single default option
Fuse :: config ( ' shouldSort ' , false );
每個FuseFuse
實例都可以使用下列方法:
search
搜尋整個文件集合,並傳回搜尋結果清單。
public function search( mixed $ pattern , ? array $ options ): array
$pattern
可以是以下之一:
$options
:
limit
(type: int
):表示傳回搜尋結果的最大數量。setCollection
設定/取代整個文件集合。如果未提供索引,則會產生一個。
public function setCollection( array $ docs , ? Fuse Core FuseIndex $ index ): void
例子:
$ fruits = [ ' apple ' , ' orange ' ];
$ fuse = new Fuse ( $ fruits );
$ fuse -> setCollection ([ ' banana ' , ' pear ' ]);
add
將文件新增至集合並相應地更新索引。
public function add( mixed $ doc ): void
例子:
$ fruits = [ ' apple ' , ' orange ' ];
$ fuse = new Fuse ( $ fruits );
$ fuse -> add ( ' banana ' );
sizeof ( $ fruits ); // => 3
remove
從清單中刪除謂詞傳回 true 的所有文檔,並傳回已刪除文檔的陣列。使用兩個參數呼叫謂詞: ($doc, $index)
。
public function remove(? callable $ predicate ): array
例子:
$ fruits = [ ' apple ' , ' orange ' , ' banana ' , ' pear ' ];
$ fuse = new Fuse ( $ fruits );
$ results = $ fuse -> remove (fn( $ doc ) => $ doc === ' banana ' || $ doc === ' pear ' );
sizeof ( $ fuse -> getCollection ()); // => 2
$ results ; // => ['banana', 'pear']
removeAt
刪除指定索引處的文件。
public function removeAt( int $ index ): void
例子:
$ fruits = [ ' apple ' , ' orange ' , ' banana ' , ' pear ' ];
$ fuse = new Fuse ( $ fruits );
$ fuse -> removeAt ( 1 );
$ fuse -> getCollection (); // => ['apple', 'banana', 'pear']
getIndex
傳回產生的 Fuse 索引。
public function getIndex(): Fuse Core FuseIndex
例子:
$ fruits = [ ' apple ' , ' orange ' , ' banana ' , ' pear ' ];
$ fuse = new Fuse ( $ fruits );
$ fuse -> getIndex ()-> size (); // => 4
每個FuseFuse
實例都可以使用下列方法:
Fuse::createIndex
從清單中預先產生索引,並將其直接傳遞到 Fuse 實例中。如果列表(相當)大,它會加快實例化速度。
public static function createIndex( array $ keys , array $ docs , array $ options = []): Fuse Core FuseIndex
例子:
$ list = [ ... ]; // See the example from the 'Usage' section
$ options = [ ' keys ' => [ ' title ' , ' author.firstName ' ] ];
// Create the Fuse index
$ myIndex = Fuse :: createIndex ( $ options [ ' keys ' ], $ list );
// Initialize Fuse with the index
$ fuse = new Fuse ( $ list , $ options , $ myIndex );
Fuse::parseIndex
解析 JSON 序列化的 Fuse 索引。
public static function parseIndex( array $ data , array $ options = []): Fuse Core FuseIndex
例子:
// (1) When the data is collected
$ list = [ ... ]; // See the example from the 'Usage' section
$ options = [ ' keys ' => [ ' title ' , ' author.firstName ' ] ];
// Create the Fuse index
$ myIndex = Fuse :: createIndex ( $ options [ ' keys ' ], $ list );
// Serialize and save it
file_put_contents ( ' fuse-index.json ' , json_encode ( $ myIndex ));
// (2) When the search is needed
// Load and deserialize index to an array
$ fuseIndex = json_decode ( file_get_contents ( ' fuse-index.json ' ), true );
$ myIndex = Fuse :: parseIndex ( $ fuseIndex );
// Initialize Fuse with the index
$ fuse = new Fuse ( $ list , $ options , $ myIndex );
Fuse.js | PHP熔斷器 | |
---|---|---|
取得保險絲版本 | Fuse.version | – |
存取全域配置 | Fuse.config 屬性 | Fuse::config 方法 |
清單修改 | 使用fuse.add() 等修改傳遞給new Fuse 建構子的原始列表。 | 在 PHP 中,陣列是一種原始資料類型,這表示 Fuse 永遠不會修改您的原始清單。若要在新增/刪除項目後接收目前列表,可以使用$fuse->getCollection() 方法。 |
請注意,我正在努力實現與 Fuse.js 的功能對等,因此不會為 Fuse.js 本身未反映的搜尋邏輯添加功能或修復。
如果您對搜尋結果有任何問題,但這些問題在這個 PHP 端口中並不是明顯的錯誤,並且您碰巧了解JavaScript,請檢查您的用例是否在Fuse.js 的線上演示中正常工作,因為這是規範的Fuse 實作。如果問題也出現在那裡,請在他們的儲存庫中開啟一個問題。
要開始在 Fuse 上進行開發,您需要 git、PHP (≥ 7.4) 和 Composer。
由於程式碼是使用 Prettier 格式化的,因此也建議安裝 Node.js/npm 以及使用支援 Prettier 格式化的編輯器。
克隆存儲庫並cd
到其中:
git clone https://github.com/loilo/fuse.git
cd fuse
安裝 Composer 依賴項:
composer install
安裝 npm 依賴項(可選但建議)。這僅在程式碼格式化時需要,因為 npm 依賴項包括該專案使用的 Prettier 插件。
npm ci
該項目有不同類型的程式碼檢查。所有這些都在提交拉取請求時運行,但也可以在本地運行:
命令 | 目的 | 描述 |
---|---|---|
vendor/bin/phpcs | 檢查程式碼風格 | 執行 PHP_CodeSniffer 以驗證 Fuse 原始碼是否遵循 PSR-12 編碼樣式。 |
vendor/bin/psalm | 靜態分析 | 針對程式碼庫執行 Psalm 以避免與類型相關的錯誤和不安全的編碼模式。 |
vendor/bin/phpunit | 檢查程序邏輯 | 從test 資料夾執行所有 PHPUnit 測試。 |
在提交拉取請求之前,請將相關測試新增至test
資料夾。