Skip to content

Commit

Permalink
Allow bool in Query::setSource function #818
Browse files Browse the repository at this point in the history
  • Loading branch information
ruflin committed Apr 24, 2015
1 parent cc60e8f commit 0d41761
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
3 changes: 3 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
CHANGES

2015-04-23
- Allow bool in Query::setSource function #818 http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html

2015-04-15
- deleteByQuery() implemented in Elastica\Index

Expand Down
6 changes: 3 additions & 3 deletions lib/Elastica/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,13 +412,13 @@ public function setRescore($rescore)
/**
* Sets the _source field to be returned with every hit
*
* @param array $fields Fields to be returned
* @param array|bool $params Fields to be returned or false to disable source
* @return $this
* @link http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html
*/
public function setSource(array $fields)
public function setSource($params)
{
return $this->setParam('_source', $fields);
return $this->setParam('_source', $params);
}

/**
Expand Down
38 changes: 38 additions & 0 deletions test/lib/Elastica/Test/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Elastica\ScriptFields;
use Elastica\Suggest;
use Elastica\Test\Base as BaseTest;
use Elastica\Type;

class QueryTest extends BaseTest
{
Expand Down Expand Up @@ -356,4 +357,41 @@ public function testSetPostFilterToArrayCast()

$this->assertNotEquals($query->toArray(), $anotherQuery->toArray());
}

public function testNoSource()
{
$index = $this->_createIndex();

$type = new Type($index, 'user');

// Adds 1 document to the index
$doc1 = new Document(1,
array('username' => 'ruflin', 'test' => array('2', '3', '5'))
);
$type->addDocument($doc1);

// To update index
$index->refresh();

$query = Query::create('ruflin');
$resultSet = $type->search($query);

// Disable source
$query->setSource(false);

$resultSetNoSource = $type->search($query);

$this->assertEquals(1, $resultSet->count());
$this->assertEquals(1, $resultSetNoSource->count());

// Tests if no source is in response except id
$result = $resultSetNoSource->current();
$this->assertEquals(1, $result->getId());
$this->assertEmpty($result->getData());

// Tests if source is in response except id
$result = $resultSet->current();
$this->assertEquals(1, $result->getId());
$this->assertNotEmpty($result->getData());
}
}

0 comments on commit 0d41761

Please sign in to comment.