Skip to content
Closed
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Add config option for the matching strategy of multi-word queries

## [4.0.0] - 2026-08-06

- Upgrade loupe to 1.0
Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ release of Loupe improves them.
// Minimum word length to allow searching by prefix
'min_token_length_for_prefix_search' => 2,

// Strategy for multi-word queries, see "Matching strategy" section below
'matching_strategy' => 'any',

// Maximum number of results returned per search
'hits_per_page' => null,

Expand Down Expand Up @@ -137,6 +140,34 @@ an empty array to let Loupe detect the language of every document by itself:
],
```

## Matching strategy

The matching strategy decides whether a document has to contain every word of a query (`all`) or
whether any of them is enough (`any`). Loupe defaults to `any` and lets the ranking do the work:
a query like `winter tyre pressure` puts documents containing all three words on top, followed by
documents about winter tyres, followed by documents merely mentioning pressure. Nothing is
discarded, so `ranking_score_threshold` is the knob to cut off a long tail of weak matches.

With `all`, every additional word narrows the result set instead, and a query matching no document
returns nothing, no matter how close it was.

```diff
'indexes' => [
'default' => [
'driver' => 'loupe',
'searchables' => 'content',
+ 'matching_strategy' => 'all',
],
],
```

Which one fits depends on what your users do when they type a second word.

- Use `any` when they are exploring and don't know the wording of your content, as in site search or
documentation: every extra word is a hint about relevance, not a requirement.
- Use `all` when they know what they are after and are narrowing down a list, as in product catalogues
or ticket systems: an extra word means "and also this", and results ignoring it feel like noise.

## Stop words

Ignoring very common words of your content languages reduces the index size and keeps them from
Expand Down
7 changes: 7 additions & 0 deletions src/Loupe/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Index extends BaseIndex
protected array $defaults = [
'fields' => ['title'],
'languages' => 'auto',
'matching_strategy' => null,
'max_query_tokens' => null,
'max_total_hits' => null,
'min_token_length_for_prefix_search' => 2,
Expand Down Expand Up @@ -93,6 +94,7 @@ public function lookup($query)
{
$parameters = SearchParameters::create()
->withQuery($query)
->withMatchingStrategy($this->matchingStrategy())
->withHitsPerPage($this->hitsPerPage())
->withShowRankingScore(true)
->withRankingScoreThreshold($this->config['ranking_score_threshold'])
Expand Down Expand Up @@ -172,6 +174,11 @@ protected function typoTolerance(): TypoTolerance
return $tolerance;
}

public function matchingStrategy(): string
{
return $this->config['matching_strategy'] ?: SearchParameters::create()->getMatchingStrategy();
}

public function hitsPerPage(): int
{
$max = min($this->config['max_total_hits'] ?? SearchParameters::MAX_LIMIT, SearchParameters::MAX_LIMIT);
Expand Down
16 changes: 16 additions & 0 deletions tests/Feature/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,22 @@

});

it('defaults to the matching strategy of loupe', function () {
/** @var Index */
$index = $this->app->makeWith(Index::class, ['name' => 'default']);

expect($index->matchingStrategy())->toEqual(SearchParameters::create()->getMatchingStrategy());
});

it('uses a configured matching strategy', function () {
$config = ['matching_strategy' => 'all'];

/** @var Index */
$index = $this->app->makeWith(Index::class, ['name' => 'default', 'config' => $config]);

expect($index->matchingStrategy())->toEqual('all');
});

it('fetches as many results as loupe allows', function () {
/** @var Index */
$index = $this->app->makeWith(Index::class, ['name' => 'default']);
Expand Down
27 changes: 27 additions & 0 deletions tests/Feature/SearchOptionsTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Support\Facades\File;
use Loupe\Loupe\Exception\InvalidSearchParametersException;
use Statamic\Facades\Collection;
use Statamic\Facades\Entry;
use Statamic\Facades\Search;
Expand Down Expand Up @@ -148,6 +149,32 @@ function scoresFor(string $query): array
expect(titlesFor('umbrella season'))->toHaveCount(2);
});

it('matches any query word by default', function () {
useIndexOptions([]);

indexEntry('a', ['title' => 'Umbrella Season']);
indexEntry('b', ['title' => 'Umbrella']);

expect(titlesFor('umbrella season'))->toContain('Umbrella Season', 'Umbrella');
});

it('requires all query words with the all matching strategy', function () {
useIndexOptions(['matching_strategy' => 'all']);

indexEntry('a', ['title' => 'Umbrella Season']);
indexEntry('b', ['title' => 'Umbrella']);

expect(titlesFor('umbrella season'))->toEqual(['Umbrella Season']);
});

it('rejects an unknown matching strategy', function () {
useIndexOptions(['matching_strategy' => 'some']);

indexEntry('a', ['title' => 'Umbrella']);

titlesFor('umbrella');
})->throws(InvalidSearchParametersException::class);

it('limits the number of query tokens', function () {
useIndexOptions(['max_query_tokens' => 1]);

Expand Down
Loading