Basic query
$results = User::search('Ada')->get();
$results = User::search('Ada')->take(10)->get();
The engine sends a RediSearch query and uses the returned identifiers to load Eloquent models through Scout.
Prefix matching
Prefix matching is enabled by default. A search for Ad is compiled as Ad*,
so it matches names such as Ada and Adam:
$results = User::search('Ad')->get();
Disable it globally with SCOUT_REDIS_SEARCH_PREFIX_MATCHING=false or for one
query:
$results = User::search('Ad')
->options(['prefix_matching' => false])
->get();
prefix_min_length defaults to 2 and can be configured globally or per query.
Fuzzy search
Use a fuzzy level from 1 to 3. Fuzzy search uses RediSearch’s edit-distance
syntax and does not add the prefix wildcard:
$results = User::search('Ado')
->options(['fuzzy' => 1])
->get();
Higher levels are broader and may be more expensive.
Search specific fields
Restrict a query to one or more indexed fields:
$results = User::search('Ada')
->options(['fields' => ['name']])
->get();
Filtering
Use TAG fields for exact values:
$results = Article::search('Laravel')
->where('category', 'docs')
->take(20)
->get();
$results = Article::search('Laravel')
->whereIn('category', ['docs', 'guide'])
->get();
Use NUMERIC fields for comparisons:
$results = User::search('')
->where('id', '>=', 100)
->where('id', '<', 500)
->get();
Supported operators are =, !=, >, >=, <, and <=. An empty search
string performs a filter-only query.
Sorting
Mark the field as SORTABLE in the schema before sorting:
$results = User::search('Ada')
->orderByDesc('id')
->get();
The engine supports one orderBy clause per query.
Pagination and collections
$page = User::search($request->string('q')->toString())->paginate(15);
$models = User::search('Ada')->cursor();
$ids = User::search('Ada')->keys();
paginate() preserves the total returned by RediSearch and loads only the
requested page of Eloquent models.
Query options
$results = User::search('Ada Lovelace')
->options([
'fields' => ['name'],
'fuzzy' => 0,
'verbatim' => true,
'in_order' => true,
'language' => 'English',
'dialect' => 2,
])
->get();
| Option | Description |
|---|---|
fields |
Restrict matching to the listed fields |
prefix_matching |
Enable or disable suffixing terms with * |
prefix_min_length |
Minimum term length before prefix matching |
fuzzy |
Fuzzy matching level from 0 to 3 |
verbatim |
Disable stemming with VERBATIM |
in_order |
Require terms to appear in order |
language |
Set the query language |
dialect |
Set the RediSearch query dialect |
raw_query |
Send the search string as RediSearch query syntax |
For trusted RediSearch syntax, set raw_query:
$results = User::search('@name:(Ada|Grace)')
->options(['raw_query' => true])
->get();
Raw queries bypass escaping and must never contain untrusted user input.
Custom index
Use Scout’s within() method when a model should query another configured index:
$results = User::search('Ada')
->within('users_archive')
->get();