Skip to content

Commit

Permalink
Merge pull request #57 from floristenhove/feature/percolate-query
Browse files Browse the repository at this point in the history
Add percolate query
  • Loading branch information
Nielsvanpach authored Nov 7, 2024
2 parents 9a386b2 + d164f79 commit 267ee05
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ The following query types are available:
\Spatie\ElasticsearchQueryBuilder\Queries\WildcardQuery::create('user.id', '*doe');
```

#### `PercolateQuery`

[https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-percolate-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-percolate-query.html)

```php
\Spatie\ElasticsearchQueryBuilder\Queries\PercolateQuery::create('query', ['title' => 'foo', 'body' => 'bar']);
```

#### `BoolQuery`

[https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html)
Expand Down
66 changes: 66 additions & 0 deletions src/Queries/PercolateQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Spatie\ElasticsearchQueryBuilder\Queries;

use Exception;

class PercolateQuery implements Query
{
protected string $field;
protected array $document;
protected string $index;
protected string $id;

public static function create(string $field): static
{
return new self($field);
}

public function __construct(string $field)
{
$this->field = $field;
}

public function setInlineDocument(array $document): static
{
$this->document = $document;

return $this;
}

public function setDocument(string $index, string|int $id): static
{
$this->index = $index;
$this->id = (string) $id;

return $this;
}

public function toArray(): array
{
if(isset($this->document) && isset($this->index)) {

Check failure on line 41 in src/Queries/PercolateQuery.php

View workflow job for this annotation

GitHub Actions / psalm

RedundantPropertyInitializationCheck

src/Queries/PercolateQuery.php:41:12: RedundantPropertyInitializationCheck: Property $this->document with type array<array-key, mixed> should already be set in the constructor (see https://psalm.dev/261)

Check failure on line 41 in src/Queries/PercolateQuery.php

View workflow job for this annotation

GitHub Actions / psalm

RedundantCondition

src/Queries/PercolateQuery.php:41:12: RedundantCondition: Type array<array-key, mixed> for $this->document is always isset (see https://psalm.dev/122)
throw new Exception('You can only set an inline document or a document, not both.');
}

if (!isset($this->document) && !isset($this->index)) {

Check failure on line 45 in src/Queries/PercolateQuery.php

View workflow job for this annotation

GitHub Actions / psalm

RedundantPropertyInitializationCheck

src/Queries/PercolateQuery.php:45:13: RedundantPropertyInitializationCheck: Property $this->document with type array<array-key, mixed> should already be set in the constructor (see https://psalm.dev/261)
throw new Exception('You must set an inline document or a document.');
}

$query = [
'percolate' => [
'field' => $this->field,
],
];

if (isset($this->document)) {
$query['percolate']['document'] = $this->document;

return $query;
}

$query['percolate']['index'] = $this->index;
$query['percolate']['id'] = $this->id;

return $query;
}
}
63 changes: 63 additions & 0 deletions tests/Queries/PercolateQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Spatie\ElasticsearchQueryBuilder\Tests\Queries;

use Exception;
use PHPUnit\Framework\TestCase;
use Spatie\ElasticsearchQueryBuilder\Queries\PercolateQuery;

class PercolateQueryTest extends TestCase
{
public function testCreateReturnsNewInstance(): void
{
$query = PercolateQuery::create('test_field');

self::assertInstanceOf(PercolateQuery::class, $query);
}

public function testInlineDocumentToArrayBuildsCorrectQuery(): void
{
$query = (new PercolateQuery('test_field'))
->setInlineDocument(['foo' => 'bar', 'baz' => 'qux']);

self::assertEquals([
'percolate' => [
'field' => 'test_field',
'document' => ['foo' => 'bar', 'baz' => 'qux'],
],
], $query->toArray());
}

public function testDocumentToArrayBuildsCorrectQuery(): void
{
$query = (new PercolateQuery('test_field'))
->setDocument('test_index', 123);

self::assertEquals([
'percolate' => [
'field' => 'test_field',
'index' => 'test_index',
'id' => '123',
],
], $query->toArray());
}

public function testToArrayThrowsExceptionWhenBothInlineDocumentAndDocumentAreSet(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('You can only set an inline document or a document, not both.');

(new PercolateQuery('test_field'))
->setInlineDocument(['foo' => 'bar', 'baz' => 'qux'])
->setDocument('test_index', 123)
->toArray();
}

public function testToArrayThrowsExceptionWhenNeitherInlineDocumentNorDocumentAreSet(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('You must set an inline document or a document.');

(new PercolateQuery('test_field'))->toArray();
}
}

0 comments on commit 267ee05

Please sign in to comment.