Skip to content

Commit 18935f9

Browse files
committed
Support dynamic mapping behavior
1 parent 8959364 commit 18935f9

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use DirectoryTree\OpenSearchAdapter\Indices\Mapping;
4242
use DirectoryTree\OpenSearchAdapter\Indices\Settings;
4343

4444
$mapping = (new Mapping)
45+
->dynamic('strict')
4546
->keyword('id')
4647
->text('title')
4748
->object('author', [

src/Indices/Mapping.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
*/
1111
class Mapping
1212
{
13+
/**
14+
* The dynamic field mapping behavior.
15+
*/
16+
protected bool|string|null $dynamic = null;
17+
1318
/**
1419
* The mapping property definitions.
1520
*/
@@ -712,6 +717,18 @@ public function disableSource(): self
712717
return $this;
713718
}
714719

720+
/**
721+
* Set the dynamic field mapping behavior.
722+
*
723+
* @see https://docs.opensearch.org/latest/mappings/mapping-parameters/dynamic/
724+
*/
725+
public function dynamic(bool|string $dynamic): self
726+
{
727+
$this->dynamic = $dynamic;
728+
729+
return $this;
730+
}
731+
715732
/**
716733
* Add a dynamic template definition.
717734
*
@@ -737,6 +754,10 @@ public function toArray(): array
737754

738755
$properties = $this->properties->toArray();
739756

757+
if (isset($this->dynamic)) {
758+
$mapping['dynamic'] = $this->dynamic;
759+
}
760+
740761
if (isset($this->isFieldNamesEnabled)) {
741762
$mapping['_field_names'] = [
742763
'enabled' => $this->isFieldNamesEnabled,

tests/Unit/Indices/MappingTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@
4444
], $mapping->toArray());
4545
});
4646

47+
it('sets dynamic field mapping behavior', function (bool|string $dynamic) {
48+
$mapping = (new Mapping)->dynamic($dynamic);
49+
50+
expect($mapping->toArray())->toBe([
51+
'dynamic' => $dynamic,
52+
]);
53+
})->with([
54+
true,
55+
false,
56+
'strict',
57+
'strict_allow_templates',
58+
]);
59+
4760
it('casts default values to an empty array', function () {
4861
$this->assertSame([], (new Mapping)->toArray());
4962
});

0 commit comments

Comments
 (0)