Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #73 from digiaonline/support-script-scoring-function
Browse files Browse the repository at this point in the history
Support script scoring function
  • Loading branch information
hungneox committed Nov 29, 2017
2 parents d6b9502 + 81a69ab commit 6a501ca
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/Search/Scoring/Functions/ScriptScoringFunction.php
@@ -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
]
];
}
}
57 changes: 57 additions & 0 deletions tests/Search/Query/Compound/FunctionScoreQueryTest.php
@@ -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());
}
}

0 comments on commit 6a501ca

Please sign in to comment.