Skip to content

Commit a4015e2

Browse files
Spamerczclaude
andcommitted
feat(query): add span multi query
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 83ae5ee commit a4015e2

3 files changed

Lines changed: 111 additions & 0 deletions

File tree

doc/02-query-objects.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,18 @@ new \Spameri\ElasticQuery\Query\SpanWithin(
683683
);
684684
```
685685

686+
##### SpanMulti Query
687+
Wrap a multi-term query (prefix/wildcard/regexp/fuzzy/range) so it can be used inside other span queries.
688+
- Class: `\Spameri\ElasticQuery\Query\SpanMulti`
689+
- [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-multi-term-query.html)
690+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/SpanMulti.php)
691+
692+
```php
693+
new \Spameri\ElasticQuery\Query\SpanMulti(
694+
match: new \Spameri\ElasticQuery\Query\Prefix(field: 'user', query: 'ki'),
695+
);
696+
```
697+
686698
##### SpanTerm Query
687699
Match a single term in a span-aware way.
688700
- Class: `\Spameri\ElasticQuery\Query\SpanTerm`

src/Query/SpanMulti.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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-span-multi-term-query.html
9+
*/
10+
class SpanMulti implements \Spameri\ElasticQuery\Query\LeafQueryInterface
11+
{
12+
13+
public function __construct(
14+
private \Spameri\ElasticQuery\Query\LeafQueryInterface $match,
15+
)
16+
{
17+
}
18+
19+
20+
public function key(): string
21+
{
22+
return 'span_multi_' . $this->match->key();
23+
}
24+
25+
26+
/**
27+
* @return array<string, array<string, mixed>>
28+
*/
29+
public function toArray(): array
30+
{
31+
return [
32+
'span_multi' => [
33+
'match' => $this->match->toArray(),
34+
],
35+
];
36+
}
37+
38+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SpameriTests\ElasticQuery\Query;
4+
5+
require_once __DIR__ . '/../../bootstrap.php';
6+
7+
8+
class SpanMulti extends \Tester\TestCase
9+
{
10+
11+
private const INDEX = 'spameri_test_query_span_multi';
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+
$span = new \Spameri\ElasticQuery\Query\SpanMulti(
29+
match: new \Spameri\ElasticQuery\Query\Prefix(field: 'user', query: 'ki'),
30+
);
31+
32+
$array = $span->toArray();
33+
34+
\Tester\Assert::true(isset($array['span_multi']['match']['prefix']));
35+
}
36+
37+
38+
public function testKey(): void
39+
{
40+
$span = new \Spameri\ElasticQuery\Query\SpanMulti(
41+
match: new \Spameri\ElasticQuery\Query\Prefix('user', 'ki'),
42+
);
43+
44+
\Tester\Assert::same('span_multi_prefix_user_ki', $span->key());
45+
}
46+
47+
48+
public function tearDown(): void
49+
{
50+
$ch = \curl_init();
51+
\curl_setopt($ch, \CURLOPT_URL, \ELASTICSEARCH_HOST . '/' . self::INDEX);
52+
\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
53+
\curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'DELETE');
54+
\curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
55+
56+
\curl_exec($ch);
57+
}
58+
59+
}
60+
61+
(new SpanMulti())->run();

0 commit comments

Comments
 (0)