From 8e6b7f673b3d4ae486fee18ce5083466ec1b6a0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simonas=20=C5=A0erlinskas?= <simonas.serlinskas@nfq.com> Date: Wed, 5 Aug 2015 10:50:40 +0300 Subject: [PATCH] simplified sorting query builder --- src/Sort/AbstractSort.php | 154 -------------------------------------- src/Sort/FieldSort.php | 98 ++++++++++++++++++++++++ src/Sort/GeoSort.php | 110 --------------------------- src/Sort/ScriptSort.php | 136 --------------------------------- src/Sort/Sort.php | 80 -------------------- src/Sort/Sorts.php | 64 ---------------- 6 files changed, 98 insertions(+), 544 deletions(-) delete mode 100644 src/Sort/AbstractSort.php create mode 100644 src/Sort/FieldSort.php delete mode 100644 src/Sort/GeoSort.php delete mode 100644 src/Sort/ScriptSort.php delete mode 100644 src/Sort/Sort.php delete mode 100644 src/Sort/Sorts.php diff --git a/src/Sort/AbstractSort.php b/src/Sort/AbstractSort.php deleted file mode 100644 index 0e1c1cd..0000000 --- a/src/Sort/AbstractSort.php +++ /dev/null @@ -1,154 +0,0 @@ -<?php - -/* - * This file is part of the ONGR package. - * - * (c) NFQ Technologies UAB <info@nfq.com> - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace ONGR\ElasticsearchDSL\Sort; - -use ONGR\ElasticsearchDSL\BuilderInterface; - -/** - * Abstract class for sorting. - */ -abstract class AbstractSort implements BuilderInterface -{ - /** - * @const ORDER_ASC Sort in ascending order. - */ - const ORDER_ASC = 'asc'; - - /** - * @const ORDER_DESC Sort in descending order. - */ - const ORDER_DESC = 'desc'; - - /** - * @const MODE_MIN Pick the lowest value in multi-valued field. - */ - const MODE_MIN = 'min'; - - /** - * @const MODE_MAX Pick the highest value in multi-valued field. - */ - const MODE_MAX = 'max'; - - /** - * @const MODE_AVG Use the sum of all values as sort value. Only applicable for number based array fields. - */ - const MODE_AVG = 'avg'; - - /** - * @const MODE_SUM Use the average of all values as sort value. Only applicable for number based array fields. - */ - const MODE_SUM = 'sum'; - - /** - * @var string - */ - private $order = self::ORDER_ASC; - - /** - * @var string - */ - private $mode; - - /** - * @var string - */ - private $field; - - /** - * @param string $field Field name. - * @param string $order Order direction. - * @param string $mode Multi-valued field sorting mode [MODE_MIN, MODE_MAX, MODE_AVG, MODE_SUM]. - */ - public function __construct($field, $order, $mode) - { - $this->setField($field); - $this->setOrder($order); - $this->setMode($mode); - } - - /** - * Set multi-valued field sorting mode [MODE_MIN, MODE_MAX, MODE_AVG, MODE_SUM]. - * - * @param string $mode - */ - public function setMode($mode) - { - $this->mode = $mode; - } - - /** - * Returns mode. - * - * @return string - */ - public function getMode() - { - return $this->mode; - } - - /** - * Set order direction. - * - * @param string $order - */ - public function setOrder($order) - { - $this->order = $order; - } - - /** - * @return string - */ - public function getOrder() - { - return $this->order; - } - - /** - * @param string $field - */ - public function setField($field) - { - $this->field = $field; - } - - /** - * @return string - */ - public function getField() - { - return $this->field; - } - - /** - * @return string - */ - abstract public function getType(); - - /** - * {@inheritdoc} - */ - public function toArray() - { - $value = []; - - if ($this->getOrder()) { - $value['order'] = $this->getOrder(); - } - - if ($this->getMode() !== null) { - $value['mode'] = $this->getMode(); - } - - return $value; - } -} diff --git a/src/Sort/FieldSort.php b/src/Sort/FieldSort.php new file mode 100644 index 0000000..394affc --- /dev/null +++ b/src/Sort/FieldSort.php @@ -0,0 +1,98 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchDSL\Sort; + +use ONGR\ElasticsearchDSL\BuilderInterface; + +/** + * Holds all the values required for basic sorting. + */ +class FieldSort implements BuilderInterface +{ + CONST ASC = 'asc'; + CONST DESC = 'desc'; + + /** + * @var string. + */ + private $field; + + /** + * @var array + */ + private $params; + + /** + * @var BuilderInterface + */ + private $nestedFilter; + + /** + * @param string $field Field name. + * @param array $params Params that can be set to field sort. + */ + public function __construct($field, $params = []) + { + $this->field = $field; + $this->params = $params; + } + + /** + * @return BuilderInterface + */ + public function getNestedFilter() + { + return $this->nestedFilter; + } + + /** + * @param BuilderInterface $nestedFilter + */ + public function setNestedFilter(BuilderInterface $nestedFilter) + { + $this->nestedFilter = $nestedFilter; + } + + /** + * Returns element type. + * + * @return string + */ + public function getType() + { + return 'sort'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + if ($this->nestedFilter) { + $fieldValues = array_merge( + $this->params, + ['nested_filter' => [ + $this->nestedFilter->getType() => $this->nestedFilter->toArray(), + ] + ] + ); + } else { + $fieldValues = $this->params; + } + + $output = [ + $this->field => $fieldValues, + ]; + + return $output; + } +} diff --git a/src/Sort/GeoSort.php b/src/Sort/GeoSort.php deleted file mode 100644 index 9f4fd52..0000000 --- a/src/Sort/GeoSort.php +++ /dev/null @@ -1,110 +0,0 @@ -<?php - -/* - * This file is part of the ONGR package. - * - * (c) NFQ Technologies UAB <info@nfq.com> - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace ONGR\ElasticsearchDSL\Sort; - -/** - * A special type of sorting by distance. - */ -class GeoSort extends AbstractSort -{ - /** - * Possible types. - * - * Examples: - * [-70, 40] - * ["lat" : 40, "lon" : -70] - * "-70,40" - * - * @var string|array - */ - protected $location; - - /** - * @var string Units in which to measure distance. - */ - protected $unit; - - /** - * Constructor for geo sort. - * - * @param string $field Field name. - * @param array|string $location Possible types examples: - * [-70, 40] - * ["lat" : 40, "lon" : -70] - * "-70,40". - * @param string $order Order. - * @param string $unit Units for measuring the distance. - * @param string $mode Mode. - */ - public function __construct($field, $location, $order = self::ORDER_DESC, $unit = null, $mode = null) - { - $this->setLocation($location); - $this->setUnit($unit); - parent::__construct($field, $order, $mode); - } - - /** - * @param string $unit - */ - public function setUnit($unit) - { - $this->unit = $unit; - } - - /** - * @return string - */ - public function getUnit() - { - return $this->unit; - } - - /** - * @param array|string $location - */ - public function setLocation($location) - { - $this->location = $location; - } - - /** - * @return array|string - */ - public function getLocation() - { - return $this->location; - } - - /** - * @return string - */ - final public function getType() - { - return '_geo_distance'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $value = parent::toArray(); - - if ($this->getUnit() !== null) { - $value['unit'] = $this->getUnit(); - } - - $value[$this->getField()] = $this->getLocation(); - - return $value; - } -} diff --git a/src/Sort/ScriptSort.php b/src/Sort/ScriptSort.php deleted file mode 100644 index 9106dbf..0000000 --- a/src/Sort/ScriptSort.php +++ /dev/null @@ -1,136 +0,0 @@ -<?php - -/* - * This file is part of the ONGR package. - * - * (c) NFQ Technologies UAB <info@nfq.com> - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace ONGR\ElasticsearchDSL\Sort; - -/** - * Sort based on custom scripts. - * - * Note, it is recommended, for single custom based script based sorting, to use function_score query instead. - * Sorting based on score is faster. - */ -class ScriptSort extends AbstractSort -{ - /** - * @var string Script to execute. - */ - private $script; - - /** - * @var string Type returned (number, string). - */ - private $returnType; - - /** - * Associative array of custom params with values. - * - * Example: ['factor' => 1.5] - * - * @var array - */ - private $params; - - /** - * Initializes script sort. - * - * @param string $script - * @param string $returnType - * @param array $params - * @param string $order - */ - public function __construct($script, $returnType, $params = null, $order = self::ORDER_DESC) - { - if ($params) { - $this->setParams($params); - } - $this->setScript($script); - $this->setOrder($order); - $this->setReturnType($returnType); - } - - /** - * @return string - */ - public function getReturnType() - { - return $this->returnType; - } - - /** - * @param string $returnType - */ - public function setReturnType($returnType) - { - $this->returnType = $returnType; - } - - /** - * @return array - */ - public function getParams() - { - return $this->params; - } - - /** - * @param array $params - */ - public function setParams($params) - { - $this->params = $params; - } - - /** - * @return string - */ - public function getScript() - { - return $this->script; - } - - /** - * @param string $script - */ - public function setScript($script) - { - $this->script = $script; - } - - /** - * @return string - */ - final public function getType() - { - return '_script'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $value = []; - - if ($this->getOrder()) { - $value['order'] = $this->getOrder(); - } - - $value['script'] = $this->getScript(); - - if ($this->getParams()) { - $value['params'] = $this->getParams(); - } - - $value['type'] = $this->getReturnType(); - - return $value; - } -} diff --git a/src/Sort/Sort.php b/src/Sort/Sort.php deleted file mode 100644 index 50068bd..0000000 --- a/src/Sort/Sort.php +++ /dev/null @@ -1,80 +0,0 @@ -<?php - -/* - * This file is part of the ONGR package. - * - * (c) NFQ Technologies UAB <info@nfq.com> - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace ONGR\ElasticsearchDSL\Sort; - -use ONGR\ElasticsearchDSL\BuilderInterface; - -/** - * Holds all the values required for basic sorting. - */ -class Sort extends AbstractSort -{ - /** - * @var BuilderInterface Filter for sorting. - */ - private $nestedFilter; - - /** - * @param string $field Field name. - * @param string $order Order direction. - * @param BuilderInterface $nestedFilter Filter for sorting. - * @param string $mode Multi-valued field sorting mode [MODE_MIN, MODE_MAX, MODE_AVG, MODE_SUM]. - */ - public function __construct($field, $order = self::ORDER_ASC, BuilderInterface $nestedFilter = null, $mode = null) - { - parent::__construct($field, $order, $mode); - if ($nestedFilter) { - $this->setNestedFilter($nestedFilter); - } - } - - /** - * Sets nested filter. - * - * @param BuilderInterface $nestedFilter - */ - public function setNestedFilter($nestedFilter) - { - $this->nestedFilter = $nestedFilter; - } - - /** - * Returns nested filter. - * - * @return BuilderInterface - */ - public function getNestedFilter() - { - return $this->nestedFilter; - } - - /** - * @return string - */ - public function getType() - { - return $this->getField(); - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $value = parent::toArray(); - if ($this->getNestedFilter() !== null) { - $value['nested_filter'][$this->getNestedFilter()->getType()] = $this->getNestedFilter()->toArray(); - } - - return $value; - } -} diff --git a/src/Sort/Sorts.php b/src/Sort/Sorts.php deleted file mode 100644 index da1a9b0..0000000 --- a/src/Sort/Sorts.php +++ /dev/null @@ -1,64 +0,0 @@ -<?php - -/* - * This file is part of the ONGR package. - * - * (c) NFQ Technologies UAB <info@nfq.com> - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace ONGR\ElasticsearchDSL\Sort; - -use ONGR\ElasticsearchDSL\BuilderInterface; - -/** - * Container for sorts. - */ -class Sorts implements BuilderInterface -{ - /** - * @var AbstractSort[] Sorts collection. - */ - private $sorts = []; - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'sort'; - } - - /** - * @param AbstractSort $sort - */ - public function addSort(AbstractSort $sort) - { - $this->sorts[$sort->getType()] = $sort; - } - - /** - * Check if we have any sorting set. - * - * @return bool - */ - public function isRelevant() - { - return !empty($this->sorts); - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $value = []; - foreach ($this->sorts as $sort) { - $value[$sort->getType()] = $sort->toArray(); - } - - return $value; - } -} -- GitLab