File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -42,6 +42,7 @@ use DirectoryTree\OpenSearchAdapter\Indices\Mapping;
4242use DirectoryTree\OpenSearchAdapter\Indices\Settings;
4343
4444$mapping = (new Mapping)
45+ ->dynamic('strict')
4546 ->keyword('id')
4647 ->text('title')
4748 ->object('author', [
Original file line number Diff line number Diff line change 1010 */
1111class 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 ,
Original file line number Diff line number Diff line change 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+
4760it ('casts default values to an empty array ' , function () {
4861 $ this ->assertSame ([], (new Mapping )->toArray ());
4962});
You can’t perform that action at this time.
0 commit comments