Skip to content

Commit 67880da

Browse files
committed
Merge pull request #735 from jdeniau/weight-in-function-score
Implement the `weight` in the function score query
2 parents 9d284d5 + a698916 commit 67880da

File tree

2 files changed

+114
-17
lines changed

2 files changed

+114
-17
lines changed

lib/Elastica/Query/FunctionScore.php

+40-15
Original file line numberDiff line numberDiff line change
@@ -56,30 +56,35 @@ public function setFilter(AbstractFilter $filter)
5656
* @param string $functionType valid values are DECAY_* constants and script_score
5757
* @param array|float $functionParams the body of the function. See documentation for proper syntax.
5858
* @param AbstractFilter $filter optional filter to apply to the function
59+
* @param float $weight function weight
5960
* @return \Elastica\Query\FunctionScore
6061
*/
61-
public function addFunction($functionType, $functionParams, AbstractFilter $filter = null)
62+
public function addFunction($functionType, $functionParams, AbstractFilter $filter = null, $weight = null)
6263
{
6364
$function = array(
6465
$functionType => $functionParams,
6566
);
6667
if (!is_null($filter)) {
6768
$function['filter'] = $filter->toArray();
6869
}
69-
$this->_functions[] = $function;
70+
if ($weight !== null) {
71+
$function['weight'] = $weight;
72+
}
7073

74+
$this->_functions[] = $function;
7175
return $this;
7276
}
7377

7478
/**
7579
* Add a script_score function to the query
7680
* @param Script $script a Script object
7781
* @param AbstractFilter $filter an optional filter to apply to the function
82+
* @param float $weight the weight of the function
7883
* @return \Elastica\Query\FunctionScore
7984
*/
80-
public function addScriptScoreFunction(Script $script, AbstractFilter $filter = null)
85+
public function addScriptScoreFunction(Script $script, AbstractFilter $filter = null, $weight = null)
8186
{
82-
return $this->addFunction('script_score', $script->toArray(), $filter);
87+
return $this->addFunction('script_score', $script->toArray(), $filter, $weight);
8388
}
8489

