Skip to content
Merged

V2 #13

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions doc/02-query-objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
46 changes: 46 additions & 0 deletions src/Query/MatchAll.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types = 1);

namespace Spameri\ElasticQuery\Query;

/**
* Match all documents query.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html
*/
class MatchAll implements \Spameri\ElasticQuery\Query\LeafQueryInterface
{

public function __construct(
private float $boost = 1.0,
)
{
}


public function key(): string
{
return 'match_all';
}


/**
* @return array<string, mixed>
*/
public function toArray(): array
{
if ($this->boost !== 1.0) {
return [
'match_all' => [
'boost' => $this->boost,
],
];
}

return [
'match_all' => new \stdClass(),
];
}

}
151 changes: 151 additions & 0 deletions tests/SpameriTests/ElasticQuery/Query/MatchAll.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php declare(strict_types = 1);

namespace SpameriTests\ElasticQuery\Query;

require_once __DIR__ . '/../../bootstrap.php';


class MatchAll extends \Tester\TestCase
{

private const INDEX = 'spameri_test_video_match_all';


public function setUp(): void
{
$ch = \curl_init();
\curl_setopt($ch, \CURLOPT_URL, \ELASTICSEARCH_HOST . '/' . self::INDEX);
\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
\curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'PUT');
\curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']);

\curl_exec($ch);
}


public function testToArrayDefault(): void
{
$matchAll = new \Spameri\ElasticQuery\Query\MatchAll();

$array = $matchAll->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();