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

Commit

Permalink
Add new setter/getter for supporting script_fields and stored_fields
Browse files Browse the repository at this point in the history
  • Loading branch information
hungneox committed Jun 17, 2018
1 parent d50c7ca commit 47b2177
Showing 1 changed file with 85 additions and 27 deletions.
112 changes: 85 additions & 27 deletions src/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class Search
{

/**
* @var string
*/
Expand All @@ -26,6 +27,16 @@ class Search
*/
private $source;

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

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

/**
* @var Sort
*/
Expand All @@ -51,7 +62,6 @@ class Search
*/
private $page = 1;


/**
* Constructor.
*/
Expand All @@ -60,18 +70,18 @@ public function __construct()
$this->aggregations = new AggregationCollection();
}


/**
* @param string $index
*
* @return Search
*/
public function setIndex(string $index)
{
$this->index = $index;

return $this;
}


/**
* @return string
*/
Expand All @@ -80,18 +90,18 @@ public function getIndex()
return $this->index;
}


/**
* @param string $type
*
* @return Search
*/
public function setType(string $type)
{
$this->type = $type;

return $this;
}


/**
* @return string
*/
Expand All @@ -100,18 +110,18 @@ public function getType()
return $this->type;
}


/**
* @param QueryDSL $query
*
* @return Search
*/
public function setQuery(QueryDSL $query)
{
$this->query = $query;

return $this;
}


/**
* @return QueryDSL
*/
Expand All @@ -120,18 +130,18 @@ public function getQuery()
return $this->query;
}


/**
* @param Sort $sort
*
* @return Search
*/
public function setSort(Sort $sort)
{
$this->sort = $sort;

return $this;
}


/**
* @return Sort
*/
Expand All @@ -148,6 +158,7 @@ public function getSort()
public function setSource(array $source)
{
$this->source = $source;

return $this;
}

Expand All @@ -159,6 +170,46 @@ public function getSource()
return $this->source;
}

/**
* @return array|null
*/
public function getScriptFields(): ?array
{
return $this->scriptFields;
}

/**
* @param array $scriptFields
*
* @return Search
*/
public function setScriptFields(array $scriptFields): Search
{
$this->scriptFields = $scriptFields;

return $this;
}

/**
* @return array|null
*/
public function getStoredFields(): ?array
{
return $this->storedFields;
}

/**
* @param array $storedFields
*
* @return Search
*/
public function setStoredFields(array $storedFields): Search
{
$this->storedFields = $storedFields;

return $this;
}

/**
* @return AggregationCollection
*/
Expand All @@ -167,20 +218,21 @@ public function getAggregations()
return $this->aggregations;
}


/**
* @param Aggregation $aggregation
*
* @return Search
*/
public function addAggregation(Aggregation $aggregation)
{
$this->aggregations->add($aggregation);

return $this;
}


/**
* @param array $aggregations
*
* @return Search
*/
public function addAggregations(array $aggregations)
Expand All @@ -190,17 +242,19 @@ public function addAggregations(array $aggregations)
$this->addAggregation($aggregation);
}
}

return $this;
}


/**
* @param int $page
*
* @return Search
*/
public function setPage(int $page)
public function setPage($page)
{
$this->page = $page;
$this->page = (int)$page;

return $this;
}

Expand All @@ -217,14 +271,13 @@ public function getFrom()
*
* @return Search
*/
public function setFrom(int $from)
public function setFrom($from)
{
$this->from = $from;

return $this;
}


/**
* @return int
*/
Expand All @@ -233,18 +286,18 @@ public function getSize()
return $this->size;
}


/**
* @param int $size
*
* @return Search
*/
public function setSize(int $size)
public function setSize($size)
{
$this->size = $size;
$this->size = (int)$size;

return $this;
}


/**
* @return int
*/
Expand All @@ -253,25 +306,24 @@ public function getPage()
return $this->page;
}


/**
* @return array
*/
public function buildBody()
{
$body = [];
$query = $this->getQuery();

if ($query !== null) {
$body['query'] = $query->toArray();
}

if (empty($body['query'])) {
$body['query'] = ['match_all' => new \stdClass()];
}

$sort = $this->getSort();

if ($sort !== null) {
$body['sort'] = $sort->toArray();
}
Expand All @@ -280,6 +332,14 @@ public function buildBody()
$body['_source'] = $this->getSource();
}

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

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

$aggregations = $this->getAggregations();
if ($aggregations->count() > 0) {
$body['aggs'] = $aggregations->toArray();
Expand All @@ -297,9 +357,7 @@ public function buildBody()
$from = ($this->getPage() - 1) * $this->getSize();
}

if ($from > 0) {
$body['from'] = $from;
}
$body['from'] = $from;

return $body;
}
Expand Down

0 comments on commit 47b2177

Please sign in to comment.