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 diff --git a/README.md b/README.md index 45f6e64..c5531b7 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,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 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); 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]);