Skip to content
This repository was archived by the owner on Nov 4, 2021. It is now read-only.

Commit 6ab5b05

Browse files
committed
Merge branch 'master' into dev
2 parents 71298a0 + edbe9ea commit 6ab5b05

File tree

3 files changed

+198
-4
lines changed

3 files changed

+198
-4
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,9 @@ whereNotBetween($field, $value) | whereNotBetween('price', [100, 200]) | Checks
398398
whereExists($field) | whereExists('unemployed') | Checks if a value is defined.
399399
whereNotExists($field) | whereNotExists('unemployed') | Checks if a value isn't defined.
400400
whereRegexp($field, $value, $flags = 'ALL') | whereRegexp('name.raw', 'A.+') | Filters records according to a given regular expression. [Here](https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-regexp-query.html#regexp-syntax) you can find more about syntax.
401+
whereGeoDistance($field, $value, $distance) | whereGeoDistance('location', [-70, 40], '1000m') | Filters records according to given point and distance from it. [Here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html) you can find more about syntax.
402+
whereGeoBoundingBox($field, array $value) | whereGeoBoundingBox('location', ['top_left' => [-74.1, 40.73], 'bottom_right' => [-71.12, 40.01]]) | Filters records within given boundings. [Here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-query.html) you can find more about syntax.
403+
whereGeoPolygon($field, array $points) | whereGeoPolygon('location', [[-70, 40],[-80, 30],[-90, 20]]) | Filters records within given polygon. [Here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-polygon-query.html) you can find more about syntax.
401404

402405
In most cases it's better to use raw fields to filter records, i.e. not analyzed fields.
403406

src/Builders/FilterBuilder.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,49 @@ public function when($value, callable $callback, callable $default = null)
125125
return $this;
126126
}
127127

128+
/**
129+
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html
130+
*
131+
* @param string $field
132+
* @param string|array $value
133+
* @param int|string $distance
134+
* @return $this
135+
*/
136+
public function whereGeoDistance($field, $value, $distance)
137+
{
138+
$this->wheres['must'][] = ['geo_distance' => ['distance' => $distance, $field => $value]];
139+
140+
return $this;
141+
}
142+
143+
/**
144+
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-query.html
145+
*
146+
* @param string $field
147+
* @param array $value
148+
* @return $this
149+
*/
150+
public function whereGeoBoundingBox($field, array $value)
151+
{
152+
$this->wheres['must'][] = ['geo_bounding_box' => [$field => $value]];
153+
154+
return $this;
155+
}
156+
157+
/**
158+
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-polygon-query.html
159+
*
160+
* @param string $field
161+
* @param array $points
162+
* @return $this
163+
*/
164+
public function whereGeoPolygon($field, array $points)
165+
{
166+
$this->wheres['must'][] = ['geo_polygon' => [$field => ['points' => $points]]];
167+
168+
return $this;
169+
}
170+
128171
public function orderBy($column, $direction = 'asc')
129172
{
130173
$this->orders[] = [$column => strtolower($direction) == 'asc' ? 'asc' : 'desc'];

tests/ElasticEngineTest.php

Lines changed: 152 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace ScoutElastic\Tests;
44

5-
use Mockery;
65
use Illuminate\Database\Eloquent\Collection;
6+
use Mockery;
77
use ScoutElastic\Builders\FilterBuilder;
88
use ScoutElastic\Builders\SearchBuilder;
99
use ScoutElastic\ElasticEngine;
@@ -606,6 +606,156 @@ public function test_if_the_search_method_with_specified_whereRegexp_clause_buil
606606
$this->addToAssertionCount(1);
607607
}
608608

