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

Added the ability to pass an array of options to full-text search. #606

Merged
merged 3 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 23 additions & 0 deletions src/Attributes/SearchUsingOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Laravel\Scout\Attributes;

use Attribute;
use Illuminate\Support\Arr;

#[Attribute]
class SearchUsingOptions
{
/**
* The full-text search options.
*/
public $options = [];

/**
* Create a new attribute instance.
*/
public function __construct($options)
{
$this->options = Arr::wrap($options);
}
}
15 changes: 14 additions & 1 deletion src/Engines/DatabaseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ protected function initializeSearchQuery(Builder $builder, array $columns, array
if (in_array($column, $fullTextColumns)) {
$query->orWhereFullText(
$builder->model->qualifyColumn($column),
$builder->query
$builder->query,
$this->getFullTextOptions($builder)
);
} else {
if ($canSearchPrimaryKey && $column === $builder->model->getKeyName()) {
Expand Down Expand Up @@ -280,6 +281,18 @@ protected function getAttributeColumns(Builder $builder, $attributeClass)
return $columns;
}

/**
* Get the full-text search options for the query.
*/
protected function getFullTextOptions(Builder $builder)
{
$reflection = new ReflectionMethod($builder->model, 'toSearchableArray');

if ($attribute = collect($reflection->getAttributes(SearchUsingOptions::class))->first()) {
return collect($attribute->getArguments())->first();
}
}

/**
* Pluck and return the primary keys of the given results.
*
Expand Down