⏸ 開發結束-開放移交項目
不幸的是,我缺乏時間和精力來積極維護這個插件的開發。我暫時將插件按原樣存檔。請注意,它不支援 Kirby 3.6+。如果有人想接手並繼續開發這個插件,我會非常高興 - 請與我們聯繫。
下載、解壓縮此儲存庫並將其複製到/site/plugins/search
。
或者,您可以使用 Composer 安裝它:
composer require distantnative/search-for-kirby
決定您要使用哪個提供者(請參閱下文以了解可用的提供者)。如果使用 Sqlite 或 Algolia 提供程序,請不要忘記在第一次運行之前建立索引(也在下面)。
將/site/plugins/search
資料夾替換為新版本。請務必閱讀發行說明以了解重大變更。
或者,如果您透過 Composer 安裝了插件,請執行:
composer update distantnative/search-for-kirby
該插件提供跨不同條目組合的全域搜尋: pages
、 files
和users
。
您可以定義應包含在索引中的項目(使用 Kirby 查詢語言),甚至可以在site/config/config.php
中完全停用某個類型:
' search ' => [
' entries ' => [
' pages ' => ' site.find("projects").index ' ,
' files ' => false ,
// users will remain the default
]
]
預設情況下,索引中包含以下集合:
pages
site.index
files
site.index.files
users
kirby.users
您可以在config.php
檔案中定義要包含在搜尋中的頁面、檔案和使用者的內容欄位:
' search ' => [
' fields ' => [
' pages ' => [
' title '
],
' files ' => [
' filename '
],
' users ' => [
' email ' ,
' name '
]
]
]
有多種選項可用於定義這些陣列中的欄位:
// Simply add the field
' title ' ,
// Add the field, but run it through a field method first
' text ' => ' kirbytext ' ,
// Pass parameters to the field method
' text ' => [ ' short ' , 50 ],
// Use a callback function
' text ' => function ( $ model ) {
return strip_tags ( $ model -> text ()-> kt ());
}
// Turn string field value into number (e.g. for Algolia filters)
' myNumberField ' => function ( $ model ) {
return $ model -> myNumberField ()-> toFloat ();
}
' myVirtualNumberField ' => function ( $ model ) {
return $ model -> myNumberField ()-> toInt () + 5 ;
}
' myDate ' => function ( $ model ) {
return $ model -> anyDateField ()-> toTimestamp ();
}
您也可以在config.php
檔案中定義要包含在搜尋中的範本(或使用者情況下的角色):
' search ' => [
' templates ' => [
' pages ' => function ( $ model ) {
return $ model -> id () !== ' home ' && $ model -> id () !== ' error ' ;
},
' files ' => null ,
' users ' => null
]
]
``
If the value is null or an empty array, all templates are allowed. If it is false, all templates are excluded. There are several other options:
```php
// simple whitelist array
[ ' project ' , ' note ' , ' album ' ]
// associative array
[
' project ' => true ,
' note ' => false
]
// callback function
function ( $ model ) {
return $ model -> intendedTemplate () !== ' secret ' ;
}
該插件用其全域搜尋模式取代了預設的面板搜尋(透過放大鏡圖示在右上角存取):
該插件還替換了 PHP API 方法。由於該插件提供全局搜索,因此最好與 $site 物件一起使用作為開始:
$ site -> search ( ' query ' , $ options = []);
儘管如此,搜尋也可以僅限於更具體的集合(有一些效能損失):
$ page -> children ()-> listed ()-> filterBy ( ' template ' , ' project ' )-> search ( ' query ' , $ options = []);
該插件還新增了一個全域search()
輔助函數:
search (string $ query , $ options = [], $ collection = null )
對於選項數組,您可以傳遞一個限制選項來指定要傳回的結果數。此外,您可以指定運算子選項( AND
或OR
)來指定組合多個搜尋字詞的規則:
collection ( ' notes ' )-> search ( $ query , [
' operator ' => ' AND ' ,
' limit ' => 100
]);
您收到的結果是一個KirbyCmsCollection
物件。
該插件捆綁了三個不同的搜尋提供者。根據您的網站、需求和設置,其中之一可能比其他更合適。
sqlite
(預設)使用 SQLite FTS5 擴充功能(必須可用)建立索引 SQLite 資料庫。
在配置中,您可以重新定義資料庫檔案的位置,提供絕對路徑。預設情況下,資料庫檔案將會建立為site/logs/search/index.sqlite
。
// site/config/config.php
' search ' => [
' provider ' => ' sqlite ' ,
' sqlite ' => [
' file ' => dirname ( __DIR__ , 2 ) . ' /storage/search/index.sqlite '
]
]
模糊選項允許搜尋不僅從單字的開頭匹配內容,還可以匹配單字內部的內容。根據內容的不同,它可以極大地增加索引資料庫的大小(以指數方式思考),特別是在包含長文本欄位時。您也可以定義欄位清單以進行模糊處理。
// site/config/config.php
' search ' => [
' provider ' => ' sqlite ' ,
' sqlite ' => [
// Enabled for all fields
' fuzzy ' => true ,
// Disabled completely
' fuzzy ' => false ,
// Only for selected fields
' fuzzy ' => [
' pages ' => [ ' title ' ],
' files ' => [ ' caption ' , ' credits ' ]
],
]
]
您也可以定義一些自訂權重來將特定欄位排名高於其他欄位:
// site/config/config.php
' search ' => [
' provider ' => ' sqlite ' ,
' sqlite ' => [
' weights ' => [
' title ' => 10 ,
' caption ' => 5
],
]
]
algolia
透過此提供者將 Algolia 搜尋無縫添加到 Kirby:
// site/config/config.php
' search ' => [
' provider ' => ' algolia ' ,
' algolia ' => [
' app ' => . . . , // Algolia App ID
' key ' => . . . , // Algolia private admin key (not just read-only!)
' index ' => ' kirby ' // name of the index to use/create
' options ' => [] // options to pass to Algolia at search
]
]
為了建立初始索引,該外掛程式捆綁了一個小面板部分,您可以從中觸發建立索引。將以下部分加入例如您的site.yml
藍圖:
sections :
search :
type : search
預設情況下,該外掛程式可確保透過掛鉤在每個事件(建立頁面、上傳檔案、更新內容等)時更新搜尋索引。因此,您無需擔心每次編輯內容時都要建立新索引。
若要透過掛鉤停用自動索引更新,請將以下內容新增至您的site/config/config.php
:
' search ' => [
' hooks ' => false
]
您也可以使用插件中包含的./index
shell 腳本從命令列建立索引。這可以是面板部分的替代方案(特別是對於非常大的索引檔案),或者替換不使用面板的設定中的掛鉤 - 然後在部署新提交或透過 cronjob 時觸發。
./site/plugins/search/bin/index
如果您使用自訂資料夾設置,則必須建立該腳本的修改版本。如果您需要協助,請聯絡我們。
該插件“按原樣”提供,不提供任何保證。使用它的風險由您自行承擔,並且在生產環境中使用它之前請務必自行測試。如果您遇到任何問題,請建立問題。
產生索引時,您可能會遇到找不到驅動程式的錯誤。這很可能意味著您的伺服器設定缺少 Sqlite 擴充功能。其他用戶已成功啟動以下擴充功能:
extension=sqlite3.so
extension=pdo_sqlite.so
該插件完全免費,並在 MIT 許可下發布。然而,發展需要時間和努力。如果您在商業項目中使用它,或者只是想支持我讓這個插件保持活力,請根據您的選擇進行捐贈。