Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refacto query filters #64

Merged
merged 9 commits into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 5 additions & 28 deletions src/Filter/ExistsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,11 @@
namespace Novaway\ElasticsearchClient\Filter;


use Novaway\ElasticsearchClient\Query\CombiningFactor;
use Webmozart\Assert\Assert;
use Novaway\ElasticsearchClient\Query\Term\ExistsQuery;

class ExistsFilter implements Filter
/**
* @deprecated use Novaway\ElasticsearchClient\Query\Term\ExistsQuery instead
*/
class ExistsFilter extends ExistsQuery
{
/** @var string */
protected $property;
/** @var string */
private $combiningFactor;

public function __construct(string $property, $combiningFactor = CombiningFactor::FILTER)
{
Assert::oneOf($combiningFactor, CombiningFactor::toArray());
$this->property = $property;
$this->combiningFactor = $combiningFactor;
}

public function getCombiningFactor(): string
{
return $this->combiningFactor;
}

/**
* @inheritDoc
*/
public function formatForQuery(): array
{
return ['exists' => [ 'field' => $this->property]];
}

}
71 changes: 5 additions & 66 deletions src/Filter/GeoDistanceFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,11 @@

namespace Novaway\ElasticsearchClient\Filter;

use Novaway\ElasticsearchClient\Query\CombiningFactor;
use Novaway\ElasticsearchClient\Query\Geo\DistanceUnits;
use Webmozart\Assert\Assert;
use Novaway\ElasticsearchClient\Query\Geo\GeoDistanceQuery;

class GeoDistanceFilter implements Filter
/**
* @deprecated use Novaway\ElasticsearchClient\Query\Geo\GeoDistanceQuery instead
*/
class GeoDistanceFilter extends GeoDistanceQuery
{
/** @var string */
private $property;
/** @var float */
private $latitude;
/** @var float */
private $longitude;
/** @var float */
private $distance;
/** @var string */
private $combiningFactor;
/** @var string */
private $unit;
/** @var array */
private $options;

/**
* GeoDistanceFilter constructor.
* @param string $property
* @param float $latitude
* @param float $longitude
* @param float $distance
* @param string $combiningFactor
* @param string $unit Should be one of those https://www.elastic.co/guide/en/elasticsearch/reference/2.3/common-options.html#distance-units
* @param array $options Used to pass options from https://www.elastic.co/guide/en/elasticsearch/reference/2.3/query-dsl-geo-distance-query.html#_options_4
*/
public function __construct(string $property, float $latitude, float $longitude, float $distance, string $combiningFactor = CombiningFactor::FILTER, string $unit = DistanceUnits::KM, array $options = [])
{
Assert::oneOf($combiningFactor, CombiningFactor::toArray());
Assert::oneOf($unit, DistanceUnits::toArray());

$this->property = $property;
$this->latitude = $latitude;
$this->longitude = $longitude;
$this->distance = $distance;
$this->combiningFactor = $combiningFactor;
$this->unit = $unit;
$this->options = $options;
}

/**
* @return string
*/
public function getCombiningFactor(): string
{
return $this->combiningFactor;
}

/**
* @inheritdoc
*/
public function formatForQuery(): array
{
return [
'geo_distance' => array_merge([
'distance' => sprintf('%01.2f%s', $this->distance, $this->unit),
$this->property => [
'lat' => $this->latitude,
'lon' => $this->longitude
]
], $this->options)
];
}
}
45 changes: 5 additions & 40 deletions src/Filter/InArrayFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,11 @@

namespace Novaway\ElasticsearchClient\Filter;

use Novaway\ElasticsearchClient\Query\CombiningFactor;
use Webmozart\Assert\Assert;
use Novaway\ElasticsearchClient\Query\Term\InArrayQuery;

class InArrayFilter implements Filter
/**
* @deprecated use Novaway\ElasticsearchClient\Query\Term\InArrayQuery instead
*/
class InArrayFilter extends InArrayQuery
{
/** @var string */
private $property;
/** @var array */
private $values;
/** @var string */
private $combiningFactor;

public function __construct(string $property, array $values, string $combiningFactor = CombiningFactor::FILTER)
{
Assert::oneOf($combiningFactor, CombiningFactor::toArray());
$this->property = $property;
$this->values = $values;
$this->combiningFactor = $combiningFactor;
}

/**
* @return string
*/
public function getCombiningFactor(): string
{
return $this->combiningFactor;
}

/**
* @inheritDoc
*/
public function formatForQuery(): array
{
return [
'bool' => [
'should' => array_map(function ($value) {
$matchFilter = new TermFilter($this->property, $value);
return $matchFilter->formatForQuery();
}, $this->values)
]
];
}
}
48 changes: 5 additions & 43 deletions src/Filter/NestedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,11 @@
namespace Novaway\ElasticsearchClient\Filter;


