From 798bac070a817a6384f256ea4781018e223e6a8f Mon Sep 17 00:00:00 2001 From: Philipp Daun Date: Thu, 6 Aug 2026 14:44:27 +0200 Subject: [PATCH 1/5] Pass along matching strategy --- src/Loupe/Index.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Loupe/Index.php b/src/Loupe/Index.php index 4d291f5..d1bdd35 100644 --- a/src/Loupe/Index.php +++ b/src/Loupe/Index.php @@ -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, @@ -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']) @@ -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); From 946d802caf6f0f03afba0b8db7fa88cab1eb44fe Mon Sep 17 00:00:00 2001 From: Philipp Daun Date: Thu, 6 Aug 2026 14:44:40 +0200 Subject: [PATCH 2/5] Add tests for matching strategy --- tests/Feature/ConfigurationTest.php | 16 ++++++++++++++++ tests/Feature/SearchOptionsTest.php | 27 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/tests/Feature/ConfigurationTest.php b/tests/Feature/ConfigurationTest.php index 9e846e8..57fef5c 100644 --- a/tests/Feature/ConfigurationTest.php +++ b/tests/Feature/ConfigurationTest.php @@ -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']); diff --git a/tests/Feature/SearchOptionsTest.php b/tests/Feature/SearchOptionsTest.php index 4fbd459..3dddada 100644 --- a/tests/Feature/SearchOptionsTest.php +++ b/tests/Feature/SearchOptionsTest.php @@ -1,6 +1,7 @@ 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]); From 206394ded1fa2735f15540f5d82811a3647cec7f Mon Sep 17 00:00:00 2001 From: Philipp Daun Date: Thu, 6 Aug 2026 14:45:16 +0200 Subject: [PATCH 3/5] Add docs on matching strategy --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/README.md b/README.md index 45f6e64..b9e76f1 100644 --- a/README.md +++ b/README.md @@ -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, @@ -137,6 +140,43 @@ an empty array to let Loupe detect the language of every document by itself: ], ``` +## Matching strategy + +The matching strategy decides how a query with multiple words is treated: whether a document has +to contain every word (`all`) or whether any of them is enough (`any`). + +Loupe defaults to `any`. Documents matching more words rank higher, so a query like +`winter tyre pressure` still puts documents containing all three words on top, followed by +documents about winter tyres, followed by documents merely mentioning pressure. Nothing is +discarded, the ranking does the work. If the long tail is too long, raise +`ranking_score_threshold` to cut off weak matches instead of switching strategies. + +With `all`, every additional word narrows the result set. 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 are doing when they type a second word: + +- Use `any` when they are exploring and don't know the wording of your content. Site search, + documentation, editorial archives. Every extra word is a hint about relevance, not a + requirement, and a result they didn't ask for precisely is often the one they wanted. +- Use `all` when they know what they are after and are narrowing down a list. Product + catalogues, ticket systems, large data tables. Here an extra word means "and also this", + and results that ignore it feel like noise. + +This is a decision per index, not per query. Ask yourself which end of the pipeline should do +the heavy lifting: the ranking, which keeps everything and sorts it by relevance, or the user, +who keeps typing until the list is short enough. + ## Stop words Ignoring very common words of your content languages reduces the index size and keeps them from From 27a7664fdbcf56e7eb279b2aaf2f91bf6a3ab70e Mon Sep 17 00:00:00 2001 From: Philipp Daun Date: Thu, 6 Aug 2026 15:02:01 +0200 Subject: [PATCH 4/5] Reword readme --- README.md | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index b9e76f1..c5531b7 100644 --- a/README.md +++ b/README.md @@ -142,17 +142,14 @@ an empty array to let Loupe detect the language of every document by itself: ## Matching strategy -The matching strategy decides how a query with multiple words is treated: whether a document has -to contain every word (`all`) or whether any of them is enough (`any`). - -Loupe defaults to `any`. Documents matching more words rank higher, so a query like -`winter tyre pressure` still puts documents containing all three words on top, followed by +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, the ranking does the work. If the long tail is too long, raise -`ranking_score_threshold` to cut off weak matches instead of switching strategies. +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. A query matching no document returns -nothing, no matter how close it was. +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' => [ @@ -164,18 +161,12 @@ nothing, no matter how close it was. ], ``` -Which one fits depends on what your users are doing when they type a second word: - -- Use `any` when they are exploring and don't know the wording of your content. Site search, - documentation, editorial archives. Every extra word is a hint about relevance, not a - requirement, and a result they didn't ask for precisely is often the one they wanted. -- Use `all` when they know what they are after and are narrowing down a list. Product - catalogues, ticket systems, large data tables. Here an extra word means "and also this", - and results that ignore it feel like noise. +Which one fits depends on what your users do when they type a second word. -This is a decision per index, not per query. Ask yourself which end of the pipeline should do -the heavy lifting: the ranking, which keeps everything and sorts it by relevance, or the user, -who keeps typing until the list is short enough. +- 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 From 334376b64dcea8076b871e8280718bbb2f563726 Mon Sep 17 00:00:00 2001 From: Philipp Daun Date: Thu, 6 Aug 2026 20:58:31 +0200 Subject: [PATCH 5/5] Update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fa4b2a..6646045 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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