From 60562d62856c114f23c183f7873fe1c809f4c7b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vilius=20=C5=A0umskas?= Date: Mon, 1 Nov 2021 00:07:34 +0200 Subject: [PATCH] Add configurable Levenshtein distance for fuzzy search (#124) * Add configurable Levenshtein distance for fuzzy search * Style fix --- README.md | 2 ++ blueprints.yaml | 7 +++++++ classes/GravTNTSearch.php | 3 +++ tntsearch.yaml | 1 + 4 files changed, 13 insertions(+) diff --git a/README.md b/README.md index f6a35ce..e9541ce 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/blueprints.yaml b/blueprints.yaml index 455e49b..faa3772 100644 --- a/blueprints.yaml +++ b/blueprints.yaml @@ -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 diff --git a/classes/GravTNTSearch.php b/classes/GravTNTSearch.php index 6f78e4f..864856d 100644 --- a/classes/GravTNTSearch.php +++ b/classes/GravTNTSearch.php @@ -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); @@ -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, @@ -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']; diff --git a/tntsearch.yaml b/tntsearch.yaml index c2647ab..c0df206 100644 --- a/tntsearch.yaml +++ b/tntsearch.yaml @@ -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