From 32d2949a9e168c3fcb1cf94c282c797d23901aa5 Mon Sep 17 00:00:00 2001 From: Elliot Wright Date: Fri, 23 Jun 2017 10:59:56 +0100 Subject: [PATCH] Patch: Updated sorting to allow specifying unmapped_type option. (#38) --- .../Elasticsearch/Gateway/AbstractGateway.php | 4 +- .../Elasticsearch/Query/QueryBuilder.php | 68 +++++++++---------- lib/Byng/Pimcore/Elasticsearch/Query/Sort.php | 25 +++++-- 3 files changed, 54 insertions(+), 43 deletions(-) diff --git a/lib/Byng/Pimcore/Elasticsearch/Gateway/AbstractGateway.php b/lib/Byng/Pimcore/Elasticsearch/Gateway/AbstractGateway.php index ca0522b..d24a7b8 100644 --- a/lib/Byng/Pimcore/Elasticsearch/Gateway/AbstractGateway.php +++ b/lib/Byng/Pimcore/Elasticsearch/Gateway/AbstractGateway.php @@ -176,8 +176,8 @@ protected function processQuery(QueryInterface $query) case "sort": $result = []; - foreach ($query->getCriteria() as $column => $order) { - $result[$column]["order"] = $order; + foreach ($query->getCriteria() as $sorting) { + $result[] = $sorting; } break; diff --git a/lib/Byng/Pimcore/Elasticsearch/Query/QueryBuilder.php b/lib/Byng/Pimcore/Elasticsearch/Query/QueryBuilder.php index a3ac7bb..6f8f654 100644 --- a/lib/Byng/Pimcore/Elasticsearch/Query/QueryBuilder.php +++ b/lib/Byng/Pimcore/Elasticsearch/Query/QueryBuilder.php @@ -16,7 +16,7 @@ use Byng\Pimcore\Elasticsearch\Query\QueryInterface; /** - * + * * QueryBuilder defines the query to send to elasticsearch. * * @author Asim Liaquat @@ -27,12 +27,12 @@ class QueryBuilder * @var Query */ private $query; - + /** * @var Filter */ private $filter; - + /** * @var int */ @@ -42,15 +42,15 @@ class QueryBuilder * @var int */ private $from; - + /** * @var Sort */ private $sort; - + /** * QueryBuilder constructor - * + * * @param Query $query * @param Filter $filter */ @@ -62,7 +62,7 @@ public function __construct(Query $query = null, Filter $filter = null) /** * Get query - * + * * @return Query */ public function getQuery() @@ -72,7 +72,7 @@ public function getQuery() /** * Get filter - * + * * @return Filter */ public function getFilter() @@ -82,7 +82,7 @@ public function getFilter() /** * Set query - * + * * @param Query $query */ public function setQuery(Query $query) @@ -92,7 +92,7 @@ public function setQuery(Query $query) /** * Set filter - * + * * @param Filter $filter */ public function setFilter(Filter $filter) @@ -102,7 +102,7 @@ public function setFilter(Filter $filter) /** * Get the number or results to return - * + * * @return int */ public function getSize() @@ -112,7 +112,7 @@ public function getSize() /** * Set the number of results to return - * + * * @param int $size */ public function setSize($size) @@ -122,7 +122,7 @@ public function setSize($size) /** * Get the offset to fetch the results from - * + * * @return int|null */ public function getFrom() @@ -132,7 +132,7 @@ public function getFrom() /** * Set the offset to fetch the results from - * + * * @param int $from */ public function setFrom($from) @@ -142,7 +142,7 @@ public function setFrom($from) /** * Get sort criteria - * + * * @return Sort */ public function getSort() @@ -152,7 +152,7 @@ public function getSort() /** * Set sort criteria - * + * * @param Sort $sort */ public function setSort(Sort $sort) @@ -168,15 +168,15 @@ public function setSort(Sort $sort) public function toArray() { $body = []; - + if ($query = $this->getQuery()) { $body = $this->processQuery($query); } - + if ($filter = $this->getFilter()) { $body = array_merge($body, $this->processQuery($filter)); } - + if ($from = $this->getFrom()) { $body["from"] = $from; } @@ -184,11 +184,11 @@ public function toArray() if ($size = $this->getSize()) { $body["size"] = $size; } - + if ($sort = $this->getSort()) { $body["sort"] = $this->processQuery($sort); } - + return $body; } @@ -202,15 +202,15 @@ public function toArray() protected function processQuery(QueryInterface $query) { switch ($query->getType()) { - + case "query": $result["query"] = $this->processQuery($query->getBoolQuery()); break; - + case "filter": $result["filter"] = $this->processQuery($query->getQuery()); break; - + case "bool": $boolResult = []; @@ -230,10 +230,10 @@ protected function processQuery(QueryInterface $query) $result["bool"] = $boolResult; break; - + case "match": $result = []; - + if ($operator = $query->getOperator()) { $result["match"][$query->getField()] = [ "query" => $query->getQuery(), @@ -243,16 +243,16 @@ protected function processQuery(QueryInterface $query) $result["match"][$query->getField()] = $query->getQuery(); } break; - + case "range": $result = []; $result["range"][$query->getField()] = $query->getRanges(); break; - + case "sort": $result = []; - foreach ($query->getCriteria() as $column => $order) { - $result[$column]["order"] = $order; + foreach ($query->getCriteria() as $sorting) { + $result[] = $sorting; } break; @@ -261,7 +261,7 @@ protected function processQuery(QueryInterface $query) $query->getField() => $query->getTerms() ]; break; - + case "prefix": $result["prefix"] = [ $query->getField() => $query->getQuery() @@ -279,18 +279,18 @@ protected function processQuery(QueryInterface $query) $query->getField() => $query->getQuery() ]; break; - + case "constant_score": $result["constant_score"] = $this->processQuery($query->getFilter()); break; - + case "nested": $result["nested"] = [ "path" => $query->getPath(), "query" => $this->processQuery($query->getQuery()) ]; break; - + default: throw new \InvalidArgumentException(sprintf( "Unknown query type '%s' given.", diff --git a/lib/Byng/Pimcore/Elasticsearch/Query/Sort.php b/lib/Byng/Pimcore/Elasticsearch/Query/Sort.php index 13e26a0..6dedefc 100644 --- a/lib/Byng/Pimcore/Elasticsearch/Query/Sort.php +++ b/lib/Byng/Pimcore/Elasticsearch/Query/Sort.php @@ -37,25 +37,36 @@ public function __construct() /** * Add a sorting criteria. Can be called multiple times to sort by more than * one column. - * + * * @param string $column * @param string $order + * @param mixed $unmappedType */ - public function addCriteria($column, $order) + public function addCriteria($column, $order, $unmappedType = false) { - $this->data[$column] = $order; + $sort = [ + "order" => $order + ]; + + if ($unmappedType) { + $sort["unmapped_type"] = $unmappedType; + } + + $this->data[] = [ + $column => $sort + ]; } - + /** * Get sort criteria - * + * * @return array */ public function getCriteria() { return $this->data; } - + /** * Resets the previously added criteria */ @@ -63,7 +74,7 @@ public function clear() { $this->data = []; } - + /** * {@inheritdoc} */