Elastica\Query\ConstantScore::setQuery PHP Method

setQuery() public method

Set query.
public setQuery ( array | Elastica\Query\AbstractQuery $query )
$query array | Elastica\Query\AbstractQuery
    public function setQuery($query)
    {
        if (!is_array($query) && !$query instanceof AbstractQuery) {
            throw new InvalidException('Invalid parameter. Has to be array or instance of Elastica\\Query\\AbstractQuery');
        }
        return $this->setParam('query', $query);
    }

Usage Example

 public function testQuery()
 {
     $client = $this->_getClient();
     $index = new Index($client, 'test');
     $index->create(array(), true);
     $type = new Type($index, 'constant_score');
     $doc = new Document(1, array('id' => 1, 'email' => '*****@*****.**', 'username' => 'hans'));
     $type->addDocument($doc);
     $doc = new Document(2, array('id' => 2, 'email' => '*****@*****.**', 'username' => 'emil'));
     $type->addDocument($doc);
     $doc = new Document(3, array('id' => 3, 'email' => '*****@*****.**', 'username' => 'ruth'));
     $type->addDocument($doc);
     // Refresh index
     $index->refresh();
     $boost = 1.3;
     $query_match = new MatchAll();
     $query = new ConstantScore();
     $query->setQuery($query_match);
     $query->setBoost($boost);
     $expectedArray = array('constant_score' => array('query' => $query_match->toArray(), 'boost' => $boost));
     $this->assertEquals($expectedArray, $query->toArray());
     $resultSet = $type->search($query);
     $results = $resultSet->getResults();
     $this->assertEquals($resultSet->count(), 3);
     $this->assertEquals($results[1]->getScore(), 1);
 }