From e825facd66842257828c9e2d837ef8c7fb7b5004 Mon Sep 17 00:00:00 2001 From: Asim Liaquat Date: Tue, 25 Jul 2017 10:24:42 +0100 Subject: [PATCH] Added suppot for 'fields' when querying, also had to remove exclusion of content in the results for fields to work properly --- .../Elasticsearch/Gateway/AbstractGateway.php | 4 +++ .../Elasticsearch/Gateway/AssetGateway.php | 8 +----- .../Elasticsearch/Query/QueryBuilder.php | 25 +++++++++++++++++++ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/lib/Byng/Pimcore/Elasticsearch/Gateway/AbstractGateway.php b/lib/Byng/Pimcore/Elasticsearch/Gateway/AbstractGateway.php index 0815f7f..7b58179 100644 --- a/lib/Byng/Pimcore/Elasticsearch/Gateway/AbstractGateway.php +++ b/lib/Byng/Pimcore/Elasticsearch/Gateway/AbstractGateway.php @@ -63,6 +63,10 @@ public function query( $body["sort"] = $this->processQuery($sort); } + if (is_array($fields = $queryBuilder->getFields())) { + $body["fields"] = $fields; + } + return $this->findBy( $body, $additionalOptions, diff --git a/lib/Byng/Pimcore/Elasticsearch/Gateway/AssetGateway.php b/lib/Byng/Pimcore/Elasticsearch/Gateway/AssetGateway.php index ce5cfcb..707f6e2 100644 --- a/lib/Byng/Pimcore/Elasticsearch/Gateway/AssetGateway.php +++ b/lib/Byng/Pimcore/Elasticsearch/Gateway/AssetGateway.php @@ -184,12 +184,6 @@ public function findBy( array $additionalOptions = [], array $resultOptions = [] ) { - $additionalOptions = array_merge($additionalOptions, [ - "_source" => [ - "exclude" => [ "*.content" ] - ] - ]); - $result = $this->doSearch( $this->client, $this->index, @@ -208,7 +202,7 @@ public function findBy( // TODO optimize to use list foreach ($result["hits"]["hits"] as $page) { $id = (int) $page["_id"]; - + if (($asset = Asset::getById($id)) instanceof Asset) { if (isset($resultOptions["raw"]) && $resultOptions["raw"] === true) { $assets[] = $page; diff --git a/lib/Byng/Pimcore/Elasticsearch/Query/QueryBuilder.php b/lib/Byng/Pimcore/Elasticsearch/Query/QueryBuilder.php index 692d9c7..94acbd7 100644 --- a/lib/Byng/Pimcore/Elasticsearch/Query/QueryBuilder.php +++ b/lib/Byng/Pimcore/Elasticsearch/Query/QueryBuilder.php @@ -48,6 +48,11 @@ class QueryBuilder */ private $sort; + /** + * @var array + */ + private $fields; + /** * QueryBuilder constructor * @@ -120,6 +125,22 @@ public function setSize($size) $this->size = $size; } + /** + * @return array|null + */ + public function getFields() + { + return $this->fields; + } + + /** + * @param array|null $fields + */ + public function setFields($fields) + { + $this->fields = $fields; + } + /** * Get the offset to fetch the results from * @@ -189,6 +210,10 @@ public function toArray() $body["sort"] = $this->processQuery($sort); } + if (is_array($this->fields)) { + $body["fields"] = $this->fields; + } + return $body; }