Skip to content

Commit

Permalink
add body() query method
Browse files Browse the repository at this point in the history
  • Loading branch information
basemkhirat committed Mar 7, 2017
1 parent baf872b commit 03194d2
Showing 1 changed file with 58 additions and 42 deletions.
100 changes: 58 additions & 42 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,48 +342,6 @@ protected function getSkip()
return $this->skip;
}


/**
* Generate the query body
* @return array
*/
protected function getBody()
{

$body = [];

if (count($this->_source)) {
$body["_source"] = $this->_source;
}

$bool = [];

if (count($this->must)) {
$bool["must"] = $this->must;
}

if (count($this->must_not)) {
$bool["must_not"] = $this->must_not;
}

if (count($this->filter)) {
$bool["filter"] = $this->filter;
}

if (count($bool)) {
$body["query"]["bool"] = $bool;
}

if (count($this->sort)) {
$body["sort"] = $this->sort;
}

$this->body = $body;

return $body;
}


/**
* Set the sorting field
* @param $field
Expand Down Expand Up @@ -734,6 +692,63 @@ public function search($q = NULL, $boost = 1)

}

/**
* Generate the query body
* @return array
*/
protected function getBody()
{

$body = $this->body;

if (count($this->_source)) {

$_source = array_key_exists("_source", $body) ? $body["_source"] : [];

$body["_source"] = array_unique(array_merge($_source, $this->_source));
}

if (count($this->must)) {
$body["query"]["bool"]["must"] = $this->must;
}

if (count($this->must_not)) {
$body["query"]["bool"]["must_not"] = $this->must_not;
}

if (count($this->filter)) {
$body["query"]["bool"]["filter"] = $this->filter;
}

if (count($this->sort)) {

$sortFields = array_key_exists("sort", $body) ? $body["sort"] : [];

$body["sort"] = array_unique(array_merge($sortFields, $this->sort), SORT_REGULAR);

}

$this->body = $body;

return $body;
}


/**
* set the query body array
* @param array $body
* @return $this
*/
function body($body = [])
{

$this->body = $body;

return $this;

}


/**
* Generate the query to be executed
* @return array
Expand Down Expand Up @@ -776,6 +791,7 @@ public function query()

}


/**
* Clear scroll query id
* @param string $scroll_id
Expand Down

0 comments on commit 03194d2

Please sign in to comment.