From 505770ebe3e35ad5e3917c3e30630d7bdbfbfd02 Mon Sep 17 00:00:00 2001 From: Rob Ruchte Date: Tue, 11 May 2021 21:32:28 -0400 Subject: [PATCH] Adds the ability to specify source fields to return for a search. --- src/Search.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/Search.php b/src/Search.php index 89726af4..6397b96d 100644 --- a/src/Search.php +++ b/src/Search.php @@ -67,6 +67,13 @@ class Search */ private $source; + /** + * Allows to selectively load specific source fields for each document represented by a search hit. + * + * @var array + */ + private $sourceFields; + /** * Allows to selectively load specific stored fields for each document represented by a search hit. * @@ -537,6 +544,26 @@ public function setSource($source) return $this; } + /** + * @return array + */ + public function getSourceFields() + { + return $this->sourceFields; + } + + /** + * @param array $sourceFields + * + * @return $this + */ + public function setSourceFields($sourceFields) + { + $this->sourceFields = $sourceFields; + + return $this; + } + /** * @return array */ @@ -793,6 +820,12 @@ public function toArray() 'trackTotalHits' => 'track_total_hits', ]; + if(!empty($this->sourceFields)) + { + unset($params['source']); + $params['sourceFields'] = '_source'; + } + foreach ($params as $field => $param) { if ($this->$field !== null) { $output[$param] = $this->$field;