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; ...@@ -10,15 +10,13 @@ use IQDEV\ElasticSearch\Facet\FacetFactory;
class BaseIndexProvider implements IndexProvider class BaseIndexProvider implements IndexProvider
{ {
private array $products;
private ?int $size = null; private ?int $size = null;
private ?int $limit = null; private ?int $limit = null;
private Configuration $configuration;
public function __construct($products, $configuration) public function __construct(
{ private array $products,
$this->configuration = $configuration; private Configuration $configuration
$this->products = $products; ) {
} }
public function get(): \Generator public function get(): \Generator
......
...@@ -6,19 +6,11 @@ use IQDEV\ElasticSearch\Esable; ...@@ -6,19 +6,11 @@ use IQDEV\ElasticSearch\Esable;
final class BulkIndex implements Index final class BulkIndex 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
......
...@@ -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;
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 ...@@ -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\ElasticSearch\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)
......
...@@ -32,43 +32,34 @@ final class Query implements Esable ...@@ -32,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
...@@ -127,4 +118,34 @@ final class Query implements Esable ...@@ -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; ...@@ -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) {
......
...@@ -5,22 +5,20 @@ namespace IQDEV\ElasticSearch; ...@@ -5,22 +5,20 @@ namespace IQDEV\ElasticSearch;
use Elastic\Elasticsearch\Client; use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\Exception\ClientResponseException; use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastic\Elasticsearch\Exception\ServerResponseException; use Elastic\Elasticsearch\Exception\ServerResponseException;
use IQDEV\ElasticSearch\Converter\CriteriaToEsRequest; use IQDEV\ElasticSearch\Converter\Request\CriteriaToRequest;
use IQDEV\ElasticSearch\Converter\EsResponseToResult; use IQDEV\ElasticSearch\Converter\Result\EsResponseToResult;
use IQDEV\ElasticSearch\Criteria\Criteria;
class SearchService implements Searchable class SearchService implements Searchable
{ {
private Client $esClient; private CriteriaToRequest $criteriaToRequest;
private Configuration $configuration;
private CriteriaToEsRequest $criteriaToEsRequest;
private EsResponseToResult $esResponseToResult; private EsResponseToResult $esResponseToResult;
public function __construct(Client $esClient, Configuration $configuration) public function __construct(
{ private Client $esClient,
$this->esClient = $esClient; private Configuration $configuration
$this->configuration = $configuration; ) {
$this->criteriaToRequest = new CriteriaToRequest($this->configuration);
$this->criteriaToEsRequest = new CriteriaToEsRequest();
$this->esResponseToResult = new EsResponseToResult(); $this->esResponseToResult = new EsResponseToResult();
} }
...@@ -30,13 +28,13 @@ class SearchService implements Searchable ...@@ -30,13 +28,13 @@ class SearchService implements Searchable
*/ */
public function search(Criteria $criteria): Result public function search(Criteria $criteria): Result
{ {
$request = $this->criteriaToEsRequest->fromCriteria($criteria); $request = $this->criteriaToRequest->fromCriteria($criteria);
$response = $this->esClient->search([ $response = $this->esClient->search([
'index' => $this->configuration->getIndexName(), 'index' => $this->configuration->getIndexName(),
'body' => $request->es(), 'body' => $request->es(),
]); ]);
return $this->esResponseToResult->fromResponse($response); return $this->esResponseToResult->fromResponse($response, $this->configuration);
} }
} }
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
namespace IQDEV\ElasticSearch; namespace IQDEV\ElasticSearch;
use IQDEV\ElasticSearch\Criteria\Criteria;
interface Searchable interface Searchable
{ {
/** /**
......