Skip to content

Commit

Permalink
add geoLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmed Sallam committed Nov 20, 2018
1 parent 98062ad commit de50a8f
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Builders/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Elastica\Query\Range;
use Elastica\Query\Term;
use Elastica\Query\Terms;
use Elastica\Query\GeoDistance;

class QueryBuilder implements SearchInRangeContract, SearchContract
{
Expand Down Expand Up @@ -76,6 +77,9 @@ class QueryBuilder implements SearchInRangeContract, SearchContract
/**@var array $mismatch */
protected $mismatch = [];

/**@var array $geoDistance */
protected $geoDistance = [];

/**
* @var array $simpleQueryString
*/
Expand Down Expand Up @@ -247,6 +251,18 @@ public function mismatch($attribute, $keyword)
return $this;
}

/**
* Geo distance
* @param $key
* @param $location
* @param $distance
* @return $this
*/
public function geoDistance($key, $location, $distance)
{
$this->geoDistance[] = [$key, $location, $distance];
return $this;
}
/**
* SimpleQueryString to Field
* @param $attribute
Expand Down Expand Up @@ -291,6 +307,7 @@ public function resetBuilder()
$this->whereOr = [];
$this->match = [];
$this->mismatch = [];
$this->geoDistance = [];
$this->simpleQueryString = [];
$this->queryString = [];
$this->query = new BoolQuery();
Expand Down Expand Up @@ -362,6 +379,11 @@ public function prepareQuery()
$this->prepareMismatchQueries($mismatch);
}

// add geoDistance queries
foreach ($this->geoDistance as $geoDistance) {
$this->prepareGeoDistanceQueries($geoDistance);
}

// add SimpleQueryString
foreach ($this->simpleQueryString as $query) {
$this->prepareSimpleQueryString($query);
Expand Down Expand Up @@ -509,6 +531,17 @@ private function prepareMismatchQueries($mismatch)
$this->filter->addMustNot($mismatcher);
}

/**
* prepare geoDistance query
* @param $geoDistance
*/
private function prepareGeoDistanceQueries($geoDistance)
{
list($key, $location, $distance) = array_pad($geoDistance, 3, null);
$geoDistance = new GeoDistance($key, $location, $distance);
$this->filter->addFilter($geoDistance);
}

/**
* prepare Simple Query String
* @param $query
Expand Down

0 comments on commit de50a8f

Please sign in to comment.