Skip to content

Commit 5e5e6aa

Browse files
committed
Add compound full-text index search capabilities
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.
1 parent 641c45c commit 5e5e6aa

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/Engines/DatabaseEngine.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,39 @@ protected function initializeSearchQuery(Builder $builder, array $columns, array
206206

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

209+
$fullTextColumnsChecked = [];
210+
209211
foreach ($columns as $column) {
210-
if (in_array($column, $fullTextColumns)) {
212+
if (in_array($column, $fullTextColumnsChecked)) {
213+
continue;
214+
}
215+
216+
if (in_array($column, Arr::flatten($fullTextColumns))) {
217+
$fullTextColumnTarget = Arr::first(
218+
$fullTextColumns,
219+
fn ($entry) => is_array($entry)
220+
? in_array($column, $entry)
221+
: $entry === $column
222+
);
223+
224+
// Handle full-text query depending on given type (string - single index; array - compound index)
225+
if (! is_array($fullTextColumnTarget)) {
226+
$query->orWhereFullText(
227+
$builder->model->qualifyColumn($column),
228+
$builder->query,
229+
$this->getFullTextOptions($builder)
230+
);
231+
232+
continue;
233+
}
234+
211235
$query->orWhereFullText(
212-
$builder->model->qualifyColumn($column),
236+
Arr::map($fullTextColumnTarget, fn ($target) => $builder->model->qualifyColumn($target)),
213237
$builder->query,
214238
$this->getFullTextOptions($builder)
215239
);
240+
241+
$fullTextColumnsChecked += $fullTextColumnTarget;
216242
} else {
217243
if ($canSearchPrimaryKey && $column === $builder->model->getScoutKeyName()) {
218244
continue;

0 commit comments

Comments
 (0)