-
Notifications
You must be signed in to change notification settings - Fork 20
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
Erwan Richard
committed
Feb 7, 2018
1 parent
263d000
commit 3e5e267
Showing
16 changed files
with
718 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
ElasticSearch Query Builder | ||
=========================== | ||
|
||
This is a PHP library which helps you build query for an ElasticSearch client by using a fluent interface. | ||
|
||
Installation | ||
------------ | ||
|
||
``` | ||
$ composer require erichard/elasticsearch-query-builder | ||
``` | ||
|
||
Usage | ||
----- | ||
|
||
``` | ||
use Erichard\ElasticQueryBuilder\QueryBuilder; | ||
use Erichard\ElasticQueryBuilder\Aggregation\Aggregation; | ||
use Erichard\ElasticQueryBuilder\Filter\Filter; | ||
$qb = new QueryBuilder(); | ||
$qb | ||
->setType('my_type') | ||
->setIndex('app') | ||
->setSize(10) | ||
; | ||
// Add an aggregation | ||
$qb->addAggregation(Aggregation::terms('agg_name')->setField('my_field')); | ||
// Add a filter | ||
$boolFilter = Filter::bool(); | ||
$boolFilter->addFilter(Filter::terms()->setField('field')->setValue($value)); | ||
$qb->addFilter($boolFilter); | ||
// I am using a client from elasticsearch/elasticsearch here | ||
$results = $client->search($qb->getQuery()); | ||
``` |
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,18 @@ | ||
{ | ||
"name": "erichard/elasticsearch-query-builder", | ||
"license": "MIT", | ||
"type": "library", | ||
"description": "Create elastic search query with a fluent interface", | ||
"authors": [ | ||
{ | ||
"name": "Erwan Richard", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { "Erichard\\ElasticQueryBuilder\\": "src/" } | ||
}, | ||
"require": { | ||
"php": ">=7.0" | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
namespace Erichard\ElasticQueryBuilder\Aggregation; | ||
|
||
abstract class Aggregation | ||
{ | ||
private $name; | ||
|
||
public function __construct($name = null) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
abstract public function build(): array; | ||
|
||
public static function terms($name = null) | ||
{ | ||
return new TermsAggregation($name); | ||
} | ||
|
||
public static function nested($name = null) | ||
{ | ||
return new NestedAggregation($name); | ||
} | ||
|
||
public static function filter($name = null) | ||
{ | ||
return new FilterAggregation($name); | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
namespace Erichard\ElasticQueryBuilder\Aggregation; | ||
|
||
use Erichard\ElasticQueryBuilder\Filter\Filter; | ||
|
||
class FilterAggregation extends Aggregation | ||
{ | ||
private $aggregation; | ||
private $filter; | ||
|
||
public function setFilter(Filter $filter) | ||
{ | ||
$this->filter = $filter; | ||
|
||
return $this; | ||
} | ||
|
||
public function setAggregation(Aggregation $aggregation) | ||
{ | ||
$this->aggregation = $aggregation; | ||
|
||
return $this; | ||
} | ||
|
||
public function build(): array | ||
{ | ||
return [ | ||
'filter' => $this->filter->build(), | ||
'aggs' => [ | ||
$this->aggregation->getName() => $this->aggregation->build(), | ||
], | ||
]; | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
namespace Erichard\ElasticQueryBuilder\Aggregation; | ||
|
||
class NestedAggregation extends Aggregation | ||
{ | ||
private $aggregation; | ||
private $path; | ||
|
||
public function setNestedPath(string $path) | ||
{ | ||
$this->path = $path; | ||
|
||
return $this; | ||
} | ||
|
||
public function setAggregation(Aggregation $aggregation) | ||
{ | ||
$this->aggregation = $aggregation; | ||
|
||
return $this; | ||
} | ||
|
||
public function build(): array | ||
{ | ||
return [ | ||
'nested' => [ | ||
'path' => $this->path, | ||
], | ||
'aggs' => [ | ||
$this->aggregation->getName() => $this->aggregation->build(), | ||
], | ||
]; | ||
} | ||
} |
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,52 @@ | ||
<?php | ||
|
||
namespace Erichard\ElasticQueryBuilder\Aggregation; | ||
|
||
class TermsAggregation extends Aggregation | ||
{ | ||
private $field; | ||
private $script; | ||
private $size = 10; | ||
|
||
public function setField(string $field) | ||
{ | ||
$this->field = $field; | ||
|
||
return $this; | ||
} | ||
|
||
public function setSize(int $size) | ||
{ | ||
$this->size = $size; | ||
|
||
return $this; | ||
} | ||
|
||
public function setScript(string $script) | ||
{ | ||
$this->script = $script; | ||
|
||
return $this; | ||
} | ||
|
||
public function build(): array | ||
{ | ||
if (null !== $this->script) { | ||
$term = [ | ||
'script' => [ | ||
'inline' => $this->script, | ||
'lang' => 'painless', | ||
], | ||
]; | ||
} else { | ||
$term = [ | ||
'field' => $this->field, | ||
'size' => $this->size, | ||
]; | ||
} | ||
|
||
return [ | ||
'terms' => $term, | ||
]; | ||
} | ||
} |
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,91 @@ | ||
<?php | ||
|
||
namespace Erichard\ElasticQueryBuilder\Filter; | ||
|
||
use Erichard\ElasticQueryBuilder\QueryException; | ||
|
||
class BoolFilter extends Filter | ||
{ | ||
private $must = []; | ||
private $mustNot = []; | ||
private $should = []; | ||
private $filter = []; | ||
|
||
public function addMust(Filter $filter) | ||
{ | ||
$this->must[] = $filter; | ||
|
||
return $this; | ||
} | ||
|
||
public function addMustNot(Filter $filter) | ||
{ | ||
$this->mustNot[] = $filter; | ||
|
||
return $this; | ||
} | ||
|
||
public function addShould(Filter $filter) | ||
{ | ||
$this->should[] = $filter; | ||
|
||
return $this; | ||
} | ||
|
||
public function addFilter(Filter $filter) | ||
{ | ||
$this->filter[] = $filter; | ||
|
||
return $this; | ||
} | ||
|
||
public function isEmpty() | ||
{ | ||
return empty($this->must) | ||
&& empty($this->mustNot) | ||
&& empty($this->should) | ||
&& empty($this->filter) | ||
; | ||
} | ||
|
||
public function build(): array | ||
{ | ||
$filter = []; | ||
|
||
if (!empty($this->must)) { | ||
$filter['must'] = []; | ||
foreach ($this->must as $f) { | ||
$filter['must'][] = $f->build(); | ||
} | ||
} | ||
|
||
if (!empty($this->mustNot)) { | ||
$filter['must_not'] = []; | ||
foreach ($this->mustNot as $f) { | ||
$filter['must_not'][] = $f->build(); | ||
} | ||
} | ||
|
||
if (!empty($this->filter)) { | ||
$filter['filter'] = []; | ||
foreach ($this->filter as $f) { | ||
$filter['filter'][] = $f->build(); | ||
} | ||
} | ||
|
||
if (!empty($this->should)) { | ||
$filter['should'] = []; | ||
foreach ($this->should as $f) { | ||
$filter['should'][] = $f->build(); | ||
} | ||
} | ||
|
||
if (empty($filter)) { | ||
throw new QueryException('Empty filter'); | ||
} | ||
|
||
return [ | ||
'bool' => $filter, | ||
]; | ||
} | ||
} |
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,43 @@ | ||
<?php | ||
|
||
namespace Erichard\ElasticQueryBuilder\Filter; | ||
|
||
abstract class Filter | ||
{ | ||
abstract public function build(): array; | ||
|
||
public static function terms() | ||
{ | ||
return new TermsFilter(); | ||
} | ||
|
||
public static function term() | ||
{ | ||
return new TermFilter(); | ||
} | ||
|
||
public static function bool() | ||
{ | ||
return new BoolFilter(); | ||
} | ||
|
||
public static function range() | ||
{ | ||
return new RangeFilter(); | ||
} | ||
|
||
public static function nested() | ||
{ | ||
return new NestedFilter(); | ||
} | ||
|
||
public static function match() | ||
{ | ||
return new MatchFilter(); | ||
} | ||
|
||
public static function geoDistance() | ||
{ | ||
return new GeoDistanceFilter(); | ||
} | ||
} |
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,33 @@ | ||
<?php | ||
|
||
namespace Erichard\ElasticQueryBuilder\Filter; | ||
|
||
class GeoDistanceFilter extends Filter | ||
{ | ||
protected $distance; | ||
protected $pinLocation; | ||
|
||
public function setDistance($distance) | ||
{ | ||
$this->distance = $distance; | ||
|
||
return $this; | ||
} | ||
|
||
public function setPinLocation($pinLocation) | ||
{ | ||
$this->pinLocation = $pinLocation; | ||
|
||
return $this; | ||
} | ||
|
||
public function build(): array | ||
{ | ||
return [ | ||
'geo_distance' => [ | ||
'distance' => $this->distance, | ||
'geolocation' => $this->pinLocation, | ||
], | ||
]; | ||
} | ||
} |
Oops, something went wrong.