Skip to content

Commit

Permalink
Merge pull request #953 from ruFog/ISS-639
Browse files Browse the repository at this point in the history
#639 +field_value_factor
  • Loading branch information
ruflin committed Oct 15, 2015
2 parents 44f530c + 03a145a commit 6fe305a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lib/Elastica/Query/FunctionScore.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ class FunctionScore extends AbstractQuery
const DECAY_EXPONENTIAL = 'exp';
const DECAY_LINEAR = 'linear';

const FIELD_VALUE_FACTOR_MODIFIER_NONE = 'none';
const FIELD_VALUE_FACTOR_MODIFIER_LOG = 'log';
const FIELD_VALUE_FACTOR_MODIFIER_LOG1P = 'log1p';
const FIELD_VALUE_FACTOR_MODIFIER_LOG2P = 'log2p';
const FIELD_VALUE_FACTOR_MODIFIER_LN = 'ln';
const FIELD_VALUE_FACTOR_MODIFIER_LN1P = 'ln1p';
const FIELD_VALUE_FACTOR_MODIFIER_LN2P = 'ln2p';
const FIELD_VALUE_FACTOR_MODIFIER_SQUARE = 'square';
const FIELD_VALUE_FACTOR_MODIFIER_SQRT = 'sqrt';
const FIELD_VALUE_FACTOR_MODIFIER_RECIPROCAL = 'reciprocal';

protected $_functions = array();

/**
Expand Down Expand Up @@ -135,6 +146,33 @@ public function addDecayFunction(
return $this->addFunction($function, $functionParams, $filter, $weight);
}

public function addFieldValueFactorFunction(
$field,
$factor = null,
$modifier = null,
$missing = null,
$weight = null,
AbstractFilter $filter = null
) {
$functionParams = array(
'field' => $field,
);

if (!is_null($factor)) {
$functionParams['factor'] = $factor;
}

if (!is_null($modifier)) {
$functionParams['modifier'] = $modifier;
}

if (!is_null($missing)) {
$functionParams['missing'] = $missing;
}

return $this->addFunction('field_value_factor', $functionParams, $filter, $weight);
}

/**
* Add a boost_factor function to the query.
*
Expand Down
37 changes: 37 additions & 0 deletions test/lib/Elastica/Test/Query/FunctionScoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ protected function _getIndexForTest()
'name' => array('type' => 'string', 'index' => 'not_analyzed'),
'location' => array('type' => 'geo_point'),
'price' => array('type' => 'float'),
'popularity' => array('type' => 'integer'),
));

$type->addDocuments(array(
new Document(1, array(
'name' => "Mr. Frostie's",
'location' => array('lat' => 32.799605, 'lon' => -117.243027),
'price' => 4.5,
'popularity' => null,
)),
new Document(2, array(
'name' => "Miller's Field",
'location' => array('lat' => 32.795964, 'lon' => -117.255028),
'price' => 9.5,
'popularity' => 1,
)),
));

Expand Down Expand Up @@ -323,4 +326,38 @@ public function testSetMinScore()
$this->assertCount(1, $results);
$this->assertEquals(1, $results[0]->getId());
}

/**
* @group functional
*/
public function testFieldValueFactor()
{
$this->_checkVersion('1.6');

$expected = array(
'function_score' => array(
'functions' => array(
array(
'field_value_factor' => array(
'field' => 'popularity',
'factor' => 1.2,
'modifier' => 'sqrt',
'missing' => 0.1, // available from >=1.6
),
),
),
),
);

$query = new FunctionScore();
$query->addFieldValueFactorFunction('popularity', 1.2, FunctionScore::FIELD_VALUE_FACTOR_MODIFIER_SQRT, 0.1);

$this->assertEquals($expected, $query->toArray());

$response = $this->_getIndexForTest()->search($query);
$results = $response->getResults();

$this->assertCount(2, $results);
$this->assertEquals(2, $results[0]->getId());
}
}

0 comments on commit 6fe305a

Please sign in to comment.