Fuzzy-search plugin for Kirby. Searching for non-exact matches in your content has never been this easy.
This is plugin is built on top of the fuzzget PHP library.
Basic Usage
If you are already using the Kirby built-in search
method, replacing it with Fuzzy Search is just a matter of renaming the search
method to fuzzySearch
like shown below:
$query = get('q');
$articles = page('blog')
->children()
->visible()
- ->search($query, 'title|text');
+ ->fuzzySearch($query, 'title|text');
Fuzzy Search is not compatible with any of the other options available on the Kirby search
method.
With Fuzzy Search you can also search through custom page methods or page models. You only need to include the method name in the fuzzySearch
last parameter.
// site/plugins/methods.php
page::$methods['authorName'] = function($page) {
$author = $page->author()->value();
if ($user = site()->user($author)) {
return $user->firstname().' '.$user->lastname();
}
};
$articles = page('blog')
->children()
->visible();
if ($query = get('q')) {
$articles = $articles->fuzzySearch($query, 'title|text|authorName');
}
Searching on structured fields
Fuzzy Search ships with a handy field method that allows you to search on structured fields.
$result = page('faq')
->topics()
->fuzzySearch($query, 'question|answer');
The $result
will also be a Field
object and not just a simple array. That way you are free to chain any Field method, such as toStructure
, yaml
, or isEmpty
, after getting the search result.
$result = page('contact')
->addresses()
->fuzzySearch($query, 'city')
->toStructure();
Searching on arrays
You also can use the fuzzySearch
function to search through an array of associative arrays.
$countries = [
['name' => 'Australia'],
['name' => 'Brazil'],
['name' => 'Canada'],
['name' => 'France'],
['name' => 'Germany'],
['name' => 'Portugal'],
['name' => 'United Kingdom'],
['name' => 'United States']
];
$results = fuzzySearch($countries, 'Brasil');
Advanced Usage
If you leave out the last parameter, Fuzzy Search will search through all keys (fields) in the provided data:
site()->index()->fuzzySearch($query);
It’s the same as using the *
wildcard.
site()->index()->fuzzySearch($query, '*');
Fuzzy Search is very flexible when it comes to choosing in which fields it should look for matches. Check out the other options:
Include
If you want to search for a given term only in the title
and text
fields, just pass their names in the last parameter separated by |
:
site()->index()->fuzzySearch($query, 'title|text');
This is syntax sugar for:
site()->index()->fuzzySearch($query, [
'include' => ['title', 'text']
]);
Ignore
Of course you can also list fields which you do not want to be searched:
site()->index()->fuzzySearch($query, '-author|-date');
The example above is the same as:
site()->index()->fuzzySearch($query, [
'ignore' => ['author', 'date']
]);
In this case, all fields will be considered in the search except for author
and date
fields.
If you need to include a custom page method or page model method, you can combine it with the wildcard and ignore syntax.
site()->index()->fuzzySearch($query, '*|authorName|-date');
In this example, all fields but the date
field will be searched on. $page->authorName()
will be used if it is either a custom page method or page model method.
Try it out and please send your feedback.