Skip to content

Commit

Permalink
Add geo bounding box query
Browse files Browse the repository at this point in the history
  • Loading branch information
pionl committed Apr 5, 2022
1 parent 7b9f47f commit 5a7032f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Entities/GpsPointEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Erichard\ElasticQueryBuilder\Entities;

class GpsPointEntity
{
public function __construct(
public float $lat,
public float $lon
) {
}
}
45 changes: 45 additions & 0 deletions src/Query/GeoBoundingBoxQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Erichard\ElasticQueryBuilder\Query;

use Erichard\ElasticQueryBuilder\Contracts\QueryInterface;
use Erichard\ElasticQueryBuilder\Entities\GpsPointEntity;
use Erichard\ElasticQueryBuilder\Features\HasField;

/**
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-query.html
*/
class GeoBoundingBoxQuery implements QueryInterface
{
use HasField;

public function __construct(
string $field,
private GpsPointEntity $topLeft,
private GpsPointEntity $bottomRight
) {
$this->field = $field;
}

public function build(): array
{
return [
'geo_bounding_box' => [
$this->field => [
'top_left' => $this->pointToArray($this->topLeft),
'bottom_right' => $this->pointToArray($this->bottomRight),
],
],
];
}

protected function pointToArray(GpsPointEntity $entity): array
{
return [
'lat' => $entity->lat,
'lon' => $entity->lon,
];
}
}

0 comments on commit 5a7032f

Please sign in to comment.