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 118 additions and 186 deletions
......@@ -15,12 +15,14 @@ class ArrayHelper
public static function array_filter_recursive(array $array, ?callable $callback = null): array
{
$array = is_callable($callback) ? array_filter($array, $callback) : array_filter($array);
foreach ($array as &$value) {
foreach ($array as $key => &$value) {
if (is_array($value)) {
$value = call_user_func([__CLASS__, __FUNCTION__], $value, $callback);
if (!empty($value)) {
$value = self::array_filter_recursive($value, $callback);
} else {
unset($array[$key]);
}
}
}
......
......@@ -6,19 +6,12 @@ use IQDEV\ElasticSearch\Esable;
final class AddIndex implements Index
{
private string $name;
private Esable $body;
private ?string $id;
public function __construct(
string $name,
Esable $body,
?string $id = null
private string $name,
private Esable $body,
private ?string $id = null
)
{
$this->name = $name;
$this->body = $body;
$this->id = $id;
}
public function es(): array
......
......@@ -4,34 +4,39 @@ namespace IQDEV\ElasticSearch\Indexer;
use IQDEV\ElasticSearch\Configuration;
use IQDEV\ElasticSearch\Document\ProductDocument;
use IQDEV\ElasticSearch\Facet\FacetCategory;
use IQDEV\ElasticSearch\Facet\FacetKeyword;
use IQDEV\ElasticSearch\Facet\FacetNumber;
use IQDEV\ElasticSearch\Document\Property\Property;
use IQDEV\ElasticSearch\Document\Property\PropertyType;
use IQDEV\ElasticSearch\Facet\FacetFactory;
class BaseIndexProvider implements IndexProvider
{
private array $products;
private ?int $size = null;
private ?int $limit = null;
private Configuration $configuration;
public function __construct($products, $configuration)
{
$this->configuration = $configuration;
$this->products = $products;
public function __construct(
private array $products,
private Configuration $configuration
) {
}
public function get(): \Generator
{
foreach ($this->products as $product) {
$document = new ProductDocument(new FacetCategory($product['category']));
$document = new ProductDocument(
FacetFactory::createFromProperty(new Property('category_id', PropertyType::BASE), $product['category'])
);
$document->setAdditionData($product['data'] ?? []);
foreach ($product['properties'] as $type => $values) {
foreach ($values as $key => $prop) {
foreach ($values as $key => $value) {
if ($type === 'number') {
$document->getNumberFacets()->add(new FacetNumber($key, $prop));
$document->getNumberFacets()->add(
FacetFactory::createFromProperty(new Property($key, PropertyType::NUMBER), $value)
);
} else {
$document->getKeywordFacets()->add(new FacetKeyword($key, $prop));
$document->getKeywordFacets()->add(
FacetFactory::createFromProperty(new Property($key, PropertyType::KEYWORD), $value)
);
}
}
}
......
......@@ -6,19 +6,11 @@ use IQDEV\ElasticSearch\Esable;
final class BulkIndex 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
......
......@@ -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
}
......@@ -74,11 +74,15 @@ final class IndexRunner
continue;
}
if (!($index instanceof AddIndex) && !($index instanceof UpdateIndex)) {
if ($index instanceof AddIndex) {
$this->esClient->index($index->es());
continue;
}
$this->esClient->index($index->es());
if ($index instanceof UpdateIndex) {
$this->esClient->update($index->es());
continue;
}
}
}
}
......
......@@ -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;
abstract class Order implements Esable
{
public string $by;
public OrderType $direction;
public array $properties;
public function __construct(string $by, OrderType $direction, array $properties = [])
{
$this->by = $by;
$this->direction = $direction;
$this->properties = $properties;
}
public function es(): array
{
return array_merge([$this->by => $this->direction::getType()], $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;
use IQDEV\ElasticSearch\Document\ProductCollection;
use IQDEV\ElasticSearch\Facet\Collection\FacetResultCollection;
class Result
{
private ProductCollection $products;
private FacetResultCollection $facets;
private int $total = 0;
public function __construct()
{
$this->products = new ProductCollection();
$this->facets = new FacetResultCollection();
}
public function setTotal(int $total): void
{
$this->total = $total;
}
public function getTotal(): int
{
return $this->total;
}
public function getProducts(): ProductCollection
{
return $this->products;
}
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\Search\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)
......