Skip to content
This repository has been archived by the owner on Aug 13, 2019. It is now read-only.

Commit

Permalink
Merge d1d8fa0 into 6e23452
Browse files Browse the repository at this point in the history
  • Loading branch information
bertramakers committed May 7, 2019
2 parents 6e23452 + d1d8fa0 commit 825b9e0
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Offer/ElasticSearchOfferQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use CultuurNet\UDB3\Search\Creator;
use CultuurNet\UDB3\Search\ElasticSearch\AbstractElasticSearchQueryBuilder;
use CultuurNet\UDB3\Search\ElasticSearch\KnownLanguages;
use CultuurNet\UDB3\Search\GeoBoundsParameters;
use CultuurNet\UDB3\Search\GeoDistanceParameters;
use CultuurNet\UDB3\Search\Offer\AudienceType;
use CultuurNet\UDB3\Search\Offer\CalendarType;
Expand All @@ -23,6 +24,7 @@
use CultuurNet\UDB3\Search\SortOrder;
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\TermsAggregation;
use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery;
use ONGR\ElasticsearchDSL\Query\Geo\GeoBoundingBoxQuery;
use ONGR\ElasticsearchDSL\Query\Geo\GeoDistanceQuery;
use ONGR\ElasticsearchDSL\Query\Geo\GeoShapeQuery;
use ValueObjects\Geography\Country;
Expand Down Expand Up @@ -265,6 +267,31 @@ public function withGeoDistanceFilter(GeoDistanceParameters $geoDistanceParamete
return $c;
}

/**
* @inheritdoc
*/
public function withGeoBoundsFilter(GeoBoundsParameters $geoBoundsParameters)
{
$northWest = $geoBoundsParameters->getNorthWestCoordinates();
$southEast = $geoBoundsParameters->getSouthEastCoordinates();

$topLeft = [
'lat' => $northWest->getLatitude()->toDouble(),
'lon' => $northWest->getLongitude()->toDouble(),
];

$bottomRight = [
'lat' => $southEast->getLatitude()->toDouble(),
'lon' => $southEast->getLongitude()->toDouble(),
];

$geoBoundingBoxQuery = new GeoBoundingBoxQuery('geo_point', [$topLeft, $bottomRight]);

$c = $this->getClone();
$c->boolQuery->add($geoBoundingBoxQuery, BoolQuery::FILTER);
return $c;
}

/**
* @inheritdoc
*/
Expand Down
58 changes: 58 additions & 0 deletions tests/Offer/ElasticSearchOfferQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use CultuurNet\UDB3\Search\ElasticSearch\AbstractElasticSearchQueryBuilderTest;
use CultuurNet\UDB3\Search\ElasticSearch\ElasticSearchDistance;
use CultuurNet\UDB3\Search\ElasticSearch\LuceneQueryString;
use CultuurNet\UDB3\Search\GeoBoundsParameters;
use CultuurNet\UDB3\Search\GeoDistanceParameters;
use CultuurNet\UDB3\Search\Offer\AudienceType;
use CultuurNet\UDB3\Search\Offer\CalendarType;
Expand Down Expand Up @@ -957,6 +958,63 @@ public function it_should_build_a_query_with_a_geodistance_filter()
$this->assertEquals($expectedQueryArray, $actualQueryArray);
}

/**
* @test
*/
public function it_should_build_a_query_with_a_geo_bounds_filter()
{
/* @var ElasticSearchOfferQueryBuilder $builder */
$builder = (new ElasticSearchOfferQueryBuilder())
->withStart(new Natural(30))
->withLimit(new Natural(10))
->withGeoBoundsFilter(
new GeoBoundsParameters(
new Coordinates(
new Latitude(40.73),
new Longitude(-71.12)
),
new Coordinates(
new Latitude(40.01),
new Longitude(-74.1)
)
)
);

$expectedQueryArray = [
'from' => 30,
'size' => 10,
'query' => [
'bool' => [
'must' => [
[
'match_all' => (object) [],
],
],
'filter' => [
[
'geo_bounding_box' => [
'geo_point' => [
"top_left" => [
'lat' => 40.73,
'lon' => -74.1,
],
"bottom_right" => [
'lat' => 40.01,
'lon' => -71.12,
],
],
],
],
],
],
],
];

$actualQueryArray = $builder->build()->toArray();

$this->assertEquals($expectedQueryArray, $actualQueryArray);
}

/**
* @test
*/
Expand Down

0 comments on commit 825b9e0

Please sign in to comment.