Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new config setting for minimum search length #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion config/scout-fulltext-engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
],


Expand All @@ -48,7 +60,7 @@
'parser' => QueryParserMysqlFullTextBool::class,

// Extractors will extrapolate metadata from the query text

'extractors' => [
[
// useful to match dotted words
Expand Down
6 changes: 4 additions & 2 deletions src/Parsers/Query/QueryParserMysqlFullTextBool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down