Skip to content

Commit

Permalink
Add compound full-text index search capabilities
Browse files Browse the repository at this point in the history
Previously there was no support for compound full-text index search
capabilities. This extension handles these indexes by allowing
sub-arrays within the existing `SearchUsingFullText` attribute.

Example:
Assuming we have a 2 column full-text index ("col_a", "col_b")
and a single full-text index "col_b":

`#SearchUsingFullText([["col_a", "col_b"], "col_c"])`

will result in a SQL query like:

  MATCH (`t`.`col_a`, `t`.`col_b`) AGAINST ('term' IN <NLM>)
  OR MATCH (`t`.`col_c`) AGAINST ('term' IN <NLM>)

NOTE: Previous usages of this attribute will be not affected by this
      change.
  • Loading branch information
jpedryc committed Nov 1, 2024
1 parent 641c45c commit 5e5e6aa
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/Engines/DatabaseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,39 @@ protected function initializeSearchQuery(Builder $builder, array $columns, array

$likeOperator = $connectionType == 'pgsql' ? 'ilike' : 'like';

$fullTextColumnsChecked = [];

foreach ($columns as $column) {
if (in_array($column, $fullTextColumns)) {
if (in_array($column, $fullTextColumnsChecked)) {
continue;
}

if (in_array($column, Arr::flatten($fullTextColumns))) {
$fullTextColumnTarget = Arr::first(
$fullTextColumns,
fn ($entry) => is_array($entry)
? in_array($column, $entry)
: $entry === $column
);

// Handle full-text query depending on given type (string - single index; array - compound index)
if (! is_array($fullTextColumnTarget)) {
$query->orWhereFullText(
$builder->model->qualifyColumn($column),
$builder->query,
$this->getFullTextOptions($builder)
);

continue;
}

$query->orWhereFullText(
$builder->model->qualifyColumn($column),
Arr::map($fullTextColumnTarget, fn ($target) => $builder->model->qualifyColumn($target)),
$builder->query,
$this->getFullTextOptions($builder)
);

$fullTextColumnsChecked += $fullTextColumnTarget;
} else {
if ($canSearchPrimaryKey && $column === $builder->model->getScoutKeyName()) {
continue;
Expand Down

0 comments on commit 5e5e6aa

Please sign in to comment.