From 1e8db5da84c3572b896fcf32c79a5ef115dc8ce6 Mon Sep 17 00:00:00 2001 From: James Riordon Date: Tue, 25 Jul 2023 16:28:30 -0400 Subject: [PATCH] Add new config setting for minimum search length --- config/scout-fulltext-engine.php | 14 +++++++++++++- src/Parsers/Query/QueryParserMysqlFullTextBool.php | 6 ++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/config/scout-fulltext-engine.php b/config/scout-fulltext-engine.php index 7938a84..c60e891 100644 --- a/config/scout-fulltext-engine.php +++ b/config/scout-fulltext-engine.php @@ -37,6 +37,18 @@ // it can be useful for tuning fulltext searches // !!! only supported with bind_mode = 'join' 'add_select_score' => false, + + /* + |-------------------------------------------------------------------------- + | Default minimum search word length + |-------------------------------------------------------------------------- + | + | This option controls the default minimum search word length. Be sure to match + | this to the length you have set in your MySQL / MariaDB configuration. The + | default value for the MySQL / MariaDB search word length is 4 characters. + | + */ + 'ft_min_word_len' => 3 ], @@ -48,7 +60,7 @@ 'parser' => QueryParserMysqlFullTextBool::class, // Extractors will extrapolate metadata from the query text - + 'extractors' => [ [ // useful to match dotted words diff --git a/src/Parsers/Query/QueryParserMysqlFullTextBool.php b/src/Parsers/Query/QueryParserMysqlFullTextBool.php index cefaf32..05e83e4 100644 --- a/src/Parsers/Query/QueryParserMysqlFullTextBool.php +++ b/src/Parsers/Query/QueryParserMysqlFullTextBool.php @@ -81,12 +81,14 @@ public function runExtractors(string $tmpQuery): Collection public function filterTokens(Collection $tokens): Collection { - return $tokens->map(function (string $word) { + $minlength = config('scout-fulltext-engine.fulltext_options.ft_min_word_len'); + + return $tokens->map(function (string $word) use ($minlength) { if (Str::contains($word, static::DEF_RESERVED_CHARS)) { return '"' . trim(str_replace('"', ' ', $word)) . '"'; } else { return collect(preg_split("/[\\s[:punct:]]+/", $word)) - ->filter(fn ($s) => (strlen($s) > 2)); + ->filter(fn ($s) => (strlen($s) >= $minlength)); } })->flatten(); }