From eb8fa300d0365e75ec7ad205a816d34ceb727a2f Mon Sep 17 00:00:00 2001 From: Spamer Date: Tue, 23 Dec 2025 01:34:09 +0100 Subject: [PATCH 1/2] added matchall query --- src/Query/MatchAll.php | 44 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/Query/MatchAll.php diff --git a/src/Query/MatchAll.php b/src/Query/MatchAll.php new file mode 100644 index 0000000..bf52f03 --- /dev/null +++ b/src/Query/MatchAll.php @@ -0,0 +1,44 @@ + + */ + public function toArray(): array + { + if ($this->boost !== 1.0) { + return [ + 'match_all' => [ + 'boost' => $this->boost, + ], + ]; + } + + return [ + 'match_all' => new \stdClass(), + ]; + } + +} From 4d6ca9d292f41270e1942e9be85a5dd4854b8d83 Mon Sep 17 00:00:00 2001 From: Spamer Date: Tue, 23 Dec 2025 01:38:40 +0100 Subject: [PATCH 2/2] added matchall query --- doc/02-query-objects.md | 14 ++ src/Query/MatchAll.php | 4 +- .../ElasticQuery/Query/MatchAll.phpt | 151 ++++++++++++++++++ 3 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 tests/SpameriTests/ElasticQuery/Query/MatchAll.phpt diff --git a/doc/02-query-objects.md b/doc/02-query-objects.md index df9fae5..d457672 100644 --- a/doc/02-query-objects.md +++ b/doc/02-query-objects.md @@ -170,6 +170,20 @@ new \Spameri\ElasticQuery\Query\WildCard( ## Specialized Queries +##### MatchAll Query +Matches all documents in the index. Useful as a base query for filtering or function scoring. +- Class: `\Spameri\ElasticQuery\Query\MatchAll` +- [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html) +- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/MatchAll.php) + +```php +// Match all documents +new \Spameri\ElasticQuery\Query\MatchAll(); + +// Match all with custom boost +new \Spameri\ElasticQuery\Query\MatchAll(boost: 1.5); +``` + ##### Nested Query Query nested objects with their own scope. - Class: `\Spameri\ElasticQuery\Query\Nested` diff --git a/src/Query/MatchAll.php b/src/Query/MatchAll.php index bf52f03..9734c38 100644 --- a/src/Query/MatchAll.php +++ b/src/Query/MatchAll.php @@ -1,4 +1,6 @@ -toArray(); + + \Tester\Assert::true(isset($array['match_all'])); + \Tester\Assert::type(\stdClass::class, $array['match_all']); + } + + + public function testToArrayWithBoost(): void + { + $matchAll = new \Spameri\ElasticQuery\Query\MatchAll(boost: 1.5); + + $array = $matchAll->toArray(); + + \Tester\Assert::true(isset($array['match_all'])); + \Tester\Assert::same(1.5, $array['match_all']['boost']); + } + + + public function testKey(): void + { + $matchAll = new \Spameri\ElasticQuery\Query\MatchAll(); + + \Tester\Assert::same('match_all', $matchAll->key()); + } + + + public function testCreate(): void + { + $matchAll = new \Spameri\ElasticQuery\Query\MatchAll(); + + $document = new \Spameri\ElasticQuery\Document( + self::INDEX, + new \Spameri\ElasticQuery\Document\Body\Plain( + ( + new \Spameri\ElasticQuery\ElasticQuery( + new \Spameri\ElasticQuery\Query\QueryCollection( + null, + new \Spameri\ElasticQuery\Query\MustCollection( + $matchAll, + ), + ), + ) + )->toArray(), + ), + ); + + $ch = \curl_init(); + \curl_setopt($ch, \CURLOPT_URL, \ELASTICSEARCH_HOST . '/' . $document->index . '/_search'); + \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1); + \curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'GET'); + \curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']); + \curl_setopt( + $ch, + \CURLOPT_POSTFIELDS, + \json_encode($document->toArray()['body']), + ); + + \Tester\Assert::noError(static function () use ($ch): void { + $response = \curl_exec($ch); + $resultMapper = new \Spameri\ElasticQuery\Response\ResultMapper(); + /** @var \Spameri\ElasticQuery\Response\ResultSearch $result */ + $result = $resultMapper->map(\json_decode($response, true)); + \Tester\Assert::type('int', $result->stats()->total()); + }); + } + + + public function testCreateWithBoost(): void + { + $matchAll = new \Spameri\ElasticQuery\Query\MatchAll(boost: 2.0); + + $document = new \Spameri\ElasticQuery\Document( + self::INDEX, + new \Spameri\ElasticQuery\Document\Body\Plain( + ( + new \Spameri\ElasticQuery\ElasticQuery( + new \Spameri\ElasticQuery\Query\QueryCollection( + null, + new \Spameri\ElasticQuery\Query\MustCollection( + $matchAll, + ), + ), + ) + )->toArray(), + ), + ); + + $ch = \curl_init(); + \curl_setopt($ch, \CURLOPT_URL, \ELASTICSEARCH_HOST . '/' . $document->index . '/_search'); + \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1); + \curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'GET'); + \curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']); + \curl_setopt( + $ch, + \CURLOPT_POSTFIELDS, + \json_encode($document->toArray()['body']), + ); + + \Tester\Assert::noError(static function () use ($ch): void { + $response = \curl_exec($ch); + $resultMapper = new \Spameri\ElasticQuery\Response\ResultMapper(); + /** @var \Spameri\ElasticQuery\Response\ResultSearch $result */ + $result = $resultMapper->map(\json_decode($response, true)); + \Tester\Assert::type('int', $result->stats()->total()); + }); + } + + + public function tearDown(): void + { + $ch = \curl_init(); + \curl_setopt($ch, \CURLOPT_URL, \ELASTICSEARCH_HOST . '/' . self::INDEX); + \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1); + \curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'DELETE'); + \curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']); + + \curl_exec($ch); + } + +} + +(new MatchAll())->run();