Skip to content

Commit

Permalink
Add distance feature query (#1730)
Browse files Browse the repository at this point in the history
  • Loading branch information
deguif authored and ruflin committed Dec 16, 2019
1 parent f0d5872 commit 288e554
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ All notable changes to this project will be documented in this file based on the

### Added

* Added `\Elastica\Query\DistanceFeature` [#1730](https://github.com/ruflin/Elastica/pull/1730)

### Improvements

### Deprecated
Expand Down
41 changes: 41 additions & 0 deletions lib/Elastica/Query/DistanceFeature.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Elastica\Query;

/**
* Distance feature query.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-distance-feature-query.html
*/
class DistanceFeature extends AbstractQuery
{
public function __construct(string $field, $origin, string $pivot)
{
$this->setField($field);
$this->setOrigin($origin);
$this->setPivot($pivot);
}

public function setField(string $field): self
{
return $this->setParam('field', $field);
}

/**
* @param string|array $origin
*/
public function setOrigin($origin): self
{
return $this->setParam('origin', $origin);
}

public function setPivot(string $pivot): self
{
return $this->setParam('pivot', $pivot);
}

public function setBoost(float $boost = 1.0): self
{
return $this->setParam('boost', $boost);
}
}
13 changes: 13 additions & 0 deletions lib/Elastica/QueryBuilder/DSL/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Elastica\Query\Common;
use Elastica\Query\ConstantScore;
use Elastica\Query\DisMax;
use Elastica\Query\DistanceFeature;
use Elastica\Query\Exists;
use Elastica\Query\FunctionScore;
use Elastica\Query\Fuzzy;
Expand Down Expand Up @@ -139,6 +140,18 @@ public function dis_max(): DisMax
return new DisMax();
}

/**
* distance feature query.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-distance-feature-query.html
*
* @param string|array $origin
*/
public function distance_feature(string $field, $origin, string $pivot): DistanceFeature
{
return new DistanceFeature($field, $origin, $pivot);
}

/**
* function score query.
*
Expand Down
1 change: 1 addition & 0 deletions lib/Elastica/QueryBuilder/Version/Version700.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Version700 extends Version
'common_terms',
'constant_score',
'dis_max',
'distance_feature',
'function_score',
'fuzzy',
'geo_bounding_box',
Expand Down
56 changes: 56 additions & 0 deletions test/Elastica/Query/DistanceFeatureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Elastica\Test\Query;

use Elastica\Query\DistanceFeature;
use Elastica\Test\Base as BaseTest;

class DistanceFeatureTest extends BaseTest
{
/**
* @group unit
*/
public function testToArrayDate()
{
$query = new DistanceFeature('field_date', 'now', '7d');

$expectedArray = [
'distance_feature' => [
'field' => 'field_date',
'origin' => 'now',
'pivot' => '7d',
],
];

$this->assertEquals($expectedArray, $query->toArray());
}

/**
* @group unit
*/
public function testToArrayGeoPoint()
{
$query = new DistanceFeature('field_geo_point', [-71.3, 41.15], '1000m');

$expectedArray = [
'distance_feature' => [
'field' => 'field_geo_point',
'origin' => [-71.3, 41.15],
'pivot' => '1000m',
],
];

$this->assertEquals($expectedArray, $query->toArray());
}

/**
* @group unit
*/
public function testSetBoost()
{
$query = new DistanceFeature('field_date', 'now', '7d');
$query->setBoost($value = 2.0);

$this->assertEquals($value, $query->toArray()['distance_feature']['boost']);
}
}
1 change: 1 addition & 0 deletions test/Elastica/QueryBuilder/DSL/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function testInterface()
$this->_assertImplemented($queryDSL, 'boosting', Query\Boosting::class, []);
$this->_assertImplemented($queryDSL, 'common_terms', Query\Common::class, ['field', 'query', 0.001]);
$this->_assertImplemented($queryDSL, 'dis_max', Query\DisMax::class, []);
$this->_assertImplemented($queryDSL, 'distance_feature', Query\DistanceFeature::class, ['field', 'now', '7d']);
$this->_assertImplemented($queryDSL, 'function_score', Query\FunctionScore::class, []);
$this->_assertImplemented($queryDSL, 'fuzzy', Query\Fuzzy::class, ['field', 'type']);
$this->_assertImplemented($queryDSL, 'has_child', Query\HasChild::class, [new Match()]);
Expand Down

0 comments on commit 288e554

Please sign in to comment.