Skip to content

Commit 4d6ca9d

Browse files
committed
added matchall query
1 parent eb8fa30 commit 4d6ca9d

File tree

3 files changed

+168
-1
lines changed

3 files changed

+168
-1
lines changed

doc/02-query-objects.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,20 @@ new \Spameri\ElasticQuery\Query\WildCard(
170170

171171
## Specialized Queries
172172

173+
##### MatchAll Query
174+
Matches all documents in the index. Useful as a base query for filtering or function scoring.
175+
- Class: `\Spameri\ElasticQuery\Query\MatchAll`
176+
- [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html)
177+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/MatchAll.php)
178+
179+
```php
180+
// Match all documents
181+
new \Spameri\ElasticQuery\Query\MatchAll();
182+
183+
// Match all with custom boost
184+
new \Spameri\ElasticQuery\Query\MatchAll(boost: 1.5);
185+
```
186+
173187
##### Nested Query
174188
Query nested objects with their own scope.
175189
- Class: `\Spameri\ElasticQuery\Query\Nested`

src/Query/MatchAll.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types = 1);
1+
<?php
2+
3+
declare(strict_types = 1);
24

35
namespace Spameri\ElasticQuery\Query;
46

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SpameriTests\ElasticQuery\Query;
4+
5+
require_once __DIR__ . '/../../bootstrap.php';
6+
7+
8+
class MatchAll extends \Tester\TestCase
9+
{
10+
11+
private const INDEX = 'spameri_test_video_match_all';
12+
13+
14+
public function setUp(): void
15+
{
16+
$ch = \curl_init();
17+
\curl_setopt($ch, \CURLOPT_URL, \ELASTICSEARCH_HOST . '/' . self::INDEX);
18+
\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
19+
\curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'PUT');
20+
\curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
21+
22+
\curl_exec($ch);
23+
}
24+
25+
26+
public function testToArrayDefault(): void
27+
{
28+
$matchAll = new \Spameri\ElasticQuery\Query\MatchAll();
29+
30+
$array = $matchAll->toArray();
31+
32+
\Tester\Assert::true(isset($array['match_all']));
33+
\Tester\Assert::type(\stdClass::class, $array['match_all']);
34+
}
35+
36+
37+
public function testToArrayWithBoost(): void
38+
{
39+
$matchAll = new \Spameri\ElasticQuery\Query\MatchAll(boost: 1.5);
40+
41+
$array = $matchAll->toArray();
42+
43+
\Tester\Assert::true(isset($array['match_all']));
44+
\Tester\Assert::same(1.5, $array['match_all']['boost']);
45+
}
46+
47+
48+
public function testKey(): void
49+
{
50+
$matchAll = new \Spameri\ElasticQuery\Query\MatchAll();
51+
52+
\Tester\Assert::same('match_all', $matchAll->key());
53+
}
54+
55+
56+
public function testCreate(): void
57+
{
58+
$matchAll = new \Spameri\ElasticQuery\Query\MatchAll();
59+
60+
$document = new \Spameri\ElasticQuery\Document(
61+
self::INDEX,
62+
new \Spameri\ElasticQuery\Document\Body\Plain(
63+
(
64+
new \Spameri\ElasticQuery\ElasticQuery(
65+
new \Spameri\ElasticQuery\Query\QueryCollection(
66+
null,
67+
new \Spameri\ElasticQuery\Query\MustCollection(
68+
$matchAll,
69+
),
70+
),
71+
)
72+
)->toArray(),
73+
),
74+
);
75+
76+
$ch = \curl_init();
77+
\curl_setopt($ch, \CURLOPT_URL, \ELASTICSEARCH_HOST . '/' . $document->index . '/_search');
78+
\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
79+
\curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'GET');
80+
\curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
81+
\curl_setopt(
82+
$ch,
83+
\CURLOPT_POSTFIELDS,
84+
\json_encode($document->toArray()['body']),
85+
);
86+
87+
\Tester\Assert::noError(static function () use ($ch): void {
88+
$response = \curl_exec($ch);
89+
$resultMapper = new \Spameri\ElasticQuery\Response\ResultMapper();
90+
/** @var \Spameri\ElasticQuery\Response\ResultSearch $result */
91+
$result = $resultMapper->map(\json_decode($response, true));
92+
\Tester\Assert::type('int', $result->stats()->total());
93+
});
94+
}
95+
96+
97+
public function testCreateWithBoost(): void
98+
{
99+
$matchAll = new \Spameri\ElasticQuery\Query\MatchAll(boost: 2.0);
100+
101+
$document = new \Spameri\ElasticQuery\Document(
102+
self::INDEX,
103+
new \Spameri\ElasticQuery\Document\Body\Plain(
104+
(
105+
new \Spameri\ElasticQuery\ElasticQuery(
106+
new \Spameri\ElasticQuery\Query\QueryCollection(
107+
null,
108+
new \Spameri\ElasticQuery\Query\MustCollection(
109+
$matchAll,
110+
),
111+
),
112+
)
113+
)->toArray(),
114+
),
115+
);
116+
117+
$ch = \curl_init();
118+
\curl_setopt($ch, \CURLOPT_URL, \ELASTICSEARCH_HOST . '/' . $document->index . '/_search');
119+
\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
120+
\curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'GET');
121+
\curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
122+
\curl_setopt(
123+
$ch,
124+
\CURLOPT_POSTFIELDS,
125+
\json_encode($document->toArray()['body']),
126+
);
127+
128+
\Tester\Assert::noError(static function () use ($ch): void {
129+
$response = \curl_exec($ch);
130+
$resultMapper = new \Spameri\ElasticQuery\Response\ResultMapper();
131+
/** @var \Spameri\ElasticQuery\Response\ResultSearch $result */
132+
$result = $resultMapper->map(\json_decode($response, true));
133+
\Tester\Assert::type('int', $result->stats()->total());
134+
});
135+
}
136+
137+
138+
public function tearDown(): void
139+
{
140+
$ch = \curl_init();
141+
\curl_setopt($ch, \CURLOPT_URL, \ELASTICSEARCH_HOST . '/' . self::INDEX);
142+
\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
143+
\curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'DELETE');
144+
\curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
145+
146+
\curl_exec($ch);
147+
}
148+
149+
}
150+
151+
(new MatchAll())->run();

0 commit comments

Comments
 (0)