8590
/**
@@ -91,12 +96,20 @@ public function addScriptScoreFunction(Script $script, AbstractFilter $filter =
9196
* @param string $offset If defined, this function will only be computed for documents with a distance from the origin greater than this value
9297
* @param float $decay optionally defines how documents are scored at the distance given by the $scale parameter
9398
* @param float $scaleWeight optional factor by which to multiply the score at the value provided by the $scale parameter
99+
* @param float $weight optional factor by which to multiply the score at the value provided by the $scale parameter
94100
* @param AbstractFilter $filter a filter associated with this function
95101
* @return \Elastica\Query\FunctionScore
96102
*/
97-
public function addDecayFunction($function, $field, $origin, $scale, $offset = null, $decay = null, $scaleWeight = null,
98-
AbstractFilter $filter = null)
99-
{
103+
public function addDecayFunction(
104+
$function,
105+
$field,
106+
$origin,
107+
$scale,
108+
$offset = null,
109+
$decay = null,
110+
$weight = null,
111+
AbstractFilter $filter = null
112+
) {
100113
$functionParams = array(
101114
$field => array(
102115
'origin' => $origin,
@@ -109,32 +122,44 @@ public function addDecayFunction($function, $field, $origin, $scale, $offset = n
109122
if (!is_null($decay)) {
110123
$functionParams[$field]['decay'] = (float) $decay;
111124
}
112-
if (!is_null($scaleWeight)) {
113-
$functionParams[$field]['scale_weight'] = (float) $scaleWeight;
114-
}
115125

116-
return $this->addFunction($function, $functionParams, $filter);
126+
return $this->addFunction($function, $functionParams, $filter, $weight);
117127
}
118128

119129
/**
120130
* Add a boost_factor function to the query
131+
*
121132
* @param float $boostFactor the boost factor value
122133
* @param AbstractFilter $filter a filter associated with this function
134+
*
135+
* @return void
136+
* @deprecated
123137
*/
124138
public function addBoostFactorFunction($boostFactor, AbstractFilter $filter = null)
125139
{
126-
$this->addFunction('boost_factor', $boostFactor, $filter);
140+
$this->addWeightFunction($boostFactor, $filter);
141+
}
142+
143+
/**
144+
* @param float $weight the weight of the function
145+
* @param AbstractFilter $filter a filter associated with this function
146+
*
147+
* @return void
148+
*/
149+
public function addWeightFunction($weight, AbstractFilter $filter = null)
150+
{
151+
$this->addFunction('weight', $weight, $filter);
127152
}
128153

129154
/**
130155
* Add a random_score function to the query
131156
* @param number $seed the seed value
132157
* @param AbstractFilter $filter a filter associated with this function
133-
* @param float $boost an optional boost value associated with this function
158+
* @param float $weight an optional boost value associated with this function
134159
*/
135-
public function addRandomScoreFunction($seed, AbstractFilter $filter = null, $boost = null)
160+
public function addRandomScoreFunction($seed, AbstractFilter $filter = null, $weight = null)
136161
{
137-
$this->addFunction('random_score', array('seed' => $seed), $filter, $boost);
162+
$this->addFunction('random_score', array('seed' => $seed), $filter, $weight);
138163
}
139164

140165
/**

test/lib/Elastica/Test/Query/FunctionScoreTest.php

+74-2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,52 @@ public function testToArray()
9494
$this->assertEquals($expected, $query->toArray());
9595
}
9696

97+
public function testDecayWeight()
98+
{
99+
$priceOrigin = 0;
100+
$locationScale = '2mi';
101+
$priceScale = 9.25;
102+
$query = new FunctionScore();
103+
$childQuery = new \Elastica\Query\MatchAll();
104+
$query->setQuery($childQuery);
105+
$query->addDecayFunction(
106+
FunctionScore::DECAY_GAUSS,
107+
'location',
108+
$this->locationOrigin,
109+
$locationScale,
110+
null,
111+
null,
112+
.5
113+
);
114+
$query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'price', $priceOrigin, $priceScale, null, null, 2);
115+
$expected = array(
116+
'function_score' => array(
117+
'query' => $childQuery->toArray(),
118+
'functions' => array(
119+
array(
120+
'gauss' => array(
121+
'location' => array(
122+
'origin' => $this->locationOrigin,
123+
'scale' => $locationScale
124+
)
125+
),
126+
'weight' => .5,
127+
),
128+
array(
129+
'gauss' => array(
130+
'price' => array(
131+
'origin' => $priceOrigin,
132+
'scale' => $priceScale
133+
)
134+
),
135+
'weight' => 2,
136+
)
137+
)
138+
)
139+
);
140+
$this->assertEquals($expected, $query->toArray());
141+
}
142+
97143
public function testGauss()
98144
{
99145
$query = new FunctionScore();
@@ -107,7 +153,7 @@ public function testGauss()
107153
$this->assertEquals("Mr. Frostie's", $result0['name']);
108154
}
109155

110-
public function testBoostFactor()
156+
public function testWeight()
111157
{
112158
$filter = new Term(array('price' => 4.5));
113159
$query = new FunctionScore();
@@ -116,7 +162,7 @@ public function testBoostFactor()
116162
'function_score' => array(
117163
'functions' => array(
118164
array(
119-
'boost_factor' => 5.0,
165+
'weight' => 5.0,
120166
'filter' => array(
121167
'term' => array(
122168
'price' => 4.5
@@ -170,6 +216,32 @@ public function testRandomScore()
170216
$this->assertEquals("Miller's Field", $result0['name']);
171217
}
172218

219+
public function testRandomScoreWeight()
220+
{
221+
$filter = new Term(array('price' => 4.5));
222+
$query = new FunctionScore();
223+
$query->addRandomScoreFunction(2, $filter, 2);
224+
$expected = array(
225+
'function_score' => array(
226+
'functions' => array(
227+
array(
228+
'random_score' => array(
229+
'seed' => 2
230+
),
231+
'filter' => array(
232+
'term' => array(
233+
'price' => 4.5
234+
)
235+
),
236+
'weight' => 2,
237+
)
238+
)
239+
)
240+
);
241+
242+
$this->assertEquals($expected, $query->toArray());
243+
}
244+
173245
public function testRandomScoreWithoutSeed()
174246
{
175247
$query = new FunctionScore();

0 commit comments

Comments
 (0)