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

Add GeoDistance filter on ES 2.3 #24

Merged
merged 4 commits into from
Nov 30, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,27 @@ const LIMIT = 10;
$queryBuilder = QueryBuilder::createNew(0, 10, 0.3);
```

#### GeoDistance query
Add property on the index :
```json
{
//...
"_source": {
//...
"field_name" : {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't it need the type ? or is it how it looks in ES ?

Copy link
Author

@napley napley Nov 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is how it looks in ES

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this is the serialized value of the indexed data.

While this information is relevant, serialization is already done by the ObjectIndexer. Documentation could eventually us an example of the data before serialization.

I agree with @CheapHasz regarding the data type. My understanding is that this code snippet is the indexed data but mapping is also important.

In the end, I think GeoDistance querying is important enough to have a dedicated documentation page like it has been done for aggregations : https://github.com/novaway/elasticsearch-client/blob/master/doc/aggregation.md

"lat" : 40.12,
"lon" : -71.34
}
//...
}
}
```

Add `Filter` in the `QueryBuilder` :
```php
$queryBuilder->addFilter(new GeoDistanceFilter('field_name', 'latitude_value', 'longitude_value', 'nb_kilometer'));
```

> + **TODO** : Use a result formater
+ **TODO** : Filtering results

Expand Down Expand Up @@ -132,6 +153,8 @@ parameters:
analyzer: standard
age:
type: integer
location:
type: geo_point

services:
myapp.search.index:
Expand Down
59 changes: 59 additions & 0 deletions src/Filter/GeoDistanceFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Novaway\ElasticsearchClient\Filter;

use Novaway\ElasticsearchClient\Query\CombiningFactor;

class GeoDistanceFilter implements Filter
{
/** @var string */
private $property;
/** @var float */
private $latitude;
/** @var float */
private $longitude;
/** @var int */
private $distance;
/** @var string */
private $combiningFactor;

/**
* GeoDistanceFilter constructor.
* @param string $property
* @param float $latitude
* @param float $longitude
* @param int $distance
*/
public function __construct(string $property, float $latitude, float $longitude, int $distance, string $combiningFactor = CombiningFactor::FILTER)
{
$this->property = $property;
$this->latitude = $latitude;
$this->longitude = $longitude;
$this->distance = $distance;
$this->combiningFactor = $combiningFactor;
}

/**
* @return string
*/
public function getCombiningFactor(): string
{
return $this->combiningFactor;
}

/**
* @inheritdoc
*/
public function formatForQuery(): array
{
return [
'geo_distance' => [
'distance' => sprintf('%dkm', $this->distance),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be nice to avoid a hardcoded unit (maybe using a default value), because not everyone uses the metric system, and search can be done on narrower scopes.

$this->property => [
'lat' => $this->latitude,
'lon' => $this->longitude
]
]
];
}
}