Skip to content
Closed
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ $nestedQuery->innerHits(
->add($existsQuery, 'must_not');
```

#### `RawQuery`

This query can be used as a catch-all for any raw query that you want to add to the builder. It accepts an array which will be added to the query body as-is.

```php
\Spatie\ElasticsearchQueryBuilder\Queries\RawQuery::create([
'foo' => 'bar',
])
```

#### `collapse`

The `collapse` feature allows grouping search results by a specific field while retrieving top documents from each group using `inner_hits`. This is useful for avoiding duplicate entities in search results while still accessing grouped data.
Expand Down
23 changes: 23 additions & 0 deletions src/Queries/RawQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Spatie\ElasticsearchQueryBuilder\Queries;

class RawQuery implements Query
{
protected array $query;

public function __construct(array $query)
{
$this->query = $query;
}

public static function create(array $query): static
{
return new self($query);
}

public function toArray(): array
{
return $this->query;
}
}
31 changes: 31 additions & 0 deletions tests/Queries/RawQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Queries;

use PHPUnit\Framework\TestCase;
use Spatie\ElasticsearchQueryBuilder\Queries\RawQuery;

final class RawQueryTest extends TestCase
{
public function testCreateReturnsNewInstance(): void
{
$query = RawQuery::create(['match' => ['field' => 'value']]);

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

public function testRawQuery(): void
{
$query = [
'match' => [
'field' => 'value',
],
];

$rawQuery = new RawQuery($query);

$this->assertSame($query, $rawQuery->toArray());
}
}