-
Notifications
You must be signed in to change notification settings - Fork 730
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters