Skip to content

Commit dbc29f7

Browse files
Merge pull request #322 from daniellienert/feature/geodistance
FEATURE: Add geoDistance operation
2 parents 6672768 + 2e01081 commit dbc29f7

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

Classes/Eel/ElasticSearchQueryBuilder.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,16 +688,35 @@ public function fulltext(string $searchWord, array $options = []): QueryBuilderI
688688
*
689689
* @param string $propertyName
690690
* @param string $prefix
691+
* @param string $clauseType one of must, should, must_not
691692
* @return $this|QueryBuilderInterface
692693
* @throws QueryBuildingException
693694
*/
694-
public function prefix(string $propertyName, string $prefix): QueryBuilderInterface
695+
public function prefix(string $propertyName, string $prefix, string $clauseType = 'must'): QueryBuilderInterface
695696
{
696-
$this->request->queryFilter('prefix', [$propertyName => $prefix]);
697+
$this->request->queryFilter('prefix', [$propertyName => $prefix], $clauseType);
697698

698699
return $this;
699700
}
700701

702+
/**
703+
* Filters documents that include only hits that exists within a specific distance from a geo point.
704+
*
705+
* @param string $propertyName
706+
* @param string|array $geoPoint Either ['lon' => x.x, 'lat' => y.y], [lon, lat], 'lat,lon', or GeoHash
707+
* @param string $distance Distance with unit. See: https://www.elastic.co/guide/en/elasticsearch/reference/7.6/common-options.html#distance-units
708+
* @param string $clauseType one of must, should, must_not
709+
* @return QueryBuilderInterface
710+
* @throws QueryBuildingException
711+
*/
712+
public function geoDistance(string $propertyName, $geoPoint, string $distance, string $clauseType = 'must'): QueryBuilderInterface
713+
{
714+
$this->queryFilter('geo_distance', [
715+
'distance' => $distance,
716+
$propertyName => $geoPoint,
717+
], $clauseType);
718+
}
719+
701720
/**
702721
* Configure Result Highlighting. Only makes sense in combination with fulltext(). By default, highlighting is enabled.
703722
* It can be disabled by calling "highlight(FALSE)".

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,11 @@ As **value**, the following methods accept a simple type, a node object or a Dat
417417
|`greaterThanOrEqual('propertyName', value, [clauseType])`|Range filter with property values greater than or equal to the given value|
418418
|`lessThan('propertyName', value, [clauseType])` |Range filter with property values less than the given value|
419419
|`lessThanOrEqual('propertyName', value, [clauseType])`|Range filter with property values less than or equal to the given value|
420-
|`sortAsc('propertyName')` / `sortDesc('propertyName')`|Can also be used multiple times, e.g. `sortAsc('tag').sortDesc(`date')` will first sort by tag ascending, and then by date descending.|
420+
|`sortAsc('propertyName')` / `sortDesc('propertyName')`|Can also be used multiple times, e.g. `sortAsc('tag').sortDesc('date')` will first sort by tag ascending, and then by date descending.|
421421
|`limit(5)` |Only return five results. If not specified, the default limit by Elasticsearch applies (which is at 10 by default)|
422422
|`from(5)` |Return the results starting from the 6th one|
423-
|`prefix('propertyName', 'prefix')` |Adds a prefix filter on the given field with the given prefix|
423+
|`prefix('propertyName', 'prefix', [clauseType])` |Adds a prefix filter on the given field with the given prefix|
424+
|`geoDistance(propertyName, geoPoint, distance, [clauseType])` |Filters documents that include only hits that exists within a specific distance from a geo point.|
424425
|`fulltext('searchWord', options)` |Does a query_string query on the Fulltext index using the searchword and additional [options](https://www.elastic.co/guide/en/elasticsearch/reference/7.6/query-dsl-query-string-query.html) to the query_string|
425426

426427
## moreLikeThis(like, fields, options)

0 commit comments

Comments
 (0)