diff --git a/README.md b/README.md index a99be15..0473fdb 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/Queries/PercolateQuery.php b/src/Queries/PercolateQuery.php new file mode 100644 index 0000000..43b2921 --- /dev/null +++ b/src/Queries/PercolateQuery.php @@ -0,0 +1,66 @@ +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)) { + throw new Exception('You can only set an inline document or a document, not both.'); + } + + if (!isset($this->document) && !isset($this->index)) { + 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; + } +} diff --git a/tests/Queries/PercolateQueryTest.php b/tests/Queries/PercolateQueryTest.php new file mode 100644 index 0000000..06da5b6 --- /dev/null +++ b/tests/Queries/PercolateQueryTest.php @@ -0,0 +1,63 @@ +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(); + } +}