Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to sort by geo_distance #322

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 200 additions & 0 deletions src/Sort/GeoDistanceSort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <[email protected]>
*
* 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;
use ONGR\ElasticsearchDSL\ParametersTrait;

/**
* Holds all the values required for basic sorting.
*/
class GeoDistanceSort implements BuilderInterface
{
use ParametersTrait;

const ASC = 'asc';
const DESC = 'desc';

const ARC = 'arc';
const PLANE = 'plane';

/**
* @var string
*/
private $field;

/**
* @var string
*/
private $order;

/**
* @var string
*/
private $distanceType;

/**
* @var string
*/
private $location;

/**
* @var BuilderInterface
*/
private $nestedFilter;

/**
* @param string $field Field name.
* @param string|array $location The location to measure the distance from. The possible representations can be found here: https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html
* @param string $order Order direction.
* @param array $params Params that can be set to field sort.
*/
public function __construct($field, $location, $order = null, $params = [])
{
$this->field = $field;
$this->order = $order;
$this->location = $location;
$this->setParameters($params);
}

/**
* @return string
*/
public function getField()
{
return $this->field;
}

/**
* @param string $field
*
* @return $this
*/
public function setField($field)
{
$this->field = $field;

return $this;
}

/**
* @return string
*/
public function getOrder()
{
return $this->order;
}

/**
* @param string $order
*
* @return $this
*/
public function setOrder($order)
{
$this->order = $order;

return $this;
}

/**
* @return BuilderInterface
*/
public function getNestedFilter()
{
return $this->nestedFilter;
}

/**
* @param BuilderInterface $nestedFilter
*
* @return $this
*/
public function setNestedFilter(BuilderInterface $nestedFilter)
{
$this->nestedFilter = $nestedFilter;

return $this;
}

/**
* @return string
*/
public function getDistanceType()
{
return $this->distanceType;
}

/**
* @param string $distanceType
* @return GeoSort
*/
public function setDistanceType($distanceType)
{
$this->distanceType = $distanceType;

return $this;
}

/**
* @return string
*/
public function getLocation()
{
return $this->location;
}

/**
* @param string $location
* @return GeoSort
*/
public function setLocation($location)
{
$this->location = $location;

return $this;
}

/**
* Returns element type.
*
* @return string
*/
public function getType()
{
return 'sort';
}

/**
* {@inheritdoc}
*/
public function toArray()
{
if ($this->field) {
$this->addParameter($this->field, $this->location);
}

if ($this->order) {
$this->addParameter('order', $this->order);
}

if ($this->nestedFilter) {
$this->addParameter('nested', $this->nestedFilter->toArray());
}

$output = [
'_geo_distance' => !$this->getParameters() ? new \stdClass() : $this->getParameters(),
];

return $output;
}
}