addRandomScoreFunction() public method

Add a random_score function to the query.
public addRandomScoreFunction ( number $seed, AbstractQuery $filter = null, float $weight = null )
$seed number the seed value
$filter AbstractQuery a filter associated with this function
$weight float an optional boost value associated with this function
 public function testRandomScore()
 {
     $filter = new Term(array('price' => 4.5));
     $query = new FunctionScore();
     $query->addRandomScoreFunction(2, $filter);
     $expected = array('function_score' => array('functions' => array(array('random_score' => array('seed' => 2), 'filter' => array('term' => array('price' => 4.5))))));
     $this->assertEquals($expected, $query->toArray());
     $response = $this->type->search($query);
     $results = $response->getResults();
     // the document with the random score should have a score > 1, means it is the first result
     $result0 = $results[1]->getData();
     $this->assertEquals("Miller's Field", $result0['name']);
 }
Esempio n. 2
0
 /**
  * @group unit
  */
 public function testRandomScoreWeight()
 {
     $filter = new Term(array('price' => 4.5));
     $query = new FunctionScore();
     $query->addRandomScoreFunction(2, $filter, 2);
     $expected = array('function_score' => array('functions' => array(array('random_score' => array('seed' => 2), 'filter' => array('term' => array('price' => 4.5)), 'weight' => 2))));
     $this->assertEquals($expected, $query->toArray());
 }