Skip to content

Commit

Permalink
Initial Release
Browse files Browse the repository at this point in the history
  • Loading branch information
Erwan Richard committed Feb 7, 2018
1 parent 263d000 commit 3e5e267
Show file tree
Hide file tree
Showing 16 changed files with 718 additions and 0 deletions.
43 changes: 43 additions & 0 deletions README.md
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());
```
18 changes: 18 additions & 0 deletions composer.json
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"
}
}
35 changes: 35 additions & 0 deletions src/Aggregation/Aggregation.php
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);
}
}
35 changes: 35 additions & 0 deletions src/Aggregation/FilterAggregation.php
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(),
],
];
}
}
35 changes: 35 additions & 0 deletions src/Aggregation/NestedAggregation.php
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(),
],
];
}
}
52 changes: 52 additions & 0 deletions src/Aggregation/TermsAggregation.php
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,
];
}
}
91 changes: 91 additions & 0 deletions src/Filter/BoolFilter.php
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,
];
}
}
43 changes: 43 additions & 0 deletions src/Filter/Filter.php
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();
}
}
33 changes: 33 additions & 0 deletions src/Filter/GeoDistanceFilter.php
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,
],
];
}
}
Loading

0 comments on commit 3e5e267

Please sign in to comment.