diff --git a/README.md b/README.md index 723f753..560857c 100644 --- a/README.md +++ b/README.md @@ -436,6 +436,21 @@ $searchParameters->scriptFields([ ], ]); +// runtime mappings +$searchParameters->runtimeMappings([ + 'day_of_week' => [ + 'type' => 'long', + 'script' => [ + 'lang' => 'painless', + 'source' => 'doc[params.field] * params.multiplier', + 'params' => [ + 'field' => 'my_field', + 'multiplier' => 2, + ], + ], + ], +]); + // boost indices $searchParameters->indicesBoost([ ['my-alias' => 1.4], diff --git a/src/Search/SearchParameters.php b/src/Search/SearchParameters.php index eea5e40..fca9676 100644 --- a/src/Search/SearchParameters.php +++ b/src/Search/SearchParameters.php @@ -116,6 +116,18 @@ public function scriptFields(array $scriptFields): self return $this; } + public function runtimeMappings(array $runtimeFields): self + { + $this->params['body']['runtime_mappings'] = $runtimeFields; + return $this; + } + + public function fields(array $fields): self + { + $this->params['body']['fields'] = $fields; + return $this; + } + public function searchType(string $searchType): self { $this->params['search_type'] = $searchType; diff --git a/tests/Unit/Search/SearchParametersTest.php b/tests/Unit/Search/SearchParametersTest.php index b682994..950b797 100644 --- a/tests/Unit/Search/SearchParametersTest.php +++ b/tests/Unit/Search/SearchParametersTest.php @@ -314,6 +314,41 @@ public function test_array_casting_with_script_fields(): void ], $searchParameters->toArray()); } + public function test_array_casting_with_runtime_mappings(): void + { + $searchParameters = (new SearchParameters())->runtimeMappings([ + 'day_of_week' => [ + 'type' => 'long', + 'script' => [ + 'lang' => 'painless', + 'source' => 'doc[params.field] * params.multiplier', + 'params' => [ + 'field' => 'my_field', + 'multiplier' => 2, + ], + ], + ], + ]); + + $this->assertEquals([ + 'body' => [ + 'runtime_mappings' => [ + 'day_of_week' => [ + 'type' => 'long', + 'script' => [ + 'lang' => 'painless', + 'source' => 'doc[params.field] * params.multiplier', + 'params' => [ + 'field' => 'my_field', + 'multiplier' => 2, + ], + ], + ], + ], + ], + ], $searchParameters->toArray()); + } + public function test_array_casting_with_min_score(): void { $searchParameters = (new SearchParameters())->minScore(0.5); @@ -325,6 +360,17 @@ public function test_array_casting_with_min_score(): void ], $searchParameters->toArray()); } + public function test_array_casting_with_fields(): void + { + $searchParameters = (new SearchParameters())->fields(['my_field']); + + $this->assertEquals([ + 'body' => [ + 'fields' => ['my_field'], + ], + ], $searchParameters->toArray()); + } + public function test_array_casting_with_search_type(): void { $searchParameters = (new SearchParameters())->searchType('query_then_fetch');