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 165 additions and 156 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
{
......
<?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;
namespace IQDEV\ElasticSearch\Criteria;
final class Pagination
{
......
<?php
namespace IQDEV\ElasticSearch\Query;
namespace IQDEV\ElasticSearch\Criteria\Query;
use IQDEV\ElasticSearch\Criteria;
use IQDEV\ElasticSearch\Criteria\Criteria;
final class SearchQuery
{
......
<?php
namespace IQDEV\ElasticSearch\Query;
namespace IQDEV\ElasticSearch\Criteria\Query;
use IQDEV\ElasticSearch\SearchService;
......
<?php
namespace IQDEV\ElasticSearch\Query;
namespace IQDEV\ElasticSearch\Criteria\Query;
use IQDEV\ElasticSearch\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;
}
}
}
......@@ -113,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 = 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;
/**
* @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);
}
}
}
......@@ -24,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,8 +3,8 @@
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
{
......@@ -23,7 +23,7 @@ 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) {
......@@ -38,7 +38,7 @@ final class FilterNumberFacet implements Esable
$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;
}
}
......@@ -8,11 +8,11 @@ final class Terms implements Esable
{
/**
* @param string $key
* @param string|float|array<string|float> $value
* @param string|float|bool|array<string|float> $value
*/
public function __construct(
private string $key,
private string|float|array $value
private mixed $value
) {
}
......