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 148 additions and 344 deletions
<?php
namespace IQDEV\ElasticSearch\Facet;
use IQDEV\ElasticSearch\Esable;
final class FacetCategory implements Esable
{
private string $category;
public function __construct(string $category)
{
$this->category = $category;
}
public function es(): array
{
return [
'category_id' => $this->category,
];
}
}
<?php
namespace IQDEV\ElasticSearch\Facet;
use IQDEV\ElasticSearch\Document\Property\Property;
use IQDEV\ElasticSearch\Document\Property\PropertyType;
use IQDEV\ElasticSearch\Facet\Type\BaseFacet;
use IQDEV\ElasticSearch\Facet\Type\KeywordFacet;
use IQDEV\ElasticSearch\Facet\Type\NumberFacet;
class FacetFactory
{
public static function createFromProperty(Property $property, mixed $value): Facet
{
match ($property->getType()) {
PropertyType::KEYWORD => $facet = new KeywordFacet($property, $value),
PropertyType::NUMBER => $facet = new NumberFacet($property, $value),
default => $facet = new BaseFacet($property, $value),
};
return $facet;
}
}
<?php
namespace IQDEV\ElasticSearch\Facet;
final class FacetKeyword implements Facetable
{
private string $key;
/** @var string|array<string> */
private string|array $value;
/**
* @param string|string[] $value
*/
public function __construct(string $key, string|array $value)
{
$this->key = $key;
$this->value = $value;
}
public function es(): array
{
return [
'facet_code' => $this->key,
'facet_value' => $this->value,
];
}
}
<?php
namespace IQDEV\ElasticSearch\Facet;
final class FacetNumber implements Facetable
{
private string $key;
private float $value;
public function __construct(string $key, float $value)
{
$this->key = $key;
$this->value = $value;
}
public function es(): array
{
return [
'facet_code' => $this->key,
'facet_value' => $this->value,
];
}
}
<?php
namespace IQDEV\ElasticSearch\Facet;
use IQDEV\ElasticSearch\Facet\Item\FacetItemCollection;
class FacetResult
{
public FacetItemCollection $products;
protected FacetType $type;
protected string $code;
public function __construct(FacetType $type, string $code)
{
$this->type = $type;
$this->code = $code;
$this->products = new FacetItemCollection();
}
public function getType(): FacetType
{
return $this->type;
}
public function getCode(): string
{
return $this->code;
}
}
......@@ -2,8 +2,8 @@
namespace IQDEV\ElasticSearch\Facet;
enum FacetTypeEnum: string
enum FacetType: string
{
case KEYWORD = 'keyword';
case NUMBER = 'number';
case LIST = 'list';
case RANGE = 'range';
}
<?php
declare(strict_types=1);
namespace IQDEV\ElasticSearch\Facet;
class NewFacet implements Facetable
{
protected FacetTypeEnum $type;
public function es(): array
{
return [];
}
}
<?php
namespace IQDEV\ElasticSearch\Facet\Type;
use IQDEV\ElasticSearch\Facet\Facet;
final class BaseFacet extends Facet
{
/**
* @inheritDoc
*/
public function es(): array
{
return [
$this->property->getKey() => $this->value,
];
}
}
<?php
namespace IQDEV\ElasticSearch\Facet\Type;
class FacetListType implements FacetType
{
}
\ No newline at end of file
<?php
namespace IQDEV\ElasticSearch\Facet\Type;
class FacetRangeType implements FacetType
{
}
<?php
namespace IQDEV\ElasticSearch\Facet\Type;
interface FacetType
{
}
<?php
namespace IQDEV\ElasticSearch\Facet\Type;
use IQDEV\ElasticSearch\Facet\Facet;
final class KeywordFacet extends Facet
{
/**
* @inheritDoc
*/
public function es(): array
{
$value = is_array($this->value) ?
array_map(static fn($value) => (string) $value, $this->value) :
(string) $this->value;
return [
'facet_code' => $this->property->getKey(),
'facet_value' => $value
];
}
}
<?php
namespace IQDEV\ElasticSearch\Facet\Type;
use IQDEV\ElasticSearch\Facet\Facet;
final class NumberFacet extends Facet
{
/**
* @inheritDoc
*/
public function es(): array
{
return [
'facet_code' => $this->property->getKey(),
'facet_value' => (float) $this->value,
];
}
}
<?php
namespace IQDEV\ElasticSearch\Filter;
use Ramsey\Collection\AbstractCollection;
/**
* @method self add(FilterGroupCollection $item)
*/
class FilterCollection extends AbstractCollection
{
/** @var LogicOperator Тип логической операции для коллекции */
protected LogicOperator $type;
/**
* @param FilterGroupCollection[] $data
*/
public function __construct(array $data = [])
{
parent::__construct($data);
$this->type = new LogicOperator(LogicOperator::AND);
}
/**
* @inheritDoc
*/
public function getType(): string
{
return FilterGroupCollection::class;
}
/**
* Установка типа логической операции
*
* @param LogicOperator $type
*
* @return $this
*/
public function setLogicalType(LogicOperator $type): self
{
$this->type = $type;
return $this;
}
/**
* Получение типа логической операции
*
* @return LogicOperator
*/
public function getLogicalType(): LogicOperator
{
return $this->type;
}
}
<?php
namespace IQDEV\ElasticSearch\Filter;
final class FilterOperator
{
const EQ = '=';
const NE = '!=';
const GT = '>';
const LT = '<';
const GTE = '>=';
const LTE = '<=';
const CONTAINS = 'CONTAINS';
const NOT_CONTAINS = 'NOT_CONTAINS';
private string $operator;
public function __construct(string $operator)
{
if (!in_array($operator, self::toArray(), true)) {
throw new \InvalidArgumentException(sprintf('invalid operator %s', $operator));
}
$this->operator = $operator;
}
public function value(): string
{
return $this->operator;
}
/**
* @return string[]
*/
public static function toArray(): array
{
return [
self::EQ,
self::NE,
self::GT,
self::LT,
self::GTE,
self::LTE,
self::CONTAINS,
self::NOT_CONTAINS,
];
}
}
<?php
namespace IQDEV\ElasticSearch\Filter;
/**
* @method static self query()
* @method static self post()
*/
final class FilterType
{
/**
* Тип пост фильтрации
*/
public const POST = 'post';
/**
* Тип полной фильтрации
*/
public const QUERY = 'query';
private string $operator;
public function __construct(string $operator)
{
if (!in_array($operator, self::toArray(), true)) {
throw new \InvalidArgumentException(sprintf('invalid operator %s', $operator));
}
$this->operator = $operator;
}
public function value(): string
{
return $this->operator;
}
/**
* @return string[]
*/
public static function toArray(): array
{
return [
self::POST,
self::QUERY
];
}
public static function __callStatic($method, $arguments)
{
if (in_array($method, self::toArray())) {
return new self($method);
}
}
}
<?php
namespace IQDEV\ElasticSearch\Filter;
/**
* @method static self and()
* @method static self or()
*/
final class LogicOperator
{
public const AND = 'AND';
public const OR = 'OR';
private string $operator;
public function __construct(string $operator)
{
if (!in_array($operator, self::toArray(), true)) {
throw new \InvalidArgumentException(sprintf('invalid operator %s', $operator));
}
$this->operator = $operator;
}
public function value(): string
{
return $this->operator;
}
/**
* @return string[]
*/
public static function toArray(): array
{
return [
self::AND,
self::OR
];
}
public static function __callStatic($method, $arguments)
{
$method = strtoupper($method);
if (in_array($method, self::toArray())) {
return new self($method);
}
}
}
......@@ -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,33 +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\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 FacetKeyword($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
......