Elastica\Query\FunctionScore::addRandomScoreFunction PHP Method

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 addRandomScoreFunction($seed, AbstractQuery $filter = null, $weight = null)
    {
        return $this->addFunction('random_score', ['seed' => $seed], $filter, $weight);
    }

Usage Example

 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']);
 }
All Usage Examples Of Elastica\Query\FunctionScore::addRandomScoreFunction