Skip to content

Commit 03194d2

Browse files
committed
add body() query method
1 parent baf872b commit 03194d2

File tree

1 file changed

+58
-42
lines changed

1 file changed

+58
-42
lines changed

src/Query.php

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -342,48 +342,6 @@ protected function getSkip()
342342
return $this->skip;
343343
}
344344

345-
346-
/**
347-
* Generate the query body
348-
* @return array
349-
*/
350-
protected function getBody()
351-
{
352-
353-
$body = [];
354-
355-
if (count($this->_source)) {
356-
$body["_source"] = $this->_source;
357-
}
358-
359-
$bool = [];
360-
361-
if (count($this->must)) {
362-
$bool["must"] = $this->must;
363-
}
364-
365-
if (count($this->must_not)) {
366-
$bool["must_not"] = $this->must_not;
367-
}
368-
369-
if (count($this->filter)) {
370-
$bool["filter"] = $this->filter;
371-
}
372-
373-
if (count($bool)) {
374-
$body["query"]["bool"] = $bool;
375-
}
376-
377-
if (count($this->sort)) {
378-
$body["sort"] = $this->sort;
379-
}
380-
381-
$this->body = $body;
382-
383-
return $body;
384-
}
385-
386-
387345
/**
388346
* Set the sorting field
389347
* @param $field
@@ -734,6 +692,63 @@ public function search($q = NULL, $boost = 1)
734692

735693
}
736694

695+
/**
696+
* Generate the query body
697+
* @return array
698+
*/
699+
protected function getBody()
700+
{
701+
702+
$body = $this->body;
703+
704+
if (count($this->_source)) {
705+
706+
$_source = array_key_exists("_source", $body) ? $body["_source"] : [];
707+
708+
$body["_source"] = array_unique(array_merge($_source, $this->_source));
709+
}
710+
711+
if (count($this->must)) {
712+
$body["query"]["bool"]["must"] = $this->must;
713+
}
714+
715+
if (count($this->must_not)) {
716+
$body["query"]["bool"]["must_not"] = $this->must_not;
717+
}
718+
719+
if (count($this->filter)) {
720+
$body["query"]["bool"]["filter"] = $this->filter;
721+
}
722+
723+
if (count($this->sort)) {
724+
725+
$sortFields = array_key_exists("sort", $body) ? $body["sort"] : [];
726+
727+
$body["sort"] = array_unique(array_merge($sortFields, $this->sort), SORT_REGULAR);
728+
729+
}
730+
731+
$this->body = $body;
732+
733+
return $body;
734+
}
735+
736+
737+
/**
738+
* set the query body array
739+
* @param array $body
740+
* @return $this
741+
*/
742+
function body($body = [])
743+
{
744+
745+
$this->body = $body;
746+
747+
return $this;
748+
749+
}
750+
751+
737752
/**
738753
* Generate the query to be executed
739754
* @return array
@@ -776,6 +791,7 @@ public function query()
776791

777792
}
778793

794+
779795
/**
780796
* Clear scroll query id
781797
* @param string $scroll_id

0 commit comments

Comments
 (0)