feat(query): add shape query · Spameri/ElasticQuery@776a6b6 · GitHub
Skip to content

Commit 776a6b6

Browse files
Spamerczclaude
andcommitted
feat(query): add shape query
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 089111e commit 776a6b6

3 files changed

Lines changed: 133 additions & 0 deletions

File tree

doc/02-query-objects.md

Lines changed: 14 additions & 0 deletions

src/Query/Shape.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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-shape-query.html
9+
*/
10+
class Shape implements \Spameri\ElasticQuery\Query\LeafQueryInterface
11+
{
12+
13+
/**
14+
* @param array<string, mixed> $shape
15+
*/
16+
public function __construct(
17+
private string $field,
18+
private array $shape,
19+
private string $relation = 'intersects',
20+
private bool|null $ignoreUnmapped = null,
21+
)
22+
{
23+
if ( ! \in_array($relation, ['intersects', 'disjoint', 'within', 'contains'], true)) {
24+
throw new \Spameri\ElasticQuery\Exception\InvalidArgumentException(
25+
'Shape relation must be one of: intersects, disjoint, within, contains.',
26+
);
27+
}
28+
}
29+
30+
31+
public function key(): string
32+
{
33+
return 'shape_' . $this->field;
34+
}
35+
36+
37+
/**
38+
* @return array<string, array<string, array<string, mixed>>>
39+
*/
40+
public function toArray(): array
41+
{
42+
$inner = [
43+
'shape' => $this->shape,
44+
'relation' => $this->relation,
45+
];
46+
47+
if ($this->ignoreUnmapped !== null) {
48+
$inner['ignore_unmapped'] = $this->ignoreUnmapped;
49+
}
50+
51+
return [
52+
'shape' => [
53+
$this->field => $inner,
54+
],
55+
];
56+
}
57+
58+
}
Lines changed: 61 additions & 0 deletions

0 commit comments

Comments
 (0)