Skip to content

Commit e84ef0c

Browse files
Spamerczclaude
andcommitted
feat(query): add wrapper query
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent fcefba0 commit e84ef0c

3 files changed

Lines changed: 108 additions & 0 deletions

File tree

doc/02-query-objects.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,16 @@ new \Spameri\ElasticQuery\Query\Percolate(
572572
);
573573
```
574574

575+
##### Wrapper Query
576+
Embed a raw JSON query string (auto-base64-encoded) — escape hatch for unsupported syntax.
577+
- Class: `\Spameri\ElasticQuery\Query\Wrapper`
578+
- [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wrapper-query.html)
579+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/Wrapper.php)
580+
581+
```php
582+
new \Spameri\ElasticQuery\Query\Wrapper('{"term":{"user":{"value":"kimchy"}}}');
583+
```
584+
575585
##### Script Query
576586
Filter documents with a Painless boolean script.
577587
- Class: `\Spameri\ElasticQuery\Query\Script`

src/Query/Wrapper.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Spameri\ElasticQuery\Query;
6+
7+
/**
8+
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wrapper-query.html
9+
*/
10+
class Wrapper implements \Spameri\ElasticQuery\Query\LeafQueryInterface
11+
{
12+
13+
private string $encoded;
14+
15+
16+
public function __construct(string $rawJsonQuery)
17+
{
18+
$this->encoded = \base64_encode($rawJsonQuery);
19+
}
20+
21+
22+
public function key(): string
23+
{
24+
return 'wrapper_' . \substr($this->encoded, 0, 12);
25+
}
26+
27+
28+
/**
29+
* @return array<string, array<string, string>>
30+
*/
31+
public function toArray(): array
32+
{
33+
return [
34+
'wrapper' => [
35+
'query' => $this->encoded,
36+
],
37+
];
38+
}
39+
40+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SpameriTests\ElasticQuery\Query;
4+
5+
require_once __DIR__ . '/../../bootstrap.php';
6+
7+
8+
class Wrapper extends \Tester\TestCase
9+
{
10+
11+
private const INDEX = 'spameri_test_query_wrapper';
12+
13+
14+
public function setUp(): void
15+
{
16+
$ch = \curl_init();
17+
\curl_setopt($ch, \CURLOPT_URL, \ELASTICSEARCH_HOST . '/' . self::INDEX);
18+
\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
19+
\curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'PUT');
20+
\curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
21+
22+
\curl_exec($ch);
23+
}
24+
25+
26+
public function testToArray(): void
27+
{
28+
$raw = '{"term":{"user":{"value":"kimchy"}}}';
29+
$wrapper = new \Spameri\ElasticQuery\Query\Wrapper($raw);
30+
31+
$array = $wrapper->toArray();
32+
33+
\Tester\Assert::same(\base64_encode($raw), $array['wrapper']['query']);
34+
}
35+
36+
37+
public function testKey(): void
38+
{
39+
$wrapper = new \Spameri\ElasticQuery\Query\Wrapper('{}');
40+
41+
\Tester\Assert::contains('wrapper_', $wrapper->key());
42+
}
43+
44+
45+
public function tearDown(): void
46+
{
47+
$ch = \curl_init();
48+
\curl_setopt($ch, \CURLOPT_URL, \ELASTICSEARCH_HOST . '/' . self::INDEX);
49+
\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
50+
\curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'DELETE');
51+
\curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
52+
53+
\curl_exec($ch);
54+
}
55+
56+
}
57+
58+
(new Wrapper())->run();

0 commit comments

Comments
 (0)