609+
public function test_if_the_search_method_with_specified_whereGeoDistance_clause_builds_correct_payload()
610+
{
611+
$this->mockClient()
612+
->shouldReceive('search')
613+
->with([
614+
'index' => 'test_index',
615+
'type' => 'test_table',
616+
'body' => [
617+
'query' => [
618+
'bool' => [
619+
'must' => [
620+
'match' => [
621+
'_all' => 'flat'
622+
]
623+
],
624+
'filter' => [
625+
'bool' => [
626+
'must' => [
627+
[
628+
'geo_distance' => [
629+
'distance' => 1000,
630+
'location' => [-70, 40]
631+
]
632+
]
633+
]
634+
]
635+
]
636+
]
637+
]
638+
]
639+
]);
640+
641+
$model = $this->mockModel();
642+
643+
$builder = (new SearchBuilder($model, 'flat'))->whereGeoDistance(
644+
'location',
645+
[-70, 40],
646+
1000
647+
);
648+
649+
$this->buildEngine()
650+
->search($builder);
651+
652+
$this->addToAssertionCount(1);
653+
}
654+
655+
public function test_if_the_search_method_with_specified_whereGeoBoundingBox_clause_builds_correct_payload()
656+
{
657+
$this->mockClient()
658+
->shouldReceive('search')
659+
->with([
660+
'index' => 'test_index',
661+
'type' => 'test_table',
662+
'body' => [
663+
'query' => [
664+
'bool' => [
665+
'must' => [
666+
'match' => [
667+
'_all' => 'flat'
668+
]
669+
],
670+
'filter' => [
671+
'bool' => [
672+
'must' => [
673+
[
674+
'geo_bounding_box' => [
675+
'location' => [
676+
'top_left' => [-74.1, 40.73],
677+
'bottom_right' => [-71.12, 40.01]
678+
]
679+
]
680+
]
681+
]
682+
]
683+
]
684+
]
685+
]
686+
]
687+
]);
688+
689+
$model = $this->mockModel();
690+
691+
$builder = (new SearchBuilder($model, 'flat'))->whereGeoBoundingBox(
692+
'location',
693+
[
694+
'top_left' => [-74.1, 40.73],
695+
'bottom_right' => [-71.12, 40.01]
696+
]
697+
);
698+
699+
$this->buildEngine()
700+
->search($builder);
701+
702+
$this->addToAssertionCount(1);
703+
}
704+
705+
public function test_if_the_search_method_with_specified_whereGeoPolygon_clause_builds_correct_payload()
706+
{
707+
$this->mockClient()
708+
->shouldReceive('search')
709+
->with([
710+
'index' => 'test_index',
711+
'type' => 'test_table',
712+
'body' => [
713+
'query' => [
714+
'bool' => [
715+
'must' => [
716+
'match' => [
717+
'_all' => 'flat'
718+
]
719+
],
720+
'filter' => [
721+
'bool' => [
722+
'must' => [
723+
[
724+
'geo_polygon' => [
725+
'location' => [
726+
'points' => [
727+
[-70, 40],
728+
[-80, 30],
729+
[-90, 20]
730+
]
731+
]
732+
]
733+
]
734+
]
735+
]
736+
]
737+
]
738+
]
739+
]
740+
]);
741+
742+
$model = $this->mockModel();
743+
744+
$builder = (new SearchBuilder($model, 'flat'))->whereGeoPolygon(
745+
'location',
746+
[
747+
[-70, 40],
748+
[-80, 30],
749+
[-90, 20]
750+
]
751+
);
752+
753+
$this->buildEngine()
754+
->search($builder);
755+
756+
$this->addToAssertionCount(1);
757+
}
758+
609759
public function test_if_the_search_method_with_specified_rule_builds_correct_payload()
610760
{
611761
$this->mockClient()
@@ -628,7 +778,7 @@ public function test_if_the_search_method_with_specified_rule_builds_correct_pay
628778

629779
$model = $this->mockModel();
630780

631-
$builder = (new SearchBuilder($model, 'John'))->rule(function($builder) {
781+
$builder = (new SearchBuilder($model, 'John'))->rule(function ($builder) {
632782
return [
633783
'must' => [
634784
'match' => [
@@ -803,12 +953,10 @@ public function test_if_the_map_method_returns_the_same_results_from_database_as
803953
$searchResults = $this->getElasticSearchResponse();
804954

805955
$model = $this->mockModel()
806-
807956
->shouldReceive('whereIn')
808957
->with('id', [1, 3])
809958
->andReturnSelf()
810959
->getMock()
811-
812960
->shouldReceive('get')
813961
->andReturn(Collection::make([
814962
$this->mockModel(['id' => 1]),

0 commit comments

Comments
 (0)