use Novaway\ElasticsearchClient\Clause;
use Novaway\ElasticsearchClient\Query\CombiningFactor;
use Webmozart\Assert\Assert;
use Novaway\ElasticsearchClient\Query\Joining\NestedQuery;

class NestedFilter implements Filter
/**
* @deprecated use Novaway\ElasticsearchClient\Query\Joining\NestedQuery instead
*/
class NestedFilter extends NestedQuery
{
/** @var string */
protected $property;
/** @var Clause[] */
protected $clauses;
/** @var string */
private $combiningFactor;

public function __construct(string $property, $combiningFactor = CombiningFactor::FILTER)
{
Assert::oneOf($combiningFactor, CombiningFactor::toArray());
$this->property = $property;
$this->combiningFactor = $combiningFactor;
$this->clauses = [];
}

public function addClause(Clause $clause)
{
$this->clauses[] = $clause;
}

public function formatForQuery(): array
{
return ['nested' => [
'path' => $this->property,
'query' => [
'bool' => [
'filter' => array_map(function(Clause $clause) {
return $clause->formatForQuery();
}, $this->clauses)
]
]
]];
}

public function getCombiningFactor(): string
{
return $this->combiningFactor;
}

}
74 changes: 5 additions & 69 deletions src/Filter/RangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,11 @@

namespace Novaway\ElasticsearchClient\Filter;

use Novaway\ElasticsearchClient\Query\CombiningFactor;
use Webmozart\Assert\Assert;
use Novaway\ElasticsearchClient\Query\Term\RangeQuery;

class RangeFilter implements Filter
/**
* @deprecated use Novaway\ElasticsearchClient\Query\Term\RangeQuery instead
*/
class RangeFilter extends RangeQuery
{
const GREATER_THAN_OPERATOR = 'gt';
const GREATER_THAN_OR_EQUAL_OPERATOR = 'gte';
const LESS_THAN_OPERATOR = 'lt';
const LESS_THAN_OR_EQUAL_OPERATOR = 'lte';

/** @var string */
private $property;
/** @var mixed */
private $value;
/** @var array */
private $operator;
/** @var string */
private $combiningFactor;

/**
* RangeFilter constructor.
* @param string $property
* @param $value
* @param $operator
*/
public function __construct(string $property, $value, $operator, string $combiningFactor = CombiningFactor::FILTER)
{
Assert::oneOf($combiningFactor, CombiningFactor::toArray());

if(is_array($value) && !is_array($operator)) {
throw new \InvalidArgumentException("Operator should be an array when range filter value is an array");
}
if(!is_array($value) && is_array($operator)) {
throw new \InvalidArgumentException("Operator can't be an array if range filter is not an array");
}
if(is_array($value) && is_array($operator) && count($value) !== count($operator)) {
throw new \InvalidArgumentException("Number of provided operator does not match number of provided values");
}

$this->property = $property;
$this->value = is_array($value) ? $value : [$value];
$this->operator = is_array($operator) ? $operator : [$operator];
$this->combiningFactor = $combiningFactor;
}

/**
* @return string
*/
public function getCombiningFactor(): string
{
return $this->combiningFactor;
}

/**
* @inheritDoc
*/
public function formatForQuery(): array
{
$rangeConditions = [];

$valueCount = count($this->value);
for ($i = 0; $i < $valueCount; $i++){
$rangeConditions[] = [$this->operator[$i] => $this->value[$i]];
}

return [
'range' => [
$this->property => $rangeConditions
]
];
}
}
48 changes: 5 additions & 43 deletions src/Filter/TermFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,12 @@

namespace Novaway\ElasticsearchClient\Filter;

use Novaway\ElasticsearchClient\Query\CombiningFactor;
use Webmozart\Assert\Assert;
use Novaway\ElasticsearchClient\Query\Term\TermQuery;

class TermFilter implements Filter
/**
* @deprecated use Novaway\ElasticsearchClient\Filter\Term\TermQuery instead
*/
class TermFilter extends TermQuery
{
/** @var string */
private $property;
/** @var string */
private $value;
/** @var string */
private $combiningFactor;
/** @var float */
private $boost;

public function __construct(string $property, $value, string $combiningFactor = CombiningFactor::FILTER, float $boost = 1)
{
Assert::oneOf($combiningFactor, CombiningFactor::toArray());
$this->property = $property;
$this->value = $value;
$this->combiningFactor = $combiningFactor;
$this->boost = $boost;
}

/**
* @return string
*/
public function getCombiningFactor(): string
{
return $this->combiningFactor;
}

/**
* @inheritDoc
*/
public function formatForQuery(): array
{
return [
'term' => [
$this->property => [
'value' => $this->value,
'boost' => $this->boost
]
]
];
}
}

Loading