Skip to content

Commit d0eecef

Browse files
Spamerczclaude
andcommitted
feat(query): add span first query
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 813f6c3 commit d0eecef

3 files changed

Lines changed: 117 additions & 0 deletions

File tree

doc/02-query-objects.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,19 @@ new \Spameri\ElasticQuery\Query\Script(
601601

602602
Positional matching — useful for term-position-aware searches. Span clauses can be composed via the `*_near`, `*_or`, etc. queries.
603603

604+
##### SpanFirst Query
605+
Match a span only if it appears within the first N positions.
606+
- Class: `\Spameri\ElasticQuery\Query\SpanFirst`
607+
- [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-first-query.html)
608+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/SpanFirst.php)
609+
610+
```php
611+
new \Spameri\ElasticQuery\Query\SpanFirst(
612+
match: new \Spameri\ElasticQuery\Query\SpanTerm('user', 'kimchy'),
613+
end: 3,
614+
);
615+
```
616+
604617
##### SpanTerm Query
605618
Match a single term in a span-aware way.
606619
- Class: `\Spameri\ElasticQuery\Query\SpanTerm`

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

0 commit comments

Comments
 (0)