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 159 additions and 212 deletions
......@@ -10,15 +10,13 @@ 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
......
......@@ -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
}
......@@ -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;
final class Pagination
{
public int $limit;
public int $offset;
public function __construct(int $limit = 20, int $offset = 0)
{
$this->limit = $limit;
$this->offset = $offset;
}
}
<?php
namespace IQDEV\ElasticSearch\Query;
use IQDEV\ElasticSearch\Criteria;
final class SearchQuery
{
public Criteria $criteria;
public function __construct(Criteria $criteria)
{
$this->criteria = $criteria;
}
}
......@@ -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\ElasticSearch\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)
......
......@@ -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;
}
}
......@@ -6,19 +6,14 @@ use IQDEV\ElasticSearch\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;
/**
* @param string|float|string[]|float[] $value
*/
public function __construct(string $key, $value)
{
$this->key = $key;
$this->value = $value;
public function __construct(
private string $key,
private string|float|array $value
) {
}
public function es(): array
......
......@@ -6,19 +6,14 @@ use IQDEV\ElasticSearch\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;
/**
* @param string|float|string[]|float[] $value
*/
public function __construct(string $key, $value)
{
$this->key = $key;
$this->value = $value;
public function __construct(
private string $key,
private mixed $value
) {
}
public function es(): array
......
......@@ -6,13 +6,10 @@ use IQDEV\ElasticSearch\Esable;
class Pagination implements Esable
{
private ?int $size;
private ?int $from;
public function __construct(?int $size = null, ?int $from = null)
{
$this->size = $size;
$this->from = $from;
public function __construct(
private ?int $size = null,
private ?int $from = null
) {
}
public function es(): array
......
......@@ -3,7 +3,8 @@
namespace IQDEV\ElasticSearch\Search;
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\BoolQuery\Query;
......@@ -14,7 +15,7 @@ final class Request implements Esable
private ?AggsCollection $aggs = null;
private ?Pagination $pagination = null;
private ?OrderCollection $sort = null;
private array $match = [];
private ?QueryMatchCollection $matchCollection = null;
private ?array $source = null;
public function setPagination(?Pagination $pagination): self
......@@ -33,6 +34,13 @@ final class Request implements Esable
return $this->query;
}
public function setQuery(Query $query): self
{
$this->query = $query;
return $this;
}
public function getPostFilter(): Query
{
if (null === $this->postFilter) {
......@@ -42,6 +50,13 @@ final class Request implements Esable
return $this->postFilter;
}
public function setPostFilter(Query $query): self
{
$this->postFilter = $query;
return $this;
}
public function getAggs(): AggsCollection
{
if (null === $this->aggs) {
......@@ -51,16 +66,25 @@ final class Request implements Esable
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
......@@ -95,10 +119,8 @@ final class Request implements Esable
$request['query'] = $this->query->es()['query'];
}
if (false === empty($this->match)) {
foreach ($this->match as $key => $value) {
$request['query']['match'][$key] = $value;
}
if ($this->matchCollection && false === $this->matchCollection->isEmpty()) {
$request['query']['match'] = $this->matchCollection->es();
}
if ($this->aggs) {
......
......@@ -5,22 +5,20 @@ namespace IQDEV\ElasticSearch;
use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastic\Elasticsearch\Exception\ServerResponseException;
use IQDEV\ElasticSearch\Converter\CriteriaToEsRequest;
use IQDEV\ElasticSearch\Converter\EsResponseToResult;
use IQDEV\ElasticSearch\Converter\Request\CriteriaToRequest;
use IQDEV\ElasticSearch\Converter\Result\EsResponseToResult;
use IQDEV\ElasticSearch\Criteria\Criteria;
class SearchService implements Searchable
{
private Client $esClient;
private Configuration $configuration;
private CriteriaToEsRequest $criteriaToEsRequest;
private CriteriaToRequest $criteriaToRequest;
private EsResponseToResult $esResponseToResult;
public function __construct(Client $esClient, Configuration $configuration)
{
$this->esClient = $esClient;
$this->configuration = $configuration;
$this->criteriaToEsRequest = new CriteriaToEsRequest();
public function __construct(
private Client $esClient,
private Configuration $configuration
) {
$this->criteriaToRequest = new CriteriaToRequest($this->configuration);
$this->esResponseToResult = new EsResponseToResult();
}
......@@ -30,13 +28,13 @@ class SearchService implements Searchable
*/
public function search(Criteria $criteria): Result
{
$request = $this->criteriaToEsRequest->fromCriteria($criteria);
$request = $this->criteriaToRequest->fromCriteria($criteria);
$response = $this->esClient->search([
'index' => $this->configuration->getIndexName(),
'body' => $request->es(),
]);
return $this->esResponseToResult->fromResponse($response);
return $this->esResponseToResult->fromResponse($response, $this->configuration);
}
}
......@@ -2,6 +2,8 @@
namespace IQDEV\ElasticSearch;
use IQDEV\ElasticSearch\Criteria\Criteria;
interface Searchable
{
/**
......