From c75fbce2dea66f39e8aa88911e7411fd65eacb84 Mon Sep 17 00:00:00 2001 From: Spamer Date: Mon, 30 Mar 2026 10:07:51 +0200 Subject: [PATCH 1/3] fix(query): allow string type for minimumShouldMatch parameter Elasticsearch accepts string values like "75%" or "2<90%" for minimum_should_match. Changed type from int|null to int|string|null in ElasticMatch and MultiMatch. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/Query/ElasticMatch.php | 2 +- src/Query/MultiMatch.php | 2 +- .../ElasticQuery/Query/ElasticMatch.phpt | 48 +++++++++++++++++ .../ElasticQuery/Query/MultiMatch.phpt | 51 +++++++++++++++++++ 4 files changed, 101 insertions(+), 2 deletions(-) diff --git a/src/Query/ElasticMatch.php b/src/Query/ElasticMatch.php index 71ec3bd..185d47c 100644 --- a/src/Query/ElasticMatch.php +++ b/src/Query/ElasticMatch.php @@ -16,7 +16,7 @@ public function __construct( private bool|int|string|null $query, private float $boost = 1.0, private \Spameri\ElasticQuery\Query\Match\Fuzziness|null $fuzziness = null, - private int|null $minimumShouldMatch = null, + private int|string|null $minimumShouldMatch = null, private string $operator = \Spameri\ElasticQuery\Query\Match\Operator::OR, private string|null $analyzer = null, ) diff --git a/src/Query/MultiMatch.php b/src/Query/MultiMatch.php index 77df568..882a7e0 100644 --- a/src/Query/MultiMatch.php +++ b/src/Query/MultiMatch.php @@ -17,7 +17,7 @@ public function __construct( private float $boost = 1.0, private \Spameri\ElasticQuery\Query\Match\Fuzziness|null $fuzziness = null, private string $type = \Spameri\ElasticQuery\Query\Match\MultiMatchType::BEST_FIELDS, - private int|null $minimumShouldMatch = null, + private int|string|null $minimumShouldMatch = null, private string $operator = \Spameri\ElasticQuery\Query\Match\Operator::OR, private string|null $analyzer = null, ) diff --git a/tests/SpameriTests/ElasticQuery/Query/ElasticMatch.phpt b/tests/SpameriTests/ElasticQuery/Query/ElasticMatch.phpt index 39230cf..69198fe 100644 --- a/tests/SpameriTests/ElasticQuery/Query/ElasticMatch.phpt +++ b/tests/SpameriTests/ElasticQuery/Query/ElasticMatch.phpt @@ -83,6 +83,54 @@ class ElasticMatch extends \Tester\TestCase } + public function testMinimumShouldMatchString() : void + { + $match = new \Spameri\ElasticQuery\Query\ElasticMatch( + 'name', + 'Avengers Endgame', + 1.0, + null, + '75%', + ); + + $array = $match->toArray(); + + \Tester\Assert::same('75%', $array['match']['name']['minimum_should_match']); + } + + + public function testMinimumShouldMatchCombinationString() : void + { + $match = new \Spameri\ElasticQuery\Query\ElasticMatch( + 'name', + 'Avengers Endgame Infinity War', + 1.0, + null, + '2<90%', + ); + + $array = $match->toArray(); + + \Tester\Assert::same('2<90%', $array['match']['name']['minimum_should_match']); + } + + + public function testMinimumShouldMatchInt() : void + { + $match = new \Spameri\ElasticQuery\Query\ElasticMatch( + 'name', + 'Avengers Endgame', + 1.0, + null, + 2, + ); + + $array = $match->toArray(); + + \Tester\Assert::same(2, $array['match']['name']['minimum_should_match']); + } + + public function tearDown() : void { $ch = \curl_init(); diff --git a/tests/SpameriTests/ElasticQuery/Query/MultiMatch.phpt b/tests/SpameriTests/ElasticQuery/Query/MultiMatch.phpt index 72e09e5..91f2ef0 100644 --- a/tests/SpameriTests/ElasticQuery/Query/MultiMatch.phpt +++ b/tests/SpameriTests/ElasticQuery/Query/MultiMatch.phpt @@ -154,6 +154,57 @@ class MultiMatch extends \Tester\TestCase } + public function testMinimumShouldMatchString(): void + { + $multiMatch = new \Spameri\ElasticQuery\Query\MultiMatch( + ['title', 'description'], + 'search term', + 1.0, + null, + \Spameri\ElasticQuery\Query\Match\MultiMatchType::BEST_FIELDS, + '75%', + ); + + $array = $multiMatch->toArray(); + + \Tester\Assert::same('75%', $array['multi_match']['minimum_should_match']); + } + + + public function testMinimumShouldMatchCombinationString(): void + { + $multiMatch = new \Spameri\ElasticQuery\Query\MultiMatch( + ['title', 'description'], + 'search term query', + 1.0, + null, + \Spameri\ElasticQuery\Query\Match\MultiMatchType::BEST_FIELDS, + '2<90%', + ); + + $array = $multiMatch->toArray(); + + \Tester\Assert::same('2<90%', $array['multi_match']['minimum_should_match']); + } + + + public function testMinimumShouldMatchInt(): void + { + $multiMatch = new \Spameri\ElasticQuery\Query\MultiMatch( + ['title', 'description'], + 'search term', + 1.0, + null, + \Spameri\ElasticQuery\Query\Match\MultiMatchType::BEST_FIELDS, + 2, + ); + + $array = $multiMatch->toArray(); + + \Tester\Assert::same(2, $array['multi_match']['minimum_should_match']); + } + + public function testCreate(): void { $multiMatch = new \Spameri\ElasticQuery\Query\MultiMatch( From 4401ccfdd6bd81bd15a0c8f7040efb92151fd525 Mon Sep 17 00:00:00 2001 From: Spamer Date: Mon, 30 Mar 2026 10:39:31 +0200 Subject: [PATCH 2/3] style: fix fully qualified count() call and CRLF line endings Co-Authored-By: Claude Opus 4.6 (1M context) --- src/Query/Nested.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Query/Nested.php b/src/Query/Nested.php index 4be9ed6..532f81a 100644 --- a/src/Query/Nested.php +++ b/src/Query/Nested.php @@ -34,7 +34,7 @@ public function toArray(): array { $queryArray = $this->query->toArray(); - if (count($queryArray) === 0) { + if (\count($queryArray) === 0) { $queryArray = [ 'bool' => [], ]; From be953063c489380246448d26bdc66736b183faf9 Mon Sep 17 00:00:00 2001 From: Spamer Date: Mon, 30 Mar 2026 10:48:17 +0200 Subject: [PATCH 3/3] fix(tests): update tests for new constructor and Nested toArray changes - Add missing $postFilter argument in ElasticQuery and AggregationCollection tests - Update Nested test assertions to match new query array wrapping Co-Authored-By: Claude Opus 4.6 (1M context) --- .../Aggregation/AggregationCollection.phpt | 1 + tests/SpameriTests/ElasticQuery/ElasticQuery.phpt | 1 + tests/SpameriTests/ElasticQuery/Query/Nested.phpt | 10 +++++----- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/SpameriTests/ElasticQuery/Aggregation/AggregationCollection.phpt b/tests/SpameriTests/ElasticQuery/Aggregation/AggregationCollection.phpt index f998dda..bf7d0da 100644 --- a/tests/SpameriTests/ElasticQuery/Aggregation/AggregationCollection.phpt +++ b/tests/SpameriTests/ElasticQuery/Aggregation/AggregationCollection.phpt @@ -149,6 +149,7 @@ class AggregationCollection extends \Tester\TestCase null, null, null, + null, $collection, ); diff --git a/tests/SpameriTests/ElasticQuery/ElasticQuery.phpt b/tests/SpameriTests/ElasticQuery/ElasticQuery.phpt index ea2b364..aac3eb8 100644 --- a/tests/SpameriTests/ElasticQuery/ElasticQuery.phpt +++ b/tests/SpameriTests/ElasticQuery/ElasticQuery.phpt @@ -79,6 +79,7 @@ class ElasticQuery extends \Tester\TestCase $term ) ), + NULL, new \Spameri\ElasticQuery\Options\SortCollection( new \Spameri\ElasticQuery\Options\Sort( 'year', diff --git a/tests/SpameriTests/ElasticQuery/Query/Nested.phpt b/tests/SpameriTests/ElasticQuery/Query/Nested.phpt index ff0debf..ed9d044 100644 --- a/tests/SpameriTests/ElasticQuery/Query/Nested.phpt +++ b/tests/SpameriTests/ElasticQuery/Query/Nested.phpt @@ -31,7 +31,7 @@ class Nested extends \Tester\TestCase \Tester\Assert::true(isset($array['nested'])); \Tester\Assert::same('comments', $array['nested']['path']); - \Tester\Assert::true(isset($array['nested']['query']['bool'])); + \Tester\Assert::true(isset($array['nested']['query'][0]['bool'])); } @@ -47,8 +47,8 @@ class Nested extends \Tester\TestCase $array = $nested->toArray(); \Tester\Assert::same('comments', $array['nested']['path']); - \Tester\Assert::true(isset($array['nested']['query']['bool']['bool']['must'])); - \Tester\Assert::count(1, $array['nested']['query']['bool']['bool']['must']); + \Tester\Assert::true(isset($array['nested']['query'][0]['bool']['must'])); + \Tester\Assert::count(1, $array['nested']['query'][0]['bool']['must']); } @@ -74,8 +74,8 @@ class Nested extends \Tester\TestCase $array = $nested->toArray(); - \Tester\Assert::true(isset($array['nested']['query']['bool']['bool']['must'])); - \Tester\Assert::true(isset($array['nested']['query']['bool']['bool']['should'])); + \Tester\Assert::true(isset($array['nested']['query'][0]['bool']['must'])); + \Tester\Assert::true(isset($array['nested']['query'][0]['bool']['should'])); }