Skip to content
Snippets Groups Projects
Commit 8e6b7f67 authored by Simonas Šerlinskas's avatar Simonas Šerlinskas
Browse files

simplified sorting query builder

parent eb1c2ef7
No related branches found
No related tags found
No related merge requests found
<?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;
}
}
...@@ -16,40 +16,37 @@ use ONGR\ElasticsearchDSL\BuilderInterface; ...@@ -16,40 +16,37 @@ use ONGR\ElasticsearchDSL\BuilderInterface;
/** /**
* Holds all the values required for basic sorting. * Holds all the values required for basic sorting.
*/ */
class Sort extends AbstractSort class FieldSort implements BuilderInterface
{ {
CONST ASC = 'asc';
CONST DESC = 'desc';
/** /**
* @var BuilderInterface Filter for sorting. * @var string.
*/ */
private $nestedFilter; private $field;
/** /**
* @param string $field Field name. * @var array
* @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) private $params;
{
parent::__construct($field, $order, $mode);
if ($nestedFilter) {
$this->setNestedFilter($nestedFilter);
}
}
/** /**
* Sets nested filter. * @var BuilderInterface
* */
* @param BuilderInterface $nestedFilter private $nestedFilter;
/**
* @param string $field Field name.
* @param array $params Params that can be set to field sort.
*/ */
public function setNestedFilter($nestedFilter) public function __construct($field, $params = [])
{ {
$this->nestedFilter = $nestedFilter; $this->field = $field;
$this->params = $params;
} }
/** /**
* Returns nested filter.
*
* @return BuilderInterface * @return BuilderInterface
*/ */
public function getNestedFilter() public function getNestedFilter()
...@@ -58,11 +55,21 @@ class Sort extends AbstractSort ...@@ -58,11 +55,21 @@ class Sort extends AbstractSort
} }
/** /**
* @param BuilderInterface $nestedFilter
*/
public function setNestedFilter(BuilderInterface $nestedFilter)
{
$this->nestedFilter = $nestedFilter;
}
/**
* Returns element type.
*
* @return string * @return string
*/ */
public function getType() public function getType()
{ {
return $this->getField(); return 'sort';
} }
/** /**
...@@ -70,11 +77,22 @@ class Sort extends AbstractSort ...@@ -70,11 +77,22 @@ class Sort extends AbstractSort
*/ */
public function toArray() public function toArray()
{ {
$value = parent::toArray(); if ($this->nestedFilter) {
if ($this->getNestedFilter() !== null) { $fieldValues = array_merge(
$value['nested_filter'][$this->getNestedFilter()->getType()] = $this->getNestedFilter()->toArray(); $this->params,
['nested_filter' => [
$this->nestedFilter->getType() => $this->nestedFilter->toArray(),
]
]
);
} else {
$fieldValues = $this->params;
} }
return $value; $output = [
$this->field => $fieldValues,
];
return $output;
} }
} }
<?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;
}
}
<?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;
}
}
<?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;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment