Skip to content

Commit

Permalink
Add configurable Levenshtein distance for fuzzy search (#124)
Browse files Browse the repository at this point in the history
* Add configurable Levenshtein distance for fuzzy search

* Style fix
  • Loading branch information
ViliusS authored Oct 31, 2021
1 parent b4dd092 commit 60562d6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ built_in_search_page: true
enable_admin_page_events: true
search_type: auto
fuzzy: false
distance: 2
phrases: true
stemmer: 'no'
display_route: true
Expand Down Expand Up @@ -103,6 +104,7 @@ The configuration options are as follows:
* `boolean` - supports `or` or `minus`. e.g. `foo -bar`
* `auto` - automatically detects whether to use `basic` or `boolean`
* `fuzzy` - matches if the words are 'close' but not necessarily exact matches
* `distance` - Levenshtein distance of fuzzy search. It represents the amount of characters which need to be changed, removed, or added in a word in order it to match the search keyword. Increasing the distance produces more search results but decreases the accuracy of the search.
* `phrases` - automatically handle phrases support
* `stemmer` - can be one of these types:
* `no` - no stemmer
Expand Down
7 changes: 7 additions & 0 deletions blueprints.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ form:
validate:
type: bool

distance:
type: number
size: x-small
label: Levenshtein distance of fuzzy search
help: It represents the amount of characters which need to be changed, removed, or added in a word in order it to match the search keyword. Increasing the distance produces more search results but decreases the accuracy of the search.
default: 2

phrases:
type: toggle
label: Match quoted phrases
Expand Down
3 changes: 3 additions & 0 deletions classes/GravTNTSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function __construct($options = [])

$search_type = $config->get('plugins.tntsearch.search_type', 'auto');
$fuzzy = $config->get('plugins.tntsearch.fuzzy', false);
$distance = $config->get('plugins.tntsearch.distance', 2);
$stemmer = $config->get('plugins.tntsearch.stemmer', 'no');
$limit = $config->get('plugins.tntsearch.limit', 20);
$snippet = $config->get('plugins.tntsearch.snippet', 300);
Expand All @@ -66,6 +67,7 @@ public function __construct($options = [])
'json' => false,
'search_type' => $search_type,
'fuzzy' => $fuzzy,
'distance' => $distance,
'stemmer' => $stemmer,
'limit' => $limit,
'as_you_type' => true,
Expand Down Expand Up @@ -99,6 +101,7 @@ public function search($query)

if (isset($this->options['fuzzy']) && $this->options['fuzzy']) {
$this->tnt->fuzziness = true;
$this->tnt->fuzzy_distance = $this->options['distance'];
}

$limit = (int)$this->options['limit'];
Expand Down
1 change: 1 addition & 0 deletions tntsearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ built_in_search_page: true
enable_admin_page_events: true
search_type: auto
fuzzy: false
distance: 2
phrases: true
stemmer: default
display_route: true
Expand Down

0 comments on commit 60562d6

Please sign in to comment.