This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from digiaonline/support-script-scoring-function
Support script scoring function
- Loading branch information
Showing
2 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace Nord\Lumen\Elasticsearch\Search\Scoring\Functions; | ||
|
||
/** | ||
* Class ScriptScoringFunction | ||
* @package Nord\Lumen\Elasticsearch\Search\Scoring\Functions | ||
*/ | ||
class ScriptScoringFunction extends AbstractScoringFunction | ||
{ | ||
/** | ||
* @var mixed | ||
*/ | ||
private $params; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $inline; | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getParams() | ||
{ | ||
return $this->params; | ||
} | ||
|
||
/** | ||
* @param $params | ||
* | ||
* @return $this | ||
*/ | ||
public function setParams($params) | ||
{ | ||
$this->params = $params; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getInline() | ||
{ | ||
return $this->inline; | ||
} | ||
|
||
/** | ||
* @param string $inline | ||
* | ||
* @return $this | ||
*/ | ||
public function setInline($inline) | ||
{ | ||
$this->inline = $inline; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function toArray() | ||
{ | ||
$script = [ | ||
'inline' => $this->getInline(), | ||
]; | ||
|
||
$params = $this->getParams(); | ||
|
||
if (!empty($params)) { | ||
$script['params'] = $this->getParams(); | ||
} | ||
|
||
return [ | ||
'script_score' => [ | ||
'script' => $script | ||
] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Nord\Lumen\Elasticsearch\Tests\Search\Query\Compound; | ||
|
||
use Nord\Lumen\Elasticsearch\Search\Scoring\Functions\ScriptScoringFunction; | ||
use Nord\Lumen\Elasticsearch\Tests\Search\Query\AbstractQueryTestCase; | ||
|
||
class FunctionScoreQueryTest extends AbstractQueryTestCase | ||
{ | ||
/** | ||
* | ||
*/ | ||
public function testToArray() | ||
{ | ||
$boolQuery = $this->queryBuilder->createBoolQuery(); | ||
$boolQuery->addMust($this->queryBuilder->createTermQuery()->setField('field1')->setValue('value1')); | ||
|
||
$scriptScoringFunction = (new ScriptScoringFunction()) | ||
->setParams([ | ||
'featured_content' => ['abc', 'xyz'] | ||
])->setInline("int score =0;for(item in params.featured_content){ if (doc['identifier'].indexOf(item) > -1) { score+=10;}}return score;"); | ||
|
||
$functionScoreQuery = $this->queryBuilder->createFunctionScoreQuery(); | ||
|
||
$functionScoreQuery | ||
->setQuery($boolQuery) | ||
->addFunction($scriptScoringFunction); | ||
|
||
$expectedArray = [ | ||
'function_score' => [ | ||
'query' => [ | ||
'bool' => [ | ||
'must' => [ | ||
[ | ||
'term' => ['field1' => 'value1'], | ||
], | ||
], | ||
] | ||
], | ||
'functions' => [ | ||
[ | ||
'script_score' => [ | ||
'script' => [ | ||
'inline' => "int score =0;for(item in params.featured_content){ if (doc['identifier'].indexOf(item) > -1) { score+=10;}}return score;", | ||
'params' => [ | ||
'featured_content' => ['abc', 'xyz'] | ||
] | ||
] | ||
] | ||
] | ||
] | ||
] | ||
]; | ||
|
||
$this->assertEquals($expectedArray, $functionScoreQuery->toArray()); | ||
} | ||
} |