Skip to content
Merged
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
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Fluent, immutable, type-safe Elasticsearch query builder for PHP 8.4+. Zero prod
```
QueryInterface ─┬─ TermQuery (uses BoostableQuery)
├─ TermsQuery (uses BoostableQuery)
├─ ExistsQuery (uses BoostableQuery)
├─ MatchQuery (uses BoostableQuery, AnalyzerAwareQuery)
├─ MatchPhraseQuery (uses BoostableQuery, AnalyzerAwareQuery)
├─ BoolQuery (uses BoostableQuery)
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ use Bonu\ElasticsearchBuilder\Query\TermsQuery;
new TermsQuery('status', ['active', 'pending'])->boost(5)
```

### ExistsQuery

https://www.elastic.co/docs/reference/query-languages/query-dsl/query-dsl-exists-query

```php
use Bonu\ElasticsearchBuilder\Query\ExistsQuery;

new ExistsQuery('field')
```

### MatchQuery

https://www.elastic.co/docs/reference/query-languages/query-dsl/query-dsl-match-query
Expand Down
1 change: 1 addition & 0 deletions src/Query/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Query classes implementing `QueryInterface`. All immutable; mutation methods clo
| `MatchQuery.php` | Full-text match (OR/AND operators) | `BoostableQuery`, `AnalyzerAwareQuery` |
| `MatchPhraseQuery.php` | Phrase match with optional slop | `BoostableQuery`, `AnalyzerAwareQuery` |
| `BoolQuery.php` | must/should/mustNot/filter composition | `BoostableQuery` |
| `ExistsQuery.php` | Field existence check | `BoostableQuery` |
| `NestedQuery.php` | Query nested document paths | — |
| `NumericRangeQuery.php` | Numeric gt/gte/lt/lte | extends `RangeQuery` |
| `DatetimeRangeQuery.php` | Date gt/gte/lt/lte + format/timeZone | extends `RangeQuery` |
Expand Down
33 changes: 33 additions & 0 deletions src/Query/ExistsQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Bonu\ElasticsearchBuilder\Query;

/**
* @see https://www.elastic.co/docs/reference/query-languages/query-dsl/query-dsl-exists-query
*/
class ExistsQuery implements QueryInterface
{
use BoostableQuery;

/**
* @param string|\Stringable $field
*/
public function __construct(
protected string | \Stringable $field,
) {
}

/**
* @inheritDoc
*/
public function toArray(): array
{
return [
'exists' => $this->addBoostToQuery([
'field' => (string) $this->field,
]),
];
}
}
28 changes: 28 additions & 0 deletions tests/Integration/Query/ExistsQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Bonu\ElasticsearchBuilder\Tests\Integration\Query;

use PHPUnit\Framework\Attributes\Test;
use Bonu\ElasticsearchBuilder\QueryBuilder;
use Bonu\ElasticsearchBuilder\Query\ExistsQuery;
use Bonu\ElasticsearchBuilder\Tests\IntegrationTestCase;

/**
* @internal
*/
final class ExistsQueryTest extends IntegrationTestCase
{
#[Test]
public function itFiltersDocumentsWhereFieldExists(): void
{
$response = $this->client?->search(
new QueryBuilder(self::INDEX)
->query(new ExistsQuery('track_id'))
->build()
)->asArray();

$this->assertGreaterThan(0, $response['hits']['total']['value']);
}
}
40 changes: 40 additions & 0 deletions tests/Unit/Query/ExistsQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Bonu\ElasticsearchBuilder\Tests\Unit\Query;

use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\Depends;
use Bonu\ElasticsearchBuilder\Tests\TestCase;
use Bonu\ElasticsearchBuilder\Query\ExistsQuery;

use const PHP_FLOAT_EPSILON;

/**
* @internal
*/
final class ExistsQueryTest extends TestCase
{
#[Test]
public function itCorrectlyBuildsArray(): void
{
$array = new ExistsQuery('user')->toArray();

$this->assertSame([
'exists' => [
'field' => 'user',
'boost' => 1.0,
],
], $array);
}

#[Depends('itCorrectlyBuildsArray')]
#[Test]
public function itCorrectlySetsBoost(): void
{
$array = new ExistsQuery('user')->boost(10.0)->toArray();

$this->assertEqualsWithDelta(10.0, $array['exists']['boost'], PHP_FLOAT_EPSILON);
}
}