Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Showing
with 105 additions and 243 deletions
......@@ -4,16 +4,10 @@ namespace IQDEV\ElasticSearch\Indexer;
final class DeleteIndex implements Index
{
private string $name;
private ?string $id;
public function __construct(
string $name,
?string $id = null
)
{
$this->name = $name;
$this->id = $id;
private string $name,
private ?string $id = null
) {
}
public function es(): array
......
......@@ -16,20 +16,11 @@ final class EsHelperEndpoint
{
use EndpointTrait;
private Client $esClient;
private Configuration $configuration;
private LoggerInterface $logger;
public function __construct(
Client $esClient,
Configuration $configuration,
LoggerInterface $logger
)
{
$this->esClient = $esClient;
$this->configuration = $configuration;
$this->logger = $logger;
private Client $esClient,
private Configuration $configuration,
private LoggerInterface $logger
) {
}
public function isIndexExists(): bool
......
......@@ -6,5 +6,4 @@ use IQDEV\ElasticSearch\Esable;
interface Index extends Esable
{
}
\ No newline at end of file
}
......@@ -6,19 +6,11 @@ use IQDEV\ElasticSearch\Esable;
final class UpdateIndex implements Index
{
private string $name;
private Esable $body;
private ?string $id;
public function __construct(
string $name,
Esable $body,
?string $id = null
)
{
$this->name = $name;
$this->body = $body;
$this->id = $id;
private string $name,
private Esable $body,
private ?string $id = null
) {
}
public function es(): array
......
<?php
namespace IQDEV\ElasticSearch\Order;
use IQDEV\ElasticSearch\Esable;
use IQDEV\ElasticSearch\Document\Property\Type;
class Order implements Esable
{
public Type $by;
public OrderType $direction;
public array $properties;
public function __construct(Type $by, OrderType $direction, array $properties = [])
{
$this->by = $by;
$this->direction = $direction;
$this->properties = $properties;
}
public function es(): array
{
return array_merge([$this->by->key() => $this->direction::getType()], $this->properties);
}
public function orderBy(): Type
{
return $this->by;
}
public function orderType(): OrderType
{
return $this->direction;
}
public function orderProperties(): array
{
return $this->properties;
}
}
<?php
namespace IQDEV\ElasticSearch\Order;
class OrderAscType extends OrderType
{
protected static string $code = 'asc';
}
<?php
namespace IQDEV\ElasticSearch\Order;
class OrderDescType extends OrderType
{
protected static string $code = 'desc';
}
<?php
namespace IQDEV\ElasticSearch\Order;
class OrderField extends Order
{
}
<?php
namespace IQDEV\ElasticSearch\Order;
abstract class OrderType
{
protected static string $code;
public static function getType(): string
{
return static::$code;
}
}
<?php
namespace IQDEV\ElasticSearch;
final class Pagination
{
public int $limit;
public int $offset;
public function __construct(int $limit = 20, int $offset = 0)
{
$this->limit = $limit;
$this->offset = $offset;
}
}
<?php
namespace IQDEV\ElasticSearch\Query;
use IQDEV\ElasticSearch\Criteria;
final class SearchQuery
{
public Criteria $criteria;
public function __construct(Criteria $criteria)
{
$this->criteria = $criteria;
}
}
......@@ -3,18 +3,18 @@
namespace IQDEV\ElasticSearch;
use IQDEV\ElasticSearch\Document\ProductCollection;
use IQDEV\ElasticSearch\Facet\FacetCollection;
use IQDEV\ElasticSearch\Facet\Collection\FacetResultCollection;
class Result
{
private ProductCollection $products;
private FacetCollection $facets;
private FacetResultCollection $facets;
private int $total = 0;
public function __construct()
{
$this->products = new ProductCollection();
$this->facets = new FacetCollection();
$this->facets = new FacetResultCollection();
}
public function setTotal(int $total): void
......@@ -32,7 +32,7 @@ class Result
return $this->products;
}
public function getFacets(): FacetCollection
public function getFacets(): FacetResultCollection
{
return $this->facets;
}
......
......@@ -13,11 +13,10 @@ final class Aggs implements Esable
private ?Nested $nested = null;
private ?Terms $terms = null;
private ?Stats $stats = null;
private string $key;
public function __construct(string $key)
{
$this->key = $key;
public function __construct(
private string $key
) {
}
public function addAggs(Aggs $aggs): self
......
......@@ -6,11 +6,9 @@ use IQDEV\ElasticSearch\Esable;
final class Stats implements Esable
{
private string $field;
public function __construct(string $field)
{
$this->field = $field;
public function __construct(
private string $field
) {
}
public function es(): array
......
......@@ -7,11 +7,10 @@ use IQDEV\ElasticSearch\Esable;
final class Terms implements Esable
{
private array $options = [];
private string $field;
public function __construct(string $field)
{
$this->field = $field;
public function __construct(
private string $field
) {
}
public function setSize(int $size): self
......
......@@ -7,15 +7,14 @@ use IQDEV\ElasticSearch\Search\Nested;
final class FilterKeywordFacet implements Esable
{
public string $key;
/** @var string|string[] */
public $value;
public function __construct(string $key, $value)
{
$this->key = $key;
$this->value = $value;
/**
* @param string $key
* @param string|array<string> $value
*/
public function __construct(
public string $key,
public string|array $value
) {
}
public function es(): array
......@@ -25,9 +24,8 @@ final class FilterKeywordFacet implements Esable
$nested = new Nested();
$query = new Query();
$query
->filter(new Terms($path . '.facet_code', $this->key))
->filter(new Terms($path . '.facet_value', $this->value));
$query->getFilter()->add(new Terms($path . '.facet_code', $this->key));
$query->getFilter()->add(new Terms($path . '.facet_value', $this->value));
$nested
->setPath($path)
......
......@@ -3,19 +3,16 @@
namespace IQDEV\ElasticSearch\Search\BoolQuery;
use IQDEV\ElasticSearch\Esable;
use IQDEV\ElasticSearch\Criteria\Filter\FilterOperator;
use IQDEV\ElasticSearch\Search\Nested;
use IQDEV\ElasticSearch\Filter\FilterOperator;
final class FilterNumberFacet implements Esable
{
public string $key;
public array $conditions;
public function __construct(string $key, array $conditions)
{
$this->key = $key;
$this->conditions = $conditions;
public function __construct(
public string $key,
public array $conditions
) {
}
public function es(): array
......@@ -26,32 +23,22 @@ final class FilterNumberFacet implements Esable
$query = new Query();
$query
->filter(new Stats($path.'.facet_code', $this->key));
->getFilter()->add(new Stats($path.'.facet_code', $this->key));
$conditions = [];
foreach ($this->conditions as $operator => $value) {
switch ($operator) {
case FilterOperator::GTE:
$key = 'gte';
break;
case FilterOperator::LTE:
$key = 'lte';
break;
case FilterOperator::GT:
$key = 'gt';
break;
case FilterOperator::LT:
$key = 'lt';
break;
default:
$key = null;
break;
}
$key = in_array(FilterOperator::from($operator), [
FilterOperator::GTE,
FilterOperator::LTE,
FilterOperator::GT,
FilterOperator::LT,
]) ? $operator : null;
if (isset($key)) {
$conditions[$key] = $value;
}
}
$query->filter(new Stats($path.'.facet_value', $conditions));
$query->getFilter()->add(new Stats($path.'.facet_value', $conditions));
$nested
->setPath($path)
......
......@@ -32,43 +32,34 @@ final class Query implements Esable
* @param Terms|Nested $item
* @return $this
*/
public function match($item): self
public function getMatch(): BoolQueryCollection
{
$this->match->add($item);
return $this;
return $this->match;
}
/**
* @param Terms|Nested $item
* @return $this
*/
public function must($item): self
public function getMust(): BoolQueryCollection
{
$this->must->add($item);
return $this;
return $this->must;
}
public function filter(Esable $item): self
public function getFilter(): BoolQueryCollection
{
$this->filter->add($item);
return $this;
return $this->filter;
}
public function should(Esable $item): self
public function setFilter(BoolQueryCollection $filter): BoolQueryCollection
{
$this->should->add($item);
return $this;
return $this->filter;
}
public function mustNot(Esable $item): self
public function getShould(): BoolQueryCollection
{
$this->mustNot->add($item);
return $this->should;
}
return $this;
public function getMustNot(): BoolQueryCollection
{
return $this->mustNot;
}
public function isEmpty(): bool
......@@ -127,4 +118,34 @@ final class Query implements Esable
],
];
}
public function modify(Query $another): self
{
foreach ($another->getMust() as $item) {
$this->getMust()->add($item);
}
foreach ($another->getFilter() as $item) {
$this->getFilter()->add($item);
}
foreach ($another->getShould() as $item) {
$this->getShould()->add($item);
}
foreach ($another->getMustNot() as $item) {
$this->getMustNot()->add($item);
}
foreach ($another->getMatch() as $item) {
$this->getMatch()->add($item);
}
return $this;
}
public function __clone(): void
{
$this->must = clone $this->must;
$this->should = clone $this->should;
$this->filter = clone $this->filter;
$this->mustNot = clone $this->mustNot;
$this->match = clone $this->match;
}
}
......@@ -6,19 +6,14 @@ use IQDEV\ElasticSearch\Esable;
final class Stats implements Esable
{
private string $key;
/**
* @var string|float|string[]|float[]
* @param string $key
* @param string|float|array<string|float> $value
*/
private $value;
/**
* @param string|float|string[]|float[] $value
*/
public function __construct(string $key, $value)
{
$this->key = $key;
$this->value = $value;
public function __construct(
private string $key,
private string|float|array $value
) {
}
public function es(): array
......
......@@ -6,19 +6,14 @@ use IQDEV\ElasticSearch\Esable;
final class Terms implements Esable
{
private string $key;
/**
* @var string|float|string[]|float[]
* @param string $key
* @param string|float|bool|array<string|float> $value
*/
private $value;
/**
* @param string|float|string[]|float[] $value
*/
public function __construct(string $key, $value)
{
$this->key = $key;
$this->value = $value;
public function __construct(
private string $key,
private mixed $value
) {
}
public function es(): array
......