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; ...@@ -4,16 +4,10 @@ namespace IQDEV\ElasticSearch\Indexer;
final class DeleteIndex implements Index final class DeleteIndex implements Index
{ {
private string $name;
private ?string $id;
public function __construct( public function __construct(
string $name, private string $name,
?string $id = null private ?string $id = null
) ) {
{
$this->name = $name;
$this->id = $id;
} }
public function es(): array public function es(): array
......
...@@ -16,20 +16,11 @@ final class EsHelperEndpoint ...@@ -16,20 +16,11 @@ final class EsHelperEndpoint
{ {
use EndpointTrait; use EndpointTrait;
private Client $esClient;
private Configuration $configuration;
private LoggerInterface $logger;
public function __construct( public function __construct(
Client $esClient, private Client $esClient,
Configuration $configuration, private Configuration $configuration,
LoggerInterface $logger private LoggerInterface $logger
) ) {
{
$this->esClient = $esClient;
$this->configuration = $configuration;
$this->logger = $logger;
} }
public function isIndexExists(): bool public function isIndexExists(): bool
......
...@@ -6,5 +6,4 @@ use IQDEV\ElasticSearch\Esable; ...@@ -6,5 +6,4 @@ use IQDEV\ElasticSearch\Esable;
interface Index extends Esable interface Index extends Esable
{ {
}
}
\ No newline at end of file
...@@ -6,19 +6,11 @@ use IQDEV\ElasticSearch\Esable; ...@@ -6,19 +6,11 @@ use IQDEV\ElasticSearch\Esable;
final class UpdateIndex implements Index final class UpdateIndex implements Index
{ {
private string $name;
private Esable $body;
private ?string $id;
public function __construct( public function __construct(
string $name, private string $name,
Esable $body, private Esable $body,
?string $id = null private ?string $id = null
) ) {
{
$this->name = $name;
$this->body = $body;
$this->id = $id;
} }
public function es(): array 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 @@ ...@@ -3,18 +3,18 @@
namespace IQDEV\ElasticSearch; namespace IQDEV\ElasticSearch;
use IQDEV\ElasticSearch\Document\ProductCollection; use IQDEV\ElasticSearch\Document\ProductCollection;
use IQDEV\ElasticSearch\Facet\FacetCollection; use IQDEV\ElasticSearch\Facet\Collection\FacetResultCollection;
class Result class Result
{ {
private ProductCollection $products; private ProductCollection $products;
private FacetCollection $facets; private FacetResultCollection $facets;
private int $total = 0; private int $total = 0;
public function __construct() public function __construct()
{ {
$this->products = new ProductCollection(); $this->products = new ProductCollection();
$this->facets = new FacetCollection(); $this->facets = new FacetResultCollection();
} }
public function setTotal(int $total): void public function setTotal(int $total): void
...@@ -32,7 +32,7 @@ class Result ...@@ -32,7 +32,7 @@ class Result
return $this->products; return $this->products;
} }
public function getFacets(): FacetCollection public function getFacets(): FacetResultCollection
{ {
return $this->facets; return $this->facets;
} }
......
...@@ -13,11 +13,10 @@ final class Aggs implements Esable ...@@ -13,11 +13,10 @@ final class Aggs implements Esable
private ?Nested $nested = null; private ?Nested $nested = null;
private ?Terms $terms = null; private ?Terms $terms = null;
private ?Stats $stats = null; private ?Stats $stats = null;
private string $key;
public function __construct(string $key) public function __construct(
{ private string $key
$this->key = $key; ) {
} }
public function addAggs(Aggs $aggs): self public function addAggs(Aggs $aggs): self
......
...@@ -6,11 +6,9 @@ use IQDEV\ElasticSearch\Esable; ...@@ -6,11 +6,9 @@ use IQDEV\ElasticSearch\Esable;
final class Stats implements Esable final class Stats implements Esable
{ {
private string $field; public function __construct(
private string $field
public function __construct(string $field) ) {
{
$this->field = $field;
} }
public function es(): array public function es(): array
......
...@@ -7,11 +7,10 @@ use IQDEV\ElasticSearch\Esable; ...@@ -7,11 +7,10 @@ use IQDEV\ElasticSearch\Esable;
final class Terms implements Esable final class Terms implements Esable
{ {
private array $options = []; private array $options = [];
private string $field;
public function __construct(string $field) public function __construct(
{ private string $field
$this->field = $field; ) {
} }
public function setSize(int $size): self public function setSize(int $size): self
......
...@@ -7,15 +7,14 @@ use IQDEV\ElasticSearch\Search\Nested; ...@@ -7,15 +7,14 @@ use IQDEV\ElasticSearch\Search\Nested;
final class FilterKeywordFacet implements Esable final class FilterKeywordFacet implements Esable
{ {
public string $key; /**
* @param string $key
/** @var string|string[] */ * @param string|array<string> $value
public $value; */
public function __construct(
public function __construct(string $key, $value) public string $key,
{ public string|array $value
$this->key = $key; ) {
$this->value = $value;
} }
public function es(): array public function es(): array
...@@ -25,9 +24,8 @@ final class FilterKeywordFacet implements Esable ...@@ -25,9 +24,8 @@ final class FilterKeywordFacet implements Esable
$nested = new Nested(); $nested = new Nested();
$query = new Query(); $query = new Query();
$query $query->getFilter()->add(new Terms($path . '.facet_code', $this->key));
->filter(new Terms($path . '.facet_code', $this->key)) $query->getFilter()->add(new Terms($path . '.facet_value', $this->value));
->filter(new Terms($path . '.facet_value', $this->value));
$nested $nested
->setPath($path) ->setPath($path)
......
...@@ -3,19 +3,16 @@ ...@@ -3,19 +3,16 @@
namespace IQDEV\ElasticSearch\Search\BoolQuery; namespace IQDEV\ElasticSearch\Search\BoolQuery;
use IQDEV\ElasticSearch\Esable; use IQDEV\ElasticSearch\Esable;
use IQDEV\ElasticSearch\Criteria\Filter\FilterOperator;
use IQDEV\ElasticSearch\Search\Nested; use IQDEV\ElasticSearch\Search\Nested;
use IQDEV\ElasticSearch\Filter\FilterOperator;
final class FilterNumberFacet implements Esable final class FilterNumberFacet implements Esable
{ {
public string $key;
public array $conditions; public function __construct(
public string $key,
public function __construct(string $key, array $conditions) public array $conditions
{ ) {
$this->key = $key;
$this->conditions = $conditions;
} }
public function es(): array public function es(): array
...@@ -26,32 +23,22 @@ final class FilterNumberFacet implements Esable ...@@ -26,32 +23,22 @@ final class FilterNumberFacet implements Esable
$query = new Query(); $query = new Query();
$query $query
->filter(new Stats($path.'.facet_code', $this->key)); ->getFilter()->add(new Stats($path.'.facet_code', $this->key));
$conditions = []; $conditions = [];
foreach ($this->conditions as $operator => $value) { foreach ($this->conditions as $operator => $value) {
switch ($operator) { $key = in_array(FilterOperator::from($operator), [
case FilterOperator::GTE: FilterOperator::GTE,
$key = 'gte'; FilterOperator::LTE,
break; FilterOperator::GT,
case FilterOperator::LTE: FilterOperator::LT,
$key = 'lte'; ]) ? $operator : null;
break;
case FilterOperator::GT:
$key = 'gt';
break;
case FilterOperator::LT:
$key = 'lt';
break;
default:
$key = null;
break;
}
if (isset($key)) { if (isset($key)) {
$conditions[$key] = $value; $conditions[$key] = $value;
} }
} }
$query->filter(new Stats($path.'.facet_value', $conditions)); $query->getFilter()->add(new Stats($path.'.facet_value', $conditions));
$nested $nested
->setPath($path) ->setPath($path)
......
...@@ -32,43 +32,34 @@ final class Query implements Esable ...@@ -32,43 +32,34 @@ final class Query implements Esable
* @param Terms|Nested $item * @param Terms|Nested $item
* @return $this * @return $this
*/ */
public function match($item): self public function getMatch(): BoolQueryCollection
{ {
$this->match->add($item); return $this->match;
return $this;
} }
/** public function getMust(): BoolQueryCollection
* @param Terms|Nested $item
* @return $this
*/
public function must($item): self
{ {
$this->must->add($item); return $this->must;
return $this;
} }
public function filter(Esable $item): self public function getFilter(): BoolQueryCollection
{ {
$this->filter->add($item); return $this->filter;
return $this;
} }
public function should(Esable $item): self public function setFilter(BoolQueryCollection $filter): BoolQueryCollection
{ {
$this->should->add($item); return $this->filter;
return $this;
} }
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 public function isEmpty(): bool
...@@ -127,4 +118,34 @@ final class Query implements Esable ...@@ -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; ...@@ -6,19 +6,14 @@ use IQDEV\ElasticSearch\Esable;
final class Stats implements 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; public function __construct(
private string $key,
/** private string|float|array $value
* @param string|float|string[]|float[] $value ) {
*/
public function __construct(string $key, $value)
{
$this->key = $key;
$this->value = $value;
} }
public function es(): array public function es(): array
......
...@@ -6,19 +6,14 @@ use IQDEV\ElasticSearch\Esable; ...@@ -6,19 +6,14 @@ use IQDEV\ElasticSearch\Esable;
final class Terms implements 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; public function __construct(
private string $key,
/** private mixed $value
* @param string|float|string[]|float[] $value ) {
*/
public function __construct(string $key, $value)
{
$this->key = $key;
$this->value = $value;
} }
public function es(): array public function es(): array
......