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 179 additions and 248 deletions
<?php
namespace IQDEV\ElasticSearch\Order;
namespace IQDEV\ElasticSearch\Criteria\Order;
use IQDEV\ElasticSearch\Document\Property\Property;
use IQDEV\ElasticSearch\Document\Property\PropertyType;
use IQDEV\ElasticSearch\Order\Type\BaseOrder;
use IQDEV\ElasticSearch\Order\Type\KeywordPropertyOrder;
use IQDEV\ElasticSearch\Order\Type\NumberPropertyOrder;
use IQDEV\ElasticSearch\Criteria\Order\Type\BaseOrder;
use IQDEV\ElasticSearch\Criteria\Order\Type\KeywordPropertyOrder;
use IQDEV\ElasticSearch\Criteria\Order\Type\NumberPropertyOrder;
class OrderFactory
{
......@@ -14,12 +14,10 @@ class OrderFactory
Property $property,
OrderDirection $direction,
): Order {
match ($property->getType()) {
PropertyType::KEYWORD => $order = new KeywordPropertyOrder($property, $direction),
PropertyType::NUMBER => $order = new NumberPropertyOrder($property, $direction),
default => $order = new BaseOrder($property, $direction),
return match ($property->getType()) {
PropertyType::KEYWORD => new KeywordPropertyOrder($property, $direction),
PropertyType::NUMBER => new NumberPropertyOrder($property, $direction),
default => new BaseOrder($property, $direction),
};
return $order;
}
}
<?php
namespace IQDEV\ElasticSearch\Order\Type;
namespace IQDEV\ElasticSearch\Criteria\Order\Type;
use IQDEV\ElasticSearch\Order\Order;
use IQDEV\ElasticSearch\Criteria\Order\Order;
class BaseOrder extends Order
{
......
<?php
namespace IQDEV\ElasticSearch\Order\Type;
namespace IQDEV\ElasticSearch\Criteria\Order\Type;
use IQDEV\ElasticSearch\Order\Order;
use IQDEV\ElasticSearch\Criteria\Order\Order;
class KeywordPropertyOrder extends Order
{
......
<?php
namespace IQDEV\ElasticSearch\Order\Type;
namespace IQDEV\ElasticSearch\Criteria\Order\Type;
use IQDEV\ElasticSearch\Order\Order;
use IQDEV\ElasticSearch\Criteria\Order\Order;
class NumberPropertyOrder extends Order
{
......
<?php
namespace IQDEV\ElasticSearch\Criteria;
final class Pagination
{
public function __construct(
public int $limit = 20,
public int $offset = 0
) {
}
}
<?php
namespace IQDEV\ElasticSearch\Criteria\Query;
use IQDEV\ElasticSearch\Criteria\Criteria;
final class SearchQuery
{
public function __construct(
public Criteria $criteria
) {
}
}
<?php
namespace IQDEV\ElasticSearch\Query;
namespace IQDEV\ElasticSearch\Criteria\Query;
use IQDEV\ElasticSearch\SearchService;
final class SearchQueryHandler
{
private SearchService $searchService;
public function __construct(SearchService $searchService)
{
$this->searchService = $searchService;
public function __construct(
private SearchService $searchService
) {
}
public function handle(SearchQuery $q): SearchQueryResponse
......
<?php
namespace IQDEV\ElasticSearch\Query;
namespace IQDEV\ElasticSearch\Criteria\Query;
use IQDEV\ElasticSearch\Result;
final class SearchQueryResponse
{
public Result $result;
public function __construct(Result $result)
{
$this->result = $result;
public function __construct(
public Result $result
) {
}
}
<?php
declare(strict_types=1);
namespace IQDEV\ElasticSearch\Criteria\Search;
use IQDEV\ElasticSearch\Document\Property\Property;
class Search
{
public function __construct(
private readonly Property $property,
private readonly mixed $value,
) {
}
public function getProperty(): Property
{
return $this->property;
}
public function getValue(): mixed
{
return $this->value;
}
}
<?php
namespace IQDEV\ElasticSearch\Criteria\Search;
use Ramsey\Collection\AbstractCollection;
class SearchCollection extends AbstractCollection
{
/**
* @inheritDoc
*/
public function getType(): string
{
return Search::class;
}
}
<?php
declare(strict_types=1);
namespace IQDEV\ElasticSearch\Criteria\Search;
use IQDEV\ElasticSearch\Config\MappingValidator;
use IQDEV\ElasticSearch\Configuration;
use IQDEV\ElasticSearch\Criteria\Match\QueryMatch;
use IQDEV\ElasticSearch\Search\BoolQuery\Query;
use IQDEV\ElasticSearch\Search\BoolQuery\Terms;
use IQDEV\ElasticSearch\Search\Nested;
class SearchQuery
{
public function __construct(
private readonly Search $search,
) {
}
public function toQueryMatch(): QueryMatch
{
return new QueryMatch(
$this->search->getProperty()->getKey(),
$this->search->getValue(),
);
}
public function toMust(Configuration $configuration): Terms|Nested
{
if (MappingValidator::isPropertyExists($configuration, $this->search->getProperty()->getKey())) {
return new Terms($this->search->getProperty()->getKey(), $this->search->getValue());
} else {
$path = 'search_data.keyword_facet';
$nested = new Nested();
$query = new Query();
$query->getFilter()->add(new Terms($path . '.facet_code', $this->search->getProperty()->getKey()));
$query->getFilter()->add(new Terms($path . '.facet_value', $this->search->getValue()));
$nested
->setPath($path)
->setQuery($query);
return $nested;
}
}
}
......@@ -4,18 +4,10 @@ namespace IQDEV\ElasticSearch\Document;
final class Product
{
public string $id;
public string $title;
public array $info;
public function __construct(
string $id,
string $title,
array $info = []
)
{
$this->id = $id;
$this->title = $title;
$this->info = $info;
public string $id,
public string $title,
public array $info = []
) {
}
}
......@@ -17,18 +17,11 @@ class ProductDocument implements Document
private array $info = [];
private bool $skipEmpty = false;
private Facet $categoryFacet;
public function __construct(
Facet $categoryFacet
)
{
private Facet $categoryFacet
) {
$this->keywordFacets = new FacetCollection();
$this->numberFacets = new FacetCollection();
$this->categoryFacet = $categoryFacet;
}
public static function create(Facet $categoryFacet): self
......@@ -120,7 +113,7 @@ class ProductDocument implements Document
$result = array_replace_recursive($document, $this->properties);
if (true === $this->skipEmpty) {
$result = ArrayHelper::array_filter_recursive($result);
$result = ArrayHelper::array_filter_recursive($result, static fn ($val) => $val !== null || $val === false);
}
return $result;
......
......@@ -7,4 +7,5 @@ enum PropertyType
case BASE;
case KEYWORD;
case NUMBER;
case TEXT;
}
......@@ -11,9 +11,13 @@ final class KeywordFacet extends Facet
*/
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' => (string) $this->value,
'facet_value' => $value
];
}
}
<?php
namespace IQDEV\ElasticSearch\Filter\Collection;
use IQDEV\ElasticSearch\Filter\LogicOperator;
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
......