Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
Support for setting _source
Browse files Browse the repository at this point in the history
  • Loading branch information
hungneox committed Jan 19, 2018
1 parent 368bbff commit aeb8413
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Search/Search.php
Expand Up @@ -21,6 +21,11 @@ class Search
*/
private $query;

/**
* @var array
*/
private $source;

/**
* @var Sort
*/
Expand Down Expand Up @@ -135,6 +140,24 @@ public function getSort()
return $this->sort;
}

/**
* @param array $source
*
* @return $this
*/
public function setSource(array $source)
{
$this->source = $source;
return $this;
}

/**
* @return array
*/
public function getSource()
{
return $this->source;
}

/**
* @return AggregationCollection
Expand Down Expand Up @@ -253,6 +276,10 @@ public function buildBody()
}
}

if (!empty($this->getSource())) {
$body['_source'] = $this->getSource();
}

$aggregations = $this->getAggregations();
if ($aggregations->count() > 0) {
$body['aggs'] = $aggregations->toArray();
Expand Down
23 changes: 23 additions & 0 deletions tests/Search/SearchTest.php
Expand Up @@ -86,6 +86,9 @@ public function testSetterGetter()
$this->search->setSize(100);
$this->assertEquals(100, $this->search->getSize());

$this->search->setSource(['*']);
$this->assertEquals(['*'], $this->search->getSource());

$this->search->setSort($this->sort);
$this->assertInstanceOf(Sort::class, $this->search->getSort());

Expand Down Expand Up @@ -173,6 +176,26 @@ public function testToArray()
], $this->search->buildBody());
}

public function testToArrayWithSource()
{
$this->search = $this->service->createSearch();
$this->search->setPage(1);
$this->search->setSize(100);
$this->search->setSource(['id', 'title', 'description']);

$this->assertEquals([
'query' => ['match_all' => new \stdClass()],
'size' => 100,
'from' => 0,
'_source' => [
'id',
'title',
'description'
],
], $this->search->buildBody());
}


/**
* Test adding an array of aggregation to Search
*/
Expand Down

0 comments on commit aeb8413

Please sign in to comment.