Skip to content

Commit

Permalink
Merge pull request #2 from hanspeerstra/feature-script-fields
Browse files Browse the repository at this point in the history
Add script_fields support
  • Loading branch information
babenkoivan authored Feb 1, 2021
2 parents bafe7f1 + 9e12c40 commit e89e5db
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,20 @@ $request->setTrackTotalHits(true);
// track scores
$request->setTrackScores(true);

// script fields
$request->setScriptFields([
'my_doubled_field' => [
'script' => [
'lang' => 'painless',
'source' => 'doc[params.field] * params.multiplier',
'params' => [
'field' => 'my_field',
'multiplier' => 2,
],
],
],
]);

// boost indices
$request->setIndicesBoost([
['my-alias' => 1.4],
Expand Down
11 changes: 11 additions & 0 deletions src/Search/SearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ final class SearchRequest implements ArrayableInterface
* @var bool|null
*/
private $trackScores;
/**
* @var array|null
*/
private $scriptFields;

public function __construct(array $query)
{
Expand Down Expand Up @@ -142,6 +146,12 @@ public function setTrackScores(bool $trackScores): self
return $this;
}

public function setScriptFields(array $scriptFields): self
{
$this->scriptFields = $scriptFields;
return $this;
}

public function toArray(): array
{
$request = [
Expand All @@ -161,6 +171,7 @@ public function toArray(): array
'trackTotalHits' => 'track_total_hits',
'indicesBoost' => 'indices_boost',
'trackScores' => 'track_scores',
'scriptFields' => 'script_fields',
] as $property => $requestParameter) {
if (isset($this->$property)) {
$request[$requestParameter] = $this->$property;
Expand Down
38 changes: 38 additions & 0 deletions tests/Unit/Search/SearchRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,42 @@ public function test_array_casting_with_track_scores(): void
'track_scores' => true,
], $request->toArray());
}

public function test_array_casting_with_script_fields(): void
{
$request = new SearchRequest([
'match_all' => new stdClass(),
]);

$request->setScriptFields([
'my_doubled_field' => [
'script' => [
'lang' => 'painless',
'source' => 'doc[params.field] * params.multiplier',
'params' => [
'field' => 'my_field',
'multiplier' => 2,
],
],
],
]);

$this->assertEquals([
'query' => [
'match_all' => new stdClass(),
],
'script_fields' => [
'my_doubled_field' => [
'script' => [
'lang' => 'painless',
'source' => 'doc[params.field] * params.multiplier',
'params' => [
'field' => 'my_field',
'multiplier' => 2,
],
],
],
],
], $request->toArray());
}
}

0 comments on commit e89e5db

Please sign in to comment.