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 178 additions and 211 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;
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 ...@@ -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\Search\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)
......
...@@ -11,6 +11,7 @@ final class Query implements Esable ...@@ -11,6 +11,7 @@ final class Query implements Esable
protected BoolQueryCollection $filter; protected BoolQueryCollection $filter;
protected BoolQueryCollection $should; protected BoolQueryCollection $should;
protected BoolQueryCollection $mustNot; protected BoolQueryCollection $mustNot;
protected BoolQueryCollection $match;
protected string $type; protected string $type;
...@@ -31,43 +32,34 @@ final class Query implements Esable ...@@ -31,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
...@@ -126,4 +118,34 @@ final class Query implements Esable ...@@ -126,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
......
...@@ -6,13 +6,10 @@ use IQDEV\ElasticSearch\Esable; ...@@ -6,13 +6,10 @@ use IQDEV\ElasticSearch\Esable;
class Pagination implements Esable class Pagination implements Esable
{ {
private ?int $size; public function __construct(
private ?int $from; private ?int $size = null,
private ?int $from = null
public function __construct(?int $size = null, ?int $from = null) ) {
{
$this->size = $size;
$this->from = $from;
} }
public function es(): array public function es(): array
......
...@@ -3,7 +3,8 @@ ...@@ -3,7 +3,8 @@
namespace IQDEV\ElasticSearch\Search; namespace IQDEV\ElasticSearch\Search;
use IQDEV\ElasticSearch\Esable; use IQDEV\ElasticSearch\Esable;
use IQDEV\ElasticSearch\Order\OrderCollection; use IQDEV\ElasticSearch\Criteria\Match\QueryMatchCollection;
use IQDEV\ElasticSearch\Criteria\Order\OrderCollection;
use IQDEV\ElasticSearch\Search\Aggs\AggsCollection; use IQDEV\ElasticSearch\Search\Aggs\AggsCollection;
use IQDEV\ElasticSearch\Search\BoolQuery\Query; use IQDEV\ElasticSearch\Search\BoolQuery\Query;
...@@ -14,7 +15,7 @@ final class Request implements Esable ...@@ -14,7 +15,7 @@ final class Request implements Esable
private ?AggsCollection $aggs = null; private ?AggsCollection $aggs = null;
private ?Pagination $pagination = null; private ?Pagination $pagination = null;
private ?OrderCollection $sort = null; private ?OrderCollection $sort = null;
private array $match = []; private ?QueryMatchCollection $matchCollection = null;
private ?array $source = null; private ?array $source = null;
public function setPagination(?Pagination $pagination): self public function setPagination(?Pagination $pagination): self
...@@ -33,6 +34,13 @@ final class Request implements Esable ...@@ -33,6 +34,13 @@ final class Request implements Esable
return $this->query; return $this->query;
} }
public function setQuery(Query $query): self
{
$this->query = $query;
return $this;
}
public function getPostFilter(): Query public function getPostFilter(): Query
{ {
if (null === $this->postFilter) { if (null === $this->postFilter) {
...@@ -42,6 +50,13 @@ final class Request implements Esable ...@@ -42,6 +50,13 @@ final class Request implements Esable
return $this->postFilter; return $this->postFilter;
} }
public function setPostFilter(Query $query): self
{
$this->postFilter = $query;
return $this;
}
public function getAggs(): AggsCollection public function getAggs(): AggsCollection
{ {
if (null === $this->aggs) { if (null === $this->aggs) {
...@@ -51,16 +66,25 @@ final class Request implements Esable ...@@ -51,16 +66,25 @@ final class Request implements Esable
return $this->aggs; return $this->aggs;
} }
public function getPagination(): ?Pagination public function setAggs(AggsCollection $aggs): self
{ {
return $this->pagination; $this->aggs = $aggs;
return $this;
} }
public function addMatch(string $key, array $param): self public function getQueryMatch(): QueryMatchCollection
{ {
$this->match[$key] = $param; if (null === $this->matchCollection) {
$this->matchCollection = new QueryMatchCollection();
}
return $this; return $this->matchCollection;
}
public function getPagination(): ?Pagination
{
return $this->pagination;
} }
public function setSource(array $s): self public function setSource(array $s): self
...@@ -95,10 +119,8 @@ final class Request implements Esable ...@@ -95,10 +119,8 @@ final class Request implements Esable
$request['query'] = $this->query->es()['query']; $request['query'] = $this->query->es()['query'];
} }
if (false === empty($this->match)) { if ($this->matchCollection && false === $this->matchCollection->isEmpty()) {
foreach ($this->match as $key => $value) { $request['query']['match'] = $this->matchCollection->es();
$request['query']['match'][$key] = $value;
}
} }
if ($this->aggs) { if ($this->aggs) {
......