Skip to content

Commit

Permalink
Merge pull request #653 from krzaczek/patch-1
Browse files Browse the repository at this point in the history
Add setQuery() method to Elastica\Query\ConstantScore
  • Loading branch information
ruflin committed Jul 18, 2014
2 parents c7590a5 + a9e31c3 commit c276b90
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
CHANGES

2014-07-14
- Add setQuery() method to Elastica\Query\ConstantScore #653

2014-07-12
- Be able to configure ES host/port via ENV var in test env #652

Expand Down
15 changes: 15 additions & 0 deletions lib/Elastica/Query/ConstantScore.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ public function setFilter($filter)
return $this->setParam('filter', $filter);
}

/**
* Set query
*
* @param array|\Elastica\Query\AbstractQuery $query
* @return \Elastica\Query\ConstantScore Query object
*/
public function setQuery($query)
{
if ($query instanceof AbstractQuery) {
$query = $query->toArray();
}

return $this->setParam('query', $query);
}

/**
* Set boost
*
Expand Down
47 changes: 47 additions & 0 deletions test/lib/Elastica/Test/Query/ConstantScoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
use Elastica\Filter\Term;
use Elastica\Filter\Ids;
use Elastica\Query\ConstantScore;
use Elastica\Query\MatchAll;
use Elastica\Test\Base as BaseTest;
use Elastica\Index;
use Elastica\Document;
use Elastica\Type;

class ConstantScoreTest extends BaseTest
{
Expand Down Expand Up @@ -101,6 +105,49 @@ public function testConstruct()

}

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' => 'hans@test.com', 'username' => 'hans'));
$type->addDocument($doc);
$doc = new Document(2, array('id' => 2, 'email' => 'emil@test.com', 'username' => 'emil'));
$type->addDocument($doc);
$doc = new Document(3, array('id' => 3, 'email' => 'ruth@test.com', '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);

}

public function testConstructEmpty()
{
$query = new ConstantScore();
Expand Down

0 comments on commit c276b90

Please sign in to comment.