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 4769 additions and 79 deletions
......@@ -11,6 +11,7 @@ final class Query implements Esable
protected BoolQueryCollection $filter;
protected BoolQueryCollection $should;
protected BoolQueryCollection $mustNot;
protected BoolQueryCollection $match;
protected string $type;
......@@ -31,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
......@@ -113,6 +105,7 @@ final class Query implements Esable
if (false === $this->should->isEmpty()) {
$bool['should'] = $this->should->es();
$bool['minimum_should_match'] = 1;
}
if (false === $this->match->isEmpty()) {
......@@ -125,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,24 +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\Search\Criteria;
use IQDEV\Search\Result;
use IQDEV\ElasticSearch\Converter\Request\CriteriaToRequest;
use IQDEV\ElasticSearch\Converter\Result\EsResponseToResult;
use IQDEV\ElasticSearch\Criteria\Criteria;
class SearchService implements \IQDEV\Search\SearchService
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();
}
......@@ -32,13 +28,13 @@ class SearchService implements \IQDEV\Search\SearchService
*/
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);
}
}
<?php
namespace IQDEV\ElasticSearch;
use IQDEV\ElasticSearch\Criteria\Criteria;
interface Searchable
{
/**
* @param Criteria $criteria
*
* @return Result
*/
public function search(Criteria $criteria): Result;
}
<?php
namespace IQDEV\ElasticSearchTests;
use IQDEV\ElasticSearchTests\Helpers\Arr;
use PHPUnit\Framework\TestCase;
abstract class AbstractTestCase extends TestCase
{
/**
* Частичное сравнение массивов
* @param array $expected
* @param array $actual массив который должен иметь структуру и значения $expected
* @param string $message
* @return void
*/
protected function assertArray(array $expected, array $actual, string $message = '')
{
$expectedKeys = Arr::dot($expected);
$actualKeys = Arr::dot($actual);
foreach ($expectedKeys as $key => $value) {
$this->assertArrayHasKey($key, $actualKeys, $message);
if (isset($actualKeys[$key])) {
$this->assertEquals($actualKeys[$key], $value, $key);
}
}
}
}
\ No newline at end of file
<?php
include_once __DIR__ . '/../bootstrap.php';
$oSeeder = new \IQDEV\ElasticSearchTests\Seed\DefaultSeed();
$oSeeder->start();
\ No newline at end of file
<?php
namespace IQDEV\ElasticSearchTests\Config;
use IQDEV\ElasticSearch\Config\BaseConfiguration;
class ChangingStateConfiguration extends BaseConfiguration
{
public function getIndexName(): string
{
return $_ENV['IQ_ES_PRODUCT_SEARCH_INDEX'] . '_changing-state';
}
}
\ No newline at end of file
<?php
namespace IQDEV\ElasticSearchTests\Factory;
use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\ClientBuilder;
class ClientFactory
{
private static $instance = null;
public static function create(): Client
{
if (self::$instance !== null) {
return self::$instance;
}
self::$instance = ClientBuilder::create()
->setHosts(explode(',', $_ENV['IQ_ES_HOSTS'] ?: 'http://localhost:9200'))
->setBasicAuthentication($_ENV['IQ_ES_USER'], $_ENV['IQ_ES_PASSWORD'])
->build();
return self::$instance;
}
}
\ No newline at end of file
<?php
namespace IQDEV\ElasticSearchTests\Filter;
use IQDEV\ElasticSearch\Criteria\Aggs\Aggs;
use IQDEV\ElasticSearch\Criteria\Criteria;
use IQDEV\ElasticSearch\Criteria\Filter\Collection\FilterGroupCollection;
use IQDEV\ElasticSearch\Criteria\Filter\Field;
use IQDEV\ElasticSearch\Criteria\Filter\Filter;
use IQDEV\ElasticSearch\Criteria\Filter\FilterOperator;
use IQDEV\ElasticSearch\Criteria\Filter\Value\FilterNumber;
use IQDEV\ElasticSearch\Criteria\Query\SearchQuery;
use IQDEV\ElasticSearch\Document\Property\Property;
use IQDEV\ElasticSearchTests\AbstractTestCase;
use IQDEV\ElasticSearchTests\Helpers\FormatData;
use IQDEV\ElasticSearchTests\Service\SearchClient;
/**
* Тестирование агрегирующих функций для прямых свойств документов
*/
class AggsPropsTest extends AbstractTestCase
{
public function testCategoryAggs()
{
$criteria = new Criteria();
$criteria->getAggs()->add(
new Aggs(
new Property('new')
)
);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
'h1',
'h2',
'h3',
'p1',
's1',
's2',
's3',
's4',
],
'facets' => [
0 => [
'code' => 'new',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 0,
'count' => 3,
'active' => true
],
1 => [
'label' => null,
'value' => 1,
'count' => 5,
'active' => true
]
],
'range' => []
]
],
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
public function testCategoryAggsWFilters()
{
$criteria = new Criteria();
$criteria->getAggs()->add(
new Aggs(
new Property('new')
)
);
$criteria->getAggs()->add(
new Aggs(
new Property('rating')
)
);
$filterCollectionPrice = new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::GTE,
new FilterNumber(105)
)
]);
$criteria->getFilters()->add($filterCollectionPrice);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
'h2',
'h3',
'p1'
],
'facets' => [
0 => [
'code' => 'brand',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'adidas',
'count' => 0,
'active' => false
],
1 => [
'label' => null,
'value' => 'nike',
'count' => 1,
'active' => true
],
3 => [
'label' => null,
'value' => 'rebook',
'count' => 2,
'active' => true
]
],
'range' => []
]
],
1 => [
'code' => 'color',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'blue',
'count' => 0,
'active' => false
],
1 => [
'label' => null,
'value' => 'green',
'count' => 0,
'active' => false
],
2 => [
'label' => null,
'value' => 'red',
'count' => 0,
'active' => false
],
3 => [
'label' => null,
'value' => 'white',
'count' => 3,
'active' => true
]
],
'range' => []
]
],
2 => [
'code' => 'size',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
"label" => null,
"value" => "43",
"count" => 0,
"active" => false
],
1 => [
"label" => null,
"value" => "46",
"count" => 0,
"active" => false
],
2 => [
"label" => null,
"value" => "47",
"count" => 0,
"active" => false
],
3 => [
"label" => null,
"value" => "xl",
"count" => 2,
"active" => true
],
4 => [
"label" => null,
"value" => "xxl",
"count" => 1,
"active" => true
],
],
'range' => []
]
],
3 => [
'code' => 'price',
'label' => null,
'type' => 'range',
'items' => [
'list' => [],
'range' => [
0 => [
'label' => null,
'count' => 3,
'active' => true,
'fullRange' => [
'min' => 100.0,
'max' => 107.0
],
'activeRange' => [
'min' => 105.0,
'max' => 107.0
]
]
]
],
],
4 => [
'code' => 'new',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => "0",
'count' => 1,
'active' => true
],
1 => [
'label' => null,
'value' => "1",
'count' => 2,
'active' => true
]
],
'range' => []
]
],
5 => [
'code' => 'rating',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => "3",
'count' => 3,
'active' => true
],
],
'range' => []
]
],
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
}
<?php
namespace IQDEV\ElasticSearchTests\Filter;
use IQDEV\ElasticSearch\Criteria\Criteria;
use IQDEV\ElasticSearch\Criteria\Filter\Collection\FilterGroupCollection;
use IQDEV\ElasticSearch\Criteria\Filter\Field;
use IQDEV\ElasticSearch\Criteria\Filter\Filter;
use IQDEV\ElasticSearch\Criteria\Filter\FilterOperator;
use IQDEV\ElasticSearch\Criteria\Filter\LogicOperator;
use IQDEV\ElasticSearch\Criteria\Filter\Value\FilterKeyword;
use IQDEV\ElasticSearch\Criteria\Filter\Value\FilterNumber;
use IQDEV\ElasticSearch\Criteria\Query\SearchQuery;
use IQDEV\ElasticSearch\Criteria\Search\Search;
use IQDEV\ElasticSearch\Document\Property\Property;
use IQDEV\ElasticSearchTests\AbstractTestCase;
use IQDEV\ElasticSearchTests\Helpers\FormatData;
use IQDEV\ElasticSearchTests\Service\SearchClient;
/**
* Тестирование агрегирующих функций
*/
class AggsTest extends AbstractTestCase
{
public function testEmptyFilterByCategory()
{
$criteria = new Criteria();
$criteria->getSearch()->add(
new Search(
new Property('category_id'),
't-short',
),
);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
'h1',
'h2',
'h3'
],
'facets' => [
0 => [
'code' => 'brand',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'nike',
'count' => 2,
'active' => true
],
1 => [
'label' => null,
'value' => 'rebook',
'count' => 1,
'active' => true
]
],
'range' => []
]
],
1 => [
'code' => 'color',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'red',
'count' => 1,
'active' => true
],
1 => [
'label' => null,
'value' => 'white',
'count' => 2,
'active' => true
]
],
'range' => []
]
],
2 => [
'code' => 'size',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'xl',
'count' => 2,
'active' => true
],
1 => [
'label' => null,
'value' => 'xxl',
'count' => 1,
'active' => true
]
],
'range' => []
]
],
3 => [
'code' => 'price',
'label' => null,
'type' => 'range',
'items' => [
'list' => [],
'range' => [
0 => [
'label' => null,
'count' => 3,
'active' => true,
'fullRange' => [
'min' => 104.0,
'max' => 106.0
],
'activeRange' => [
'min' => 104.0,
'max' => 106.0
]
]
]
]
]
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
public function testEmptyKeywordFilter()
{
$criteria = new Criteria();
$criteria->getSearch()->add(
new Search(
new Property('category_id'),
't-short',
),
);
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('color'),
FilterOperator::EQ,
new FilterKeyword('white')
)
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
'h2',
'h3'
],
'facets' => [
0 => [
'code' => 'brand',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'nike',
'count' => 1,
'active' => true
],
1 => [
'label' => null,
'value' => 'rebook',
'count' => 1,
'active' => true
]
],
'range' => []
]
],
1 => [
'code' => 'color',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'red',
'count' => 1,
'active' => true
],
1 => [
'label' => null,
'value' => 'white',
'count' => 2,
'active' => true
]
],
'range' => []
]
],
2 => [
'code' => 'size',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'xl',
'count' => 1,
'active' => true
],
1 => [
'label' => null,
'value' => 'xxl',
'count' => 1,
'active' => true
]
],
'range' => []
]
],
3 => [
'code' => 'price',
'label' => null,
'type' => 'range',
'items' => [
'list' => [],
'range' => [
0 => [
'label' => null,
'count' => 2,
'active' => true,
'fullRange' => [
'min' => 104.0,
'max' => 106.0
],
'activeRange' => [
'min' => 105.0,
'max' => 106.0
]
]
]
]
]
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
public function testRangeFilter()
{
$criteria = new Criteria();
$criteria->getSearch()->add(
new Search(
new Property('category_id'),
't-short',
),
);
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::LTE,
new FilterNumber(104.50)
)
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
'h1',
],
'facets' => [
0 => [
'code' => 'brand',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'nike',
'count' => 1,
'active' => true
],
1 => [
'label' => null,
'value' => 'rebook',
'count' => 0,
'active' => false
]
],
'range' => []
]
],
1 => [
'code' => 'color',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'red',
'count' => 1,
'active' => true
],
1 => [
'label' => null,
'value' => 'white',
'count' => 0,
'active' => false
]
],
'range' => []
]
],
2 => [
'code' => 'size',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'xl',
'count' => 1,
'active' => true
],
1 => [
'label' => null,
'value' => 'xxl',
'count' => 0,
'active' => false
]
],
'range' => []
]
],
3 => [
'code' => 'price',
'label' => null,
'type' => 'range',
'items' => [
'list' => [],
'range' => [
0 => [
'label' => null,
'count' => 1,
'active' => true,
'fullRange' => [
'min' => 104.0,
'max' => 106.0
],
'activeRange' => [
'min' => 104.0,
'max' => 104.0
]
]
]
]
]
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
public function testCombineFilter()
{
$criteria = new Criteria();
$criteria->getSearch()->add(
new Search(
new Property('category_id'),
't-short',
),
);
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('color'),
FilterOperator::EQ,
new FilterKeyword('black')
)
]));
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::LTE,
new FilterNumber(104)
)
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [],
'facets' => [
0 => [
'code' => 'brand',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'nike',
'count' => 0,
'active' => false
],
1 => [
'label' => null,
'value' => 'rebook',
'count' => 0,
'active' => false
]
],
'range' => []
]
],
1 => [
'code' => 'color',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'red',
'count' => 1,
'active' => true
],
1 => [
'label' => null,
'value' => 'white',
'count' => 0,
'active' => false
]
],
'range' => []
]
],
2 => [
'code' => 'size',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'xl',
'count' => 0,
'active' => false
],
1 => [
'label' => null,
'value' => 'xxl',
'count' => 0,
'active' => false
]
],
'range' => []
]
],
3 => [
'code' => 'price',
'label' => null,
'type' => 'range',
'items' => [
'list' => [],
'range' => [
0 => [
'label' => null,
'count' => 0,
'active' => false,
'fullRange' => [
'min' => 104.0,
'max' => 106.0
],
'activeRange' => [
'min' => null,
'max' => null
]
]
]
]
]
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
public function testCombineFilterTwo()
{
$criteria = new Criteria();
$criteria->getSearch()->add(
new Search(
new Property('category_id'),
't-short',
),
);
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('color'),
FilterOperator::EQ,
new FilterKeyword('red')
)
]));
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::LTE,
new FilterNumber(105)
)
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
'h1'
],
'facets' => [
0 => [
'code' => 'brand',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'nike',
'count' => 1,
'active' => true
],
1 => [
'label' => null,
'value' => 'rebook',
'count' => 0,
'active' => false
]
],
'range' => []
]
],
1 => [
'code' => 'color',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'red',
'count' => 1,
'active' => true
],
1 => [
'label' => null,
'value' => 'white',
'count' => 1,
'active' => true
]
],
'range' => []
]
],
2 => [
'code' => 'size',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'xl',
'count' => 1,
'active' => true
],
1 => [
'label' => null,
'value' => 'xxl',
'count' => 0,
'active' => false
]
],
'range' => []
]
],
3 => [
'code' => 'price',
'label' => null,
'type' => 'range',
'items' => [
'list' => [],
'range' => [
0 => [
'label' => null,
'count' => 1,
'active' => true,
'fullRange' => [
'min' => 104.0,
'max' => 106.0
],
'activeRange' => [
'min' => 104.0,
'max' => 104.0
]
]
]
]
]
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
public function testKeywordFilter()
{
$criteria = new Criteria();
$criteria->getSearch()->add(
new Search(
new Property('category_id'),
't-short',
),
);
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('color'),
FilterOperator::EQ,
new FilterKeyword('white')
)
]));
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('nike')
)
]));
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::GT,
new FilterNumber(100)
)
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
'h2'
],
'facets' => [
0 => [
'code' => 'brand',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'nike',
'count' => 1,
'active' => true
],
1 => [
'label' => null,
'value' => 'rebook',
'count' => 1,
'active' => true
]
],
'range' => []
]
],
1 => [
'code' => 'color',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'red',
'count' => 1,
'active' => true
],
1 => [
'label' => null,
'value' => 'white',
'count' => 1,
'active' => true
]
],
'range' => []
]
],
2 => [
'code' => 'size',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'xl',
'count' => 0,
'active' => false
],
1 => [
'label' => null,
'value' => 'xxl',
'count' => 1,
'active' => true
]
],
'range' => []
]
],
3 => [
'code' => 'price',
'label' => null,
'type' => 'range',
'items' => [
'list' => [],
'range' => [
0 => [
'label' => null,
'count' => 1,
'active' => true,
'fullRange' => [
'min' => 104.0,
'max' => 106.0
],
'activeRange' => [
'min' => 105.0,
'max' => 105.0
]
]
]
]
]
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
public function testKeywordFilterTwo()
{
$criteria = new Criteria();
$criteria->getSearch()->add(
new Search(
new Property('category_id'),
't-short',
),
);
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('color'),
FilterOperator::EQ,
new FilterKeyword('white')
)
]));
$criteria->getFilters()->add(
(new FilterGroupCollection([
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('nike')
),
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('reebok')
)
]))
->setLogicOperator(LogicOperator::OR)
);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
'h2'
],
'facets' => [
0 => [
'code' => 'brand',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'nike',
'count' => 1,
'active' => true
],
1 => [
'label' => null,
'value' => 'rebook',
'count' => 1,
'active' => true
]
],
'range' => []
]
],
1 => [
'code' => 'color',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'red',
'count' => 1,
'active' => true
],
1 => [
'label' => null,
'value' => 'white',
'count' => 1,
'active' => true
]
],
'range' => []
]
],
2 => [
'code' => 'size',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'xl',
'count' => 0,
'active' => false
],
1 => [
'label' => null,
'value' => 'xxl',
'count' => 1,
'active' => true
]
],
'range' => []
]
],
3 => [
'code' => 'price',
'label' => null,
'type' => 'range',
'items' => [
'list' => [],
'range' => [
0 => [
'label' => null,
'count' => 1,
'active' => true,
'fullRange' => [
'min' => 104.0,
'max' => 106.0
],
'activeRange' => [
'min' => 105.0,
'max' => 105.0
]
]
]
]
]
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
}
\ No newline at end of file
<?php
namespace IQDEV\ElasticSearchTests\Filter;
use IQDEV\ElasticSearch\Criteria\Criteria;
use IQDEV\ElasticSearch\Criteria\Filter\Collection\FilterGroupCollection;
use IQDEV\ElasticSearch\Criteria\Filter\Field;
use IQDEV\ElasticSearch\Criteria\Filter\Filter;
use IQDEV\ElasticSearch\Criteria\Filter\FilterOperator;
use IQDEV\ElasticSearch\Criteria\Filter\Value\FilterKeyword;
use IQDEV\ElasticSearch\Criteria\Filter\Value\FilterNumber;
use IQDEV\ElasticSearch\Criteria\Query\SearchQuery;
use IQDEV\ElasticSearchTests\AbstractTestCase;
use IQDEV\ElasticSearchTests\Helpers\FormatData;
use IQDEV\ElasticSearchTests\Service\SearchClient;
class CommonRangeKeywordsTest extends AbstractTestCase
{
/**
* Поиск элементов по фильтру свойства и цене
*
* @return void
*/
public function testExistByFilterAndMinPrice(): void
{
$criteria = new Criteria();
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('color'),
FilterOperator::EQ,
new FilterKeyword('red')
)
]));
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::GT,
new FilterNumber(102)
)
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
'h1'
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatData($result));
}
/**
* Поиск элементов по фильтру свойства и цене
*
* @return void
*/
public function testExistByFilterAndMaxPrice(): void
{
$criteria = new Criteria();
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('color'),
FilterOperator::EQ,
new FilterKeyword('red')
)
]));
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::LT,
new FilterNumber(102)
)
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
's2'
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatData($result));
}
/**
* Поиск элементов по фильтру свойства и цене
*
* @return void
*/
public function testExistByFilterAndRangePrice(): void
{
$criteria = new Criteria();
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('color'),
FilterOperator::EQ,
new FilterKeyword('red')
)
]));
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::GTE,
new FilterNumber(101)
),
new Filter(
new Field('price'),
FilterOperator::LTE,
new FilterNumber(104)
),
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
'h1',
's2'
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatData($result));
}
}
\ No newline at end of file
<?php
namespace IQDEV\ElasticSearchTests\Filter;
use Elastic\Elasticsearch\Client;
use IQDEV\ElasticSearch\Configuration;
use IQDEV\ElasticSearch\Converter\Result\EsResponseToResult;
use IQDEV\ElasticSearch\Indexer\IndexRunner;
use IQDEV\ElasticSearchTests\AbstractTestCase;
use IQDEV\ElasticSearchTests\Config\ChangingStateConfiguration;
use IQDEV\ElasticSearchTests\Factory\ClientFactory;
use IQDEV\ElasticSearchTests\Helpers\FormatData;
use IQDEV\ElasticSearchTests\Helpers\TestIndexProvider;
use Psr\Log\NullLogger;
class IndexesTest extends AbstractTestCase
{
private array $product = [
'id' => 'test',
'name' => 'Test товар',
'category' => 'indexes',
'properties' => [
'prop1' => 'value1',
'prop2' => 'value2',
'prop3' => 'value3',
]
];
private IndexRunner $indexRunner;
private Client $esClient;
private Configuration $configuration;
public function __construct(?string $name = null, array $data = [], $dataName = '')
{
parent::__construct($name, $data, $dataName);
$this->configuration = new ChangingStateConfiguration();
$this->esClient = ClientFactory::create();
$this->indexRunner = new IndexRunner(
$this->esClient,
$this->configuration,
new NullLogger()
);
}
public function testUpdate()
{
$indexProvider = new TestIndexProvider($this->configuration, [$this->product]);
$this->indexRunner->run($indexProvider);
$updateData = [
'id' => $this->product['id'],
'category' => $this->product['category'],
'type' => 'update',
'name' => 'Обновленный элемент'
];
$indexProvider = new TestIndexProvider($this->configuration, [$updateData]);
$this->indexRunner->run($indexProvider);
$response = $this->esClient->search([
'index' => $this->configuration->getIndexName(),
'body' => [
'query' => [
'match' => [
'_id' => $this->product['id']
],
]
]
]);
$esResponseToResult = new EsResponseToResult();
$result = $esResponseToResult->fromResponse($response, $this->configuration);
unset($updateData['type']);
$expected = [
'products' => [
array_merge($this->product, $updateData)
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataProducts($result));
}
}
<?php
namespace IQDEV\ElasticSearchTests\Filter;
use IQDEV\ElasticSearch\Criteria\Criteria;
use IQDEV\ElasticSearch\Criteria\Filter\Collection\FilterGroupCollection;
use IQDEV\ElasticSearch\Criteria\Filter\Field;
use IQDEV\ElasticSearch\Criteria\Filter\Filter;
use IQDEV\ElasticSearch\Criteria\Filter\FilterOperator;
use IQDEV\ElasticSearch\Criteria\Filter\Value\FilterKeyword;
use IQDEV\ElasticSearch\Criteria\Query\SearchQuery;
use IQDEV\ElasticSearch\Criteria\Search\Search;
use IQDEV\ElasticSearch\Document\Property\Property;
use IQDEV\ElasticSearchTests\AbstractTestCase;
use IQDEV\ElasticSearchTests\Helpers\FormatData;
use IQDEV\ElasticSearchTests\Service\SearchClient;
class KeywordsTest extends AbstractTestCase
{
/**
* Поиск элементов по пустому фильтру свойства
*
* @return void
*/
public function testExistByEmptyFilter(): void
{
$criteria = new Criteria();
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('color'),
FilterOperator::EQ,
new FilterKeyword('')
)
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => []
];
$this->assertEquals($expected, FormatData::formatData($result));
}
/**
* Поиск элементов по фильтру свойства
*
* @return void
*/
public function testExistByFilter(): void
{
$criteria = new Criteria();
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('color'),
FilterOperator::EQ,
new FilterKeyword('red')
)
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
's2',
'h1'
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatData($result));
}
/**
* Поиск элементов по фильтру свойства и категории
*
* @return void
*/
public function testExistByFilterAndCategory(): void
{
$criteria = new Criteria();
$criteria->getSearch()->add(
new Search(
new Property('category_id'),
'shoes',
),
);
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('color'),
FilterOperator::EQ,
new FilterKeyword('red')
)
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
's2',
]
];
$this->assertEquals($expected, FormatData::formatData($result));
}
/**
* Поиск элементов по нескольким свойствам
*
* @return void
*/
public function testExistByMultipleFilter(): void
{
$criteria = new Criteria();
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('color'),
FilterOperator::EQ,
new FilterKeyword('red')
),
new Filter(
new Field('size'),
FilterOperator::EQ,
new FilterKeyword('xxl')
)
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => []
];
$this->assertEquals($expected, FormatData::formatData($result));
}
/**
* Поиск элементов по нескольким свойствам
*
* @return void
*/
public function testExistByMultipleFilter2(): void
{
$criteria = new Criteria();
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('color'),
FilterOperator::EQ,
new FilterKeyword('red')
),
new Filter(
new Field('size'),
FilterOperator::EQ,
new FilterKeyword('xl')
)
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
'h1'
]
];
$this->assertEquals($expected, FormatData::formatData($result));
}
/**
* Поиск элементов по нескольким свойствам и категории
*
* @return void
*/
public function testExistByMultipleFilterAndCategory(): void
{
$criteria = new Criteria();
$criteria->getSearch()->add(
new Search(
new Property('category_id'),
'prices',
),
);
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('color'),
FilterOperator::EQ,
new FilterKeyword('red')
),
new Filter(
new Field('size'),
FilterOperator::EQ,
new FilterKeyword('xl')
)
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => []
];
$this->assertEquals($expected, FormatData::formatData($result));
}
}
\ No newline at end of file
<?php
namespace IQDEV\ElasticSearchTests\Filter;
use IQDEV\ElasticSearch\Criteria\Criteria;
use IQDEV\ElasticSearch\Criteria\Filter\Collection\FilterGroupCollection;
use IQDEV\ElasticSearch\Criteria\Filter\Field;
use IQDEV\ElasticSearch\Criteria\Filter\Filter;
use IQDEV\ElasticSearch\Criteria\Filter\FilterOperator;
use IQDEV\ElasticSearch\Criteria\Filter\FilterType;
use IQDEV\ElasticSearch\Criteria\Filter\LogicOperator;
use IQDEV\ElasticSearch\Criteria\Filter\Value\FilterKeyword;
use IQDEV\ElasticSearch\Criteria\Filter\Value\FilterNumber;
use IQDEV\ElasticSearch\Criteria\Query\SearchQuery;
use IQDEV\ElasticSearchTests\AbstractTestCase;
use IQDEV\ElasticSearchTests\Helpers\FormatData;
use IQDEV\ElasticSearchTests\Service\SearchClient;
class QueryAndPostFilterTest extends AbstractTestCase
{
/**
* query по не сущ бренду
*/
public function testQueryNonExistentBrand()
{
$criteria = new Criteria();
$filterCollectionBrand = new FilterGroupCollection([
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('not')
)
]);
$filterCollectionBrand->setFilterType(FilterType::QUERY);
$criteria->getFilters()->add($filterCollectionBrand);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [],
'facets' => []
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
/**
* query по сущ бренду
* @return void
*/
public function testQueryExistingBrand()
{
$criteria = new Criteria();
$filterCollectionBrand = new FilterGroupCollection([
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('adidas')
)
]);
$filterCollectionBrand->setFilterType(FilterType::QUERY);
$criteria->getFilters()->add($filterCollectionBrand);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
's1',
's2',
],
'facets' => [
0 => [
'code' => 'brand',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'adidas',
'count' => 2,
'active' => true,
]
],
'range' => [],
]
],
1 => [
'code' => 'color',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'green',
'count' => 1,
'active' => true,
],
1 => [
'label' => null,
'value' => 'red',
'count' => 1,
'active' => true,
]
],
'range' => [],
]
],
2 => [
'code' => 'size',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => '46',
'count' => 1,
'active' => true,
],
1 => [
'label' => null,
'value' => '47',
'count' => 1,
'active' => true,
]
],
'range' => [],
]
],
3 => [
'code' => 'price',
'label' => null,
'type' => 'range',
'items' => [
'list' => [],
'range' => [
0 => [
'label' => null,
'count' => 2,
'active' => true,
'fullRange' => [
'min' => 100.0,
'max' => 101.0,
],
'activeRange' => [
'min' => 100.0,
'max' => 101.0,
]
]
]
]
]
],
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
/**
* query по сущ бренду и post_filter по бренду
* @return void
*/
public function testQueryExistingBrandAndPostFilterExistingBrand()
{
$criteria = new Criteria();
$filterCollectionBrand = new FilterGroupCollection([
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('adidas')
),
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('nike')
),
]);
$filterCollectionBrand
->setLogicOperator(LogicOperator::OR)
->setFilterType(FilterType::QUERY);
$criteria->getFilters()->add($filterCollectionBrand);
$filterCollectionBrand = new FilterGroupCollection([
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('nike')
),
]);
$criteria->getFilters()->add($filterCollectionBrand);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
's4',
'h1',
'h2',
],
'facets' => [
0 => [
'code' => 'brand',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'adidas',
'count' => 2,
'active' => true,
],
1 => [
'label' => null,
'value' => 'nike',
'count' => 3,
'active' => true,
]
],
'range' => [],
]
],
1 => [
'code' => 'color',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'green',
'count' => 1,
'active' => true,
],
1 => [
'label' => null,
'value' => 'red',
'count' => 1,
'active' => true,
],
2 => [
'label' => null,
'value' => 'white',
'count' => 1,
'active' => true,
]
],
'range' => [],
]
],
2 => [
'code' => 'size',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => '43',
'count' => 1,
'active' => true,
],
1 => [
'label' => null,
'value' => '46',
'count' => 0,
'active' => false,
],
2 => [
'label' => null,
'value' => '47',
'count' => 0,
'active' => false,
],
3 => [
'label' => null,
'value' => 'xl',
'count' => 1,
'active' => true,
],
4 => [
'label' => null,
'value' => 'xxl',
'count' => 1,
'active' => true,
]
],
'range' => [],
],
],
3 => [
'code' => 'price',
'label' => null,
'type' => 'range',
'items' => [
'list' => [],
'range' => [
0 => [
'label' => null,
'count' => 3,
'active' => true,
'fullRange' => [
'min' => 100.0,
'max' => 105.0,
],
'activeRange' => [
'min' => 103.0,
'max' => 105.0,
]
]
]
]
]
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
/**
* query по сущ бренду и post_filter по цвету
* @return void
*/
public function testQueryExistingBrandAndPostFilterExistingColor()
{
$criteria = new Criteria();
$filterCollectionBrand = new FilterGroupCollection([
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('nike')
),
]);
$filterCollectionBrand
->setLogicOperator(LogicOperator::OR)
->setFilterType(FilterType::QUERY);
$criteria->getFilters()->add($filterCollectionBrand);
$filterCollectionColor = new FilterGroupCollection([
new Filter(
new Field('color'),
FilterOperator::EQ,
new FilterKeyword('white')
),
]);
$criteria->getFilters()->add($filterCollectionColor);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
'h2',
],
'facets' => [
0 => [
'code' => 'brand',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'nike',
'count' => 1,
'active' => true,
]
],
'range' => [],
]
],
1 => [
'code' => 'color',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'green',
'count' => 1,
'active' => true,
],
1 => [
'label' => null,
'value' => 'red',
'count' => 1,
'active' => true,
],
2 => [
'label' => null,
'value' => 'white',
'count' => 1,
'active' => true,
]
],
'range' => [],
]
],
2 => [
'code' => 'size',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => '43',
'count' => 0,
'active' => false,
],
1 => [
'label' => null,
'value' => 'xl',
'count' => 0,
'active' => false,
],
2 => [
'label' => null,
'value' => 'xxl',
'count' => 1,
'active' => true,
]
],
'range' => [],
]
],
3 => [
'code' => 'price',
'label' => null,
'type' => 'range',
'items' => [
'list' => [],
'range' => [
0 => [
'label' => null,
'count' => 1,
'active' => true,
'fullRange' => [
'min' => 103.0,
'max' => 105.0,
],
'activeRange' => [
'min' => 105.0,
'max' => 105.0,
]
]
]
]
]
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
/**
* query по сущ бренду и цвету и post_filter по бренду
* @return void
*/
public function testQueryExistingBrandAndColorAndPostFilterExistingColor()
{
$criteria = new Criteria();
$filterCollectionBrand = new FilterGroupCollection([
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('nike')
),
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('rebook')
),
]);
$filterCollectionBrand
->setLogicOperator(LogicOperator::OR)
->setFilterType(FilterType::QUERY);
$criteria->getFilters()->add($filterCollectionBrand);
$filterCollectionColor = new FilterGroupCollection([
new Filter(
new Field('color'),
FilterOperator::EQ,
new FilterKeyword('white')
),
]);
$filterCollectionColor->setFilterType(FilterType::QUERY);
$criteria->getFilters()->add($filterCollectionColor);
$filterCollectionBrand = new FilterGroupCollection([
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('nike')
),
]);
$criteria->getFilters()->add($filterCollectionBrand);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
'h2',
],
'facets' => [
0 => [
'code' => 'brand',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'nike',
'count' => 1,
'active' => true,
],
1 => [
'label' => null,
'value' => 'rebook',
'count' => 2,
'active' => true,
]
],
'range' => []
]
],
1 => [
'code' => 'color',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'white',
'count' => 1,
'active' => true,
]
],
'range' => []
]
],
2 => [
'code' => 'size',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'xl',
'count' => 0,
'active' => false,
],
1 => [
'label' => null,
'value' => 'xxl',
'count' => 1,
'active' => true,
]
],
'range' => []
]
],
3 => [
'code' => 'price',
'label' => null,
'type' => 'range',
'items' => [
'list' => [],
'range' => [
0 => [
'label' => null,
'count' => 1,
'active' => true,
'fullRange' => [
'min' => 105.0,
'max' => 107.0,
],
'activeRange' => [
'min' => 105.0,
'max' => 105.0,
]
]
]
]
]
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
/**
* query по сущ бренду и post_filter по бренду и цвету
* @return void
*/
public function testQueryExistingBrandAndPostFilterExistingBrandAndColor()
{
$criteria = new Criteria();
$filterCollectionBrand = new FilterGroupCollection([
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('nike')
),
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('rebook')
),
]);
$filterCollectionBrand
->setLogicOperator(LogicOperator::OR)
->setFilterType(FilterType::QUERY);
$criteria->getFilters()->add($filterCollectionBrand);
$filterCollectionBrand = new FilterGroupCollection([
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('nike')
),
]);
$criteria->getFilters()->add($filterCollectionBrand);
$filterCollectionColor = new FilterGroupCollection([
new Filter(
new Field('color'),
FilterOperator::EQ,
new FilterKeyword('red')
),
]);
$criteria->getFilters()->add($filterCollectionColor);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
'h1',
],
'facets' => [
0 => [
'code' => 'brand',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'nike',
'count' => 1,
'active' => true,
],
1 => [
'label' => null,
'value' => 'rebook',
'count' => 0,
'active' => false,
]
],
'range' => []
]
],
1 => [
'code' => 'color',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'blue',
'count' => 0,
'active' => false,
],
1 => [
'label' => null,
'value' => 'green',
'count' => 1,
'active' => true,
],
2 => [
'label' => null,
'value' => 'red',
'count' => 1,
'active' => true,
],
3 => [
'label' => null,
'value' => 'white',
'count' => 1,
'active' => true,
]
],
'range' => []
]
],
2 => [
'code' => 'size',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => '43',
'count' => 0,
'active' => false,
],
1 => [
'label' => null,
'value' => '47',
'count' => 0,
'active' => false,
],
2 => [
'label' => null,
'value' => 'xl',
'count' => 1,
'active' => true,
],
3 => [
'label' => null,
'value' => 'xxl',
'count' => 0,
'active' => false,
]
],
'range' => []
]
],
3 => [
'code' => 'price',
'label' => null,
'type' => 'range',
'items' => [
'list' => [],
'range' => [
0 => [
'label' => null,
'count' => 1,
'active' => true,
'fullRange' => [
'min' => 102.0,
'max' => 107.0,
],
'activeRange' => [
'min' => 104.0,
'max' => 104.0,
]
]
]
]
]
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
/**
* query по не сущ цене
* @return void
*/
public function testQueryNonExistentPrice()
{
$criteria = new Criteria();
$filterCollectionPrice = new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::GTE,
new FilterNumber(108)
)
]);
$filterCollectionPrice->setFilterType(FilterType::QUERY);
$criteria->getFilters()->add($filterCollectionPrice);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [],
'facets' => [],
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
/**
* query по сущ цене
* @return void
*/
public function testQueryExistingPrice()
{
$criteria = new Criteria();
$filterCollectionPrice = new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::GTE,
new FilterNumber(105)
)
]);
$filterCollectionPrice->setFilterType(FilterType::QUERY);
$criteria->getFilters()->add($filterCollectionPrice);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
'h2',
'h3',
'p1',
],
'facets' => [
0 => [
'code' => 'brand',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'nike',
'count' => 1,
'active' => true,
],
1 => [
'label' => null,
'value' => 'rebook',
'count' => 2,
'active' => true,
]
],
'range' => [],
]
],
1 => [
'code' => 'color',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'white',
'count' => 3,
'active' => true,
]
],
'range' => []
]
],
2 => [
'code' => 'size',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'xl',
'count' => 2,
'active' => true,
],
1 => [
'label' => null,
'value' => 'xxl',
'count' => 1,
'active' => true,
]
],
'range' => []
]
],
3 => [
'code' => 'price',
'label' => null,
'type' => 'range',
'items' => [
'list' => [],
'range' => [
0 => [
'label' => null,
'count' => 3,
'active' => true,
'fullRange' => [
'min' => 105.0,
'max' => 107.0,
],
'activeRange' => [
'min' => 105.0,
'max' => 107.0,
]
]
]
]
]
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
/**
* query по сущ цене и бренду
* @return void
*/
public function testQueryExistingPriceAndBrand()
{
$criteria = new Criteria();
$filterCollection = new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::GTE,
new FilterNumber(104)
),
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('nike')
),
]);
$filterCollection->setFilterType(FilterType::QUERY);
$criteria->getFilters()->add($filterCollection);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
'h1',
'h2',
],
'facets' => [
0 => [
'code' => 'brand',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'nike',
'count' => 2,
'active' => true,
]
],
'range' => []
]
],
1 => [
'code' => 'color',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'red',
'count' => 1,
'active' => true,
],
1 => [
'label' => null,
'value' => 'white',
'count' => 1,
'active' => true,
]
],
'range' => []
]
],
2 => [
'code' => 'size',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'xl',
'count' => 1,
'active' => true,
],
1 => [
'label' => null,
'value' => 'xxl',
'count' => 1,
'active' => true,
]
],
'range' => []
]
],
3 => [
'code' => 'price',
'label' => null,
'type' => 'range',
'items' => [
'list' => [],
'range' => [
0 => [
'label' => null,
'count' => 2,
'active' => true,
'fullRange' => [
'min' => 104.0,
'max' => 105.0,
],
'activeRange' => [
'min' => 104.0,
'max' => 105.0,
]
]
]
]
]
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
/**
* query по сущ цене и бренду и post_filter по цвету
* @return void
*/
public function testQueryExistingPriceAndBrandAndPostFilterColor()
{
$criteria = new Criteria();
$filterCollection = new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::GTE,
new FilterNumber(104)
),
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('nike')
),
]);
$filterCollection->setFilterType(FilterType::QUERY);
$criteria->getFilters()->add($filterCollection);
$filterCollectionColor = new FilterGroupCollection([
new Filter(
new Field('color'),
FilterOperator::EQ,
new FilterKeyword('red')
),
]);
$criteria->getFilters()->add($filterCollectionColor);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
'h1',
],
'facets' => [
0 => [
'code' => 'brand',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'nike',
'count' => 1,
'active' => true,
]
],
'range' => []
]
],
1 => [
'code' => 'color',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'red',
'count' => 1,
'active' => true,
],
1 => [
'label' => null,
'value' => 'white',
'count' => 1,
'active' => true,
]
],
'range' => []
]
],
2 => [
'code' => 'size',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'xl',
'count' => 1,
'active' => true,
],
1 => [
'label' => null,
'value' => 'xxl',
'count' => 0,
'active' => false,
]
],
'range' => []
]
],
3 => [
'code' => 'price',
'label' => null,
'type' => 'range',
'items' => [
'list' => [],
'range' => [
0 => [
'label' => null,
'count' => 1,
'active' => true,
'fullRange' => [
'min' => 104.0,
'max' => 105.0,
],
'activeRange' => [
'min' => 104.0,
'max' => 104.0,
]
]
]
]
]
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
/**
* query по сущ цене и бренду и post_filter по цвету и бренду
* @return void
*/
public function testQueryExistingPriceAndBrandAndPostFilterColorAndBrand()
{
$criteria = new Criteria();
$filterCollectionPrice = new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::LTE,
new FilterNumber(104)
),
]);
$filterCollectionPrice->setFilterType(FilterType::QUERY);
$criteria->getFilters()->add($filterCollectionPrice);
$filterCollectionBrand = new FilterGroupCollection([
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('nike')
),
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('adidas')
),
]);
$filterCollectionBrand
->setLogicOperator(LogicOperator::OR)
->setFilterType(FilterType::QUERY);
$criteria->getFilters()->add($filterCollectionBrand);
$filterCollectionColor = new FilterGroupCollection([
new Filter(
new Field('color'),
FilterOperator::EQ,
new FilterKeyword('green')
),
]);
$criteria->getFilters()->add($filterCollectionColor);
$filterCollectionBrand = new FilterGroupCollection([
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('nike')
),
]);
$criteria->getFilters()->add($filterCollectionBrand);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
's4',
],
'facets' => [
0 => [
'code' => 'brand',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'adidas',
'count' => 1,
'active' => true,
],
1 => [
'label' => null,
'value' => 'nike',
'count' => 1,
'active' => true,
]
],
'range' => []
]
],
1 => [
'code' => 'color',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'green',
'count' => 1,
'active' => true,
],
1 => [
'label' => null,
'value' => 'red',
'count' => 1,
'active' => true,
]
],
'range' => []
]
],
2 => [
'code' => 'size',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => '43',
'count' => 1,
'active' => true,
],
1 => [
'label' => null,
'value' => '46',
'count' => 0,
'active' => false,
],
2 => [
'label' => null,
'value' => '47',
'count' => 0,
'active' => false,
],
3 => [
'label' => null,
'value' => 'xl',
'count' => 0,
'active' => false,
]
],
'range' => []
]
],
3 => [
'code' => 'price',
'label' => null,
'type' => 'range',
'items' => [
'list' => [],
'range' => [
0 => [
'label' => null,
'count' => 1,
'active' => true,
'fullRange' => [
'min' => 100.0,
'max' => 104.0,
],
'activeRange' => [
'min' => 103.0,
'max' => 103.0,
]
]
]
]
]
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
/**
* query по сущ цене и бренду и post_filter по цене
* @return void
*/
public function testQueryExistingPriceAndBrandAndPostFilterPrice()
{
$criteria = new Criteria();
$filterCollectionPrice = new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::LTE,
new FilterNumber(104)
),
]);
$filterCollectionPrice->setFilterType(FilterType::QUERY);
$criteria->getFilters()->add($filterCollectionPrice);
$filterCollectionBrand = new FilterGroupCollection([
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('nike')
),
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('adidas')
),
]);
$filterCollectionBrand
->setLogicOperator(LogicOperator::OR)
->setFilterType(FilterType::QUERY);
$criteria->getFilters()->add($filterCollectionBrand);
$filterCollectionPrice = new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::GTE,
new FilterNumber(102)
),
]);
$criteria->getFilters()->add($filterCollectionPrice);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
's4',
'h1',
],
'facets' => [
0 => [
'code' => 'brand',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'adidas',
'count' => 0,
'active' => false,
],
1 => [
'label' => null,
'value' => 'nike',
'count' => 2,
'active' => true,
]
],
'range' => []
]
],
1 => [
'code' => 'color',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'green',
'count' => 1,
'active' => true,
],
1 => [
'label' => null,
'value' => 'red',
'count' => 1,
'active' => true,
]
],
'range' => []
]
],
2 => [
'code' => 'size',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => '43',
'count' => 1,
'active' => true,
],
1 => [
'label' => null,
'value' => '46',
'count' => 0,
'active' => false,
],
2 => [
'label' => null,
'value' => '47',
'count' => 0,
'active' => false,
],
3 => [
'label' => null,
'value' => 'xl',
'count' => 1,
'active' => true,
]
],
'range' => [],
]
],
3 => [
'code' => 'price',
'label' => null,
'type' => 'range',
'items' => [
'list' => [],
'range' => [
0 => [
'label' => null,
'count' => 2,
'active' => true,
'fullRange' => [
'min' => 100.0,
'max' => 104.0,
],
'activeRange' => [
'min' => 103.0,
'max' => 104.0,
]
]
]
]
]
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
/**
* query по сущ цене и бренду и post_filter по цвету и цене
* @return void
*/
public function testQueryExistingPriceAndBrandAndPostFilterPriceAndColor()
{
$criteria = new Criteria();
$filterCollectionPrice = new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::LTE,
new FilterNumber(104)
),
]);
$filterCollectionPrice->setFilterType(FilterType::QUERY);
$criteria->getFilters()->add($filterCollectionPrice);
$filterCollectionBrand = new FilterGroupCollection([
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('nike')
),
new Filter(
new Field('brand'),
FilterOperator::EQ,
new FilterKeyword('adidas')
),
]);
$filterCollectionBrand
->setLogicOperator(LogicOperator::OR)
->setFilterType(FilterType::QUERY);
$criteria->getFilters()->add($filterCollectionBrand);
$filterCollectionPrice = new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::GTE,
new FilterNumber(101)
),
]);
$criteria->getFilters()->add($filterCollectionPrice);
$filterCollectionColor = new FilterGroupCollection([
new Filter(
new Field('color'),
FilterOperator::EQ,
new FilterKeyword('green')
),
]);
$criteria->getFilters()->add($filterCollectionColor);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
's4',
],
'facets' => [
0 => [
'code' => 'brand',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'adidas',
'count' => 0,
'active' => false,
],
1 => [
'label' => null,
'value' => 'nike',
'count' => 1,
'active' => true,
]
],
'range' => []
]
],
1 => [
'code' => 'color',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => 'green',
'count' => 1,
'active' => true,
],
1 => [
'label' => null,
'value' => 'red',
'count' => 2,
'active' => true,
]
],
'range' => []
]
],
2 => [
'code' => 'size',
'label' => null,
'type' => 'list',
'items' => [
'list' => [
0 => [
'label' => null,
'value' => '43',
'count' => 1,
'active' => true,
],
1 => [
'label' => null,
'value' => '46',
'count' => 0,
'active' => false,
],
2 => [
'label' => null,
'value' => '47',
'count' => 0,
'active' => false,
],
3 => [
'label' => null,
'value' => 'xl',
'count' => 0,
'active' => false,
]
],
'range' => []
]
],
3 => [
'code' => 'price',
'label' => null,
'type' => 'range',
'items' => [
'list' => [],
'range' => [
0 => [
'label' => null,
'count' => 1,
'active' => true,
'fullRange' => [
'min' => 100.0,
'max' => 104.0,
],
'activeRange' => [
'min' => 103.0,
'max' => 103.0,
],
]
]
]
]
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
}
<?php
namespace IQDEV\ElasticSearchTests\Filter;
use IQDEV\ElasticSearch\Config\BaseConfiguration;
use IQDEV\ElasticSearch\Converter\Request\CriteriaToRequest;
use IQDEV\ElasticSearch\Criteria\Criteria;
use IQDEV\ElasticSearch\Criteria\Filter\Collection\FilterGroupCollection;
use IQDEV\ElasticSearch\Criteria\Filter\Field;
use IQDEV\ElasticSearch\Criteria\Filter\Filter;
use IQDEV\ElasticSearch\Criteria\Filter\FilterOperator;
use IQDEV\ElasticSearch\Criteria\Filter\FilterType;
use IQDEV\ElasticSearch\Criteria\Filter\Value\FilterKeyword;
use IQDEV\ElasticSearch\Criteria\Filter\Value\FilterNumber;
use IQDEV\ElasticSearch\Criteria\Query\SearchQuery;
use IQDEV\ElasticSearch\Criteria\Search\Search;
use IQDEV\ElasticSearch\Document\Property\Property;
use IQDEV\ElasticSearchTests\AbstractTestCase;
use IQDEV\ElasticSearchTests\Helpers\FormatData;
use IQDEV\ElasticSearchTests\Service\SearchClient;
/**
* Тестирование агрегирующих функций
*/
class QueryTest extends AbstractTestCase
{
public function testFilterChangeFromPostToQuery()
{
$search = [
'category' => [
'key' => 'category_id',
'value' => 'shoes',
],
];
$filter = [
'brand' => [
'key' => 'brand',
'value' => 'nike',
],
'price' => [
'key' => 'price',
'max' => 100.0,
'min' => 10.0,
]
];
$criteria = new Criteria();
$criteria->getSearch()->add(
new Search(
new Property($search['category']['key']),
$search['category']['value'],
),
);
$filterCollectionCategory = new FilterGroupCollection();
$filterCollectionCategory->setFilterType(FilterType::QUERY);
$criteria->getFilters()->add($filterCollectionCategory);
$filterCollectionBrand = new FilterGroupCollection([
new Filter(
new Field($filter['brand']['key']),
FilterOperator::EQ,
new FilterKeyword($filter['brand']['value'])
)
]);
$filterCollectionPrice = new FilterGroupCollection([
new Filter(
new Field($filter['price']['key']),
FilterOperator::LT,
new FilterNumber($filter['price']['min'])
),
new Filter(
new Field($filter['price']['key']),
FilterOperator::GT,
new FilterNumber($filter['price']['max'])
),
]);
// Формирование фильтра для post
$criteriaPost = clone $criteria;
$criteriaPost->getFilters()->add(clone $filterCollectionPrice);
$criteriaPost->getFilters()->add(clone $filterCollectionBrand);
// Формирование фильтра для query
$criteriaQuery = clone $criteria;
$filterTypeQuery = FilterType::QUERY;
$filterCollectionPrice->setFilterType($filterTypeQuery);
$filterCollectionBrand->setFilterType($filterTypeQuery);
$criteriaQuery->getFilters()->add(clone $filterCollectionPrice);
$criteriaQuery->getFilters()->add(clone $filterCollectionBrand);
// Получение классов с данными для запроса в es
$criteriaToRequest = new CriteriaToRequest(new BaseConfiguration());
$requestPost = $criteriaToRequest->fromCriteria($criteriaPost);
$requestQuery = $criteriaToRequest->fromCriteria($criteriaQuery);
$expectedFilter = [
[
"nested" => [
"path" => "search_data",
"query" => [
"bool" => [
"filter" => [
[
"nested" => [
"path" => "search_data.number_facet",
"query" => [
"bool" => [
"filter" => [
[
"term" => [
"search_data.number_facet.facet_code" => $filter['price']['key']
]
],
[
"range" => [
"search_data.number_facet.facet_value" => [
"lt" => $filter['price']['min'],
"gt" => $filter['price']['max'],
]
]
]
]
]
]
]
]
]
]
]
]
],
[
"nested" => [
"path" => "search_data",
"query" => [
"bool" => [
"filter" => [
[
"nested" => [
"path" => "search_data.keyword_facet",
"query" => [
"bool" => [
"filter" => [
[
"term" => [
"search_data.keyword_facet.facet_code" => $filter['brand']['key']
]
],
[
"term" => [
"search_data.keyword_facet.facet_value" => $filter['brand']['value']
]
]
]
]
]
]
]
]
]
]
]
],
];
$expected = [
"query" => [
"bool" => [
"must" => [
[
"term" => [
"category_id" => $search['category']['value']
]
],
]
]
],
];
$this->assertArray(
array_merge($expected, [
"query" => [
"bool" => [
"filter" => $expectedFilter,
]
]
]),
$requestQuery->es(),
'query response'
);
$this->assertArray(
array_merge($expected, [
"post_filter" => [
"bool" => [
"filter" => $expectedFilter,
]
]
]),
$requestPost->es(),
'post response'
);
}
public function testAddingASingleKeyFilterToPostAndQuery()
{
$filter = [
'price' => [
'key' => 'price',
'max' => 100.0,
'min' => 10.0,
'lower' => 0.0,
]
];
$criteria = new Criteria();
$filterCollectionPrice = new FilterGroupCollection([
new Filter(
new Field($filter['price']['key']),
FilterOperator::LT,
new FilterNumber($filter['price']['min'])
),
new Filter(
new Field($filter['price']['key']),
FilterOperator::GT,
new FilterNumber($filter['price']['max'])
),
]);
$criteria->getFilters()->add($filterCollectionPrice);
$filterCollectionQueryPrice = new FilterGroupCollection([
new Filter(
new Field($filter['price']['key']),
FilterOperator::LT,
new FilterNumber($filter['price']['lower'])
),
]);
$filterCollectionQueryPrice->setFilterType(FilterType::QUERY);
$criteria->getFilters()->add($filterCollectionQueryPrice);
$criteriaToRequest = new CriteriaToRequest(new BaseConfiguration());
$request = $criteriaToRequest->fromCriteria($criteria);
$expected = [
"post_filter" => [
"bool" => [
"filter" => [
0 => [
"nested" => [
"path" => "search_data",
"query" => [
"bool" => [
"filter" => [
0 => [
"nested" => [
"path" => "search_data.number_facet",
"query" => [
"bool" => [
"filter" => [
[
"term" => [
"search_data.number_facet.facet_code" => "price"
]
],
[
"range" => [
"search_data.number_facet.facet_value" => [
"lt" => $filter['price']['min'],
"gt" => $filter['price']['max'],
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
],
"query" => [
"bool" => [
"filter" => [
0 => [
"nested" => [
"path" => "search_data",
"query" => [
"bool" => [
"filter" => [
0 => [
"nested" => [
"path" => "search_data.number_facet",
"query" => [
"bool" => [
"filter" => [
0 => [
"term" => [
"search_data.number_facet.facet_code" => "price"
]
],
1 => [
"range" => [
"search_data.number_facet.facet_value" => [
"lt" => $filter['price']['lower']
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
];
$this->assertArray($expected, $request->es());
}
public function testGlobalFilterPrice()
{
$filter = [
'price' => [
'key' => 'price',
'min' => 105,
'lower' => 103,
]
];
$criteria = new Criteria();
$filterCollectionPrice = new FilterGroupCollection([
new Filter(
new Field($filter['price']['key']),
FilterOperator::GTE,
new FilterNumber($filter['price']['min'])
)
]);
$criteria->getFilters()->add($filterCollectionPrice);
$filterCollectionQueryPrice = new FilterGroupCollection([
new Filter(
new Field($filter['price']['key']),
FilterOperator::GTE,
new FilterNumber($filter['price']['lower'])
),
]);
$filterCollectionQueryPrice->setFilterType(FilterType::QUERY);
$criteria->getFilters()->add($filterCollectionQueryPrice);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
'h2',
'h3',
'p1',
],
"facets" => [
0 => [
"code" => "brand",
"label" => null,
"type" => "list",
"items" => [
"list" => [
0 => [
"label" => null,
"value" => "nike",
"count" => 1,
"active" => true,
],
1 => [
"label" => null,
"value" => "rebook",
"count" => 2,
"active" => true,
]
],
"range" => [],
],
],
1 => [
"code" => "color",
"label" => null,
"type" => "list",
"items" => [
"list" => [
0 => [
"label" => null,
"value" => "green",
"count" => 0,
"active" => false,
],
1 => [
"label" => null,
"value" => "red",
"count" => 0,
"active" => false,
],
2 => [
"label" => null,
"value" => "white",
"count" => 3,
"active" => true,
],
],
"range" => [],
],
],
2 => [
"code" => "size",
"label" => null,
"type" => "list",
"items" => [
"list" => [
0 => [
"label" => null,
"count" => 0,
"value" => "43",
"active" => false,
],
1 => [
"label" => null,
"value" => "xl",
"count" => 2,
"active" => true,
],
2 => [
"label" => null,
"value" => "xxl",
"count" => 1,
"active" => true,
],
],
"range" => [],
],
],
3 => [
"code" => "price",
"label" => null,
"type" => "range",
"items" => [
"list" => [],
"range" => [
0 => [
"label" => null,
"count" => 3,
"active" => true,
"fullRange" => [
"min" => 103.0,
"max" => 107.0,
],
"activeRange" => [
"min" => 105.0,
"max" => 107.0,
]
]
]
]
]
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
public function testGlobalFilterMinPrice()
{
$filter = [
'price' => [
'key' => 'price',
'lower' => 103,
]
];
$criteria = new Criteria();
$filterCollectionQueryPrice = new FilterGroupCollection([
new Filter(
new Field($filter['price']['key']),
FilterOperator::GTE,
new FilterNumber($filter['price']['lower'])
),
]);
$filterCollectionQueryPrice->setFilterType(FilterType::QUERY);
$criteria->getFilters()->add($filterCollectionQueryPrice);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
's4',
'h1',
'h2',
'h3',
'p1',
],
"facets" => [
0 => [
"code" => "brand",
"label" => null,
"type" => "list",
"items" => [
"list" => [
0 => [
"label" => null,
"value" => "nike",
"count" => 3,
"active" => true,
],
1 => [
"label" => null,
"value" => "rebook",
"count" => 2,
"active" => true,
]
],
"range" => [],
],
],
1 => [
"code" => "color",
"label" => null,
"type" => "list",
"items" => [
"list" => [
0 => [
"label" => null,
"value" => "green",
"count" => 1,
"active" => true,
],
1 => [
"label" => null,
"value" => "red",
"count" => 1,
"active" => true,
],
2 => [
"label" => null,
"value" => "white",
"count" => 3,
"active" => true,
],
],
"range" => [],
],
],
2 => [
"code" => "size",
"label" => null,
"type" => "list",
"items" => [
"list" => [
0 => [
"label" => null,
"count" => 1,
"value" => "43",
"active" => true,
],
1 => [
"label" => null,
"value" => "xl",
"count" => 3,
"active" => true,
],
2 => [
"label" => null,
"value" => "xxl",
"count" => 1,
"active" => true,
],
],
"range" => [],
],
],
3 => [
"code" => "price",
"label" => null,
"type" => "range",
"items" => [
"list" => [],
"range" => [
0 => [
"label" => null,
"count" => 5,
"active" => true,
"fullRange" => [
"min" => 103.0,
"max" => 107.0,
],
"activeRange" => [
"min" => 103.0,
"max" => 107.0,
]
]
]
]
]
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
}
}
<?php
namespace IQDEV\ElasticSearchTests\Filter;
use IQDEV\ElasticSearch\Criteria\Criteria;
use IQDEV\ElasticSearch\Criteria\Filter\Collection\FilterGroupCollection;
use IQDEV\ElasticSearch\Criteria\Filter\Field;
use IQDEV\ElasticSearch\Criteria\Filter\Filter;
use IQDEV\ElasticSearch\Criteria\Filter\FilterOperator;
use IQDEV\ElasticSearch\Criteria\Filter\Value\FilterNumber;
use IQDEV\ElasticSearch\Criteria\Query\SearchQuery;
use IQDEV\ElasticSearchTests\AbstractTestCase;
use IQDEV\ElasticSearchTests\Helpers\FormatData;
use IQDEV\ElasticSearchTests\Service\SearchClient;
class RangeTest extends AbstractTestCase
{
/**
* Поиск элементов по мин цене
*
* @return void
*/
public function testExistByMinPrice(): void
{
$criteria = new Criteria();
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::GTE,
new FilterNumber(103)
),
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
's4',
'h1',
'h2',
'h3',
'p1'
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatData($result));
}
/**
* Поиск элементов по мин цене
*
* @return void
*/
public function testExistByMinPrice2(): void
{
$criteria = new Criteria();
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::GT,
new FilterNumber(103.01)
),
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
'h1',
'h2',
'h3',
'p1'
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatData($result));
}
/**
* Поиск элементов по мин цене
*
* @return void
*/
public function testExistByMinPrice3(): void
{
$criteria = new Criteria();
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::GT,
new FilterNumber(102.99)
),
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
's4',
'h1',
'h2',
'h3',
'p1'
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatData($result));
}
/**
* Поиск элементов по макс цене
*
* @return void
*/
public function testExistByMaxPrice(): void
{
$criteria = new Criteria();
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::LTE,
new FilterNumber(102)
),
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
's1',
's2',
's3',
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatData($result));
}
/**
* Поиск элементов по макс цене
*
* @return void
*/
public function testExistByMaxPrice2(): void
{
$criteria = new Criteria();
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::LT,
new FilterNumber(102.99)
),
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
's1',
's2',
's3',
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatData($result));
}
/**
* Поиск элементов по макс цене
*
* @return void
*/
public function testExistByMaxPrice3(): void
{
$criteria = new Criteria();
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::LT,
new FilterNumber(101.99)
),
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
's1',
's2',
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatData($result));
}
/**
* Поиск элементов по диапазону цен
*
* @return void
*/
public function testExistByRangePrice(): void
{
$criteria = new Criteria();
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::GTE,
new FilterNumber(101)
),
new Filter(
new Field('price'),
FilterOperator::LTE,
new FilterNumber(102)
),
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
's2',
's3',
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatData($result));
}
/**
* Поиск элементов по диапазону цен
*
* @return void
*/
public function testExistByRangePrice2(): void
{
$criteria = new Criteria();
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::GTE,
new FilterNumber(101.01)
),
new Filter(
new Field('price'),
FilterOperator::LTE,
new FilterNumber(102)
),
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
's3',
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatData($result));
}
/**
* Поиск элементов по диапазону цен
*
* @return void
*/
public function testExistByRangePrice3(): void
{
$criteria = new Criteria();
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::GTE,
new FilterNumber(101)
),
new Filter(
new Field('price'),
FilterOperator::LTE,
new FilterNumber(101.99)
),
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
's2',
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatData($result));
}
/**
* Поиск элементов по диапазону цен
*
* @return void
*/
public function testExistByRangePrice4(): void
{
$criteria = new Criteria();
$criteria->getFilters()->add(new FilterGroupCollection([
new Filter(
new Field('price'),
FilterOperator::GTE,
new FilterNumber(101.99)
),
new Filter(
new Field('price'),
FilterOperator::LTE,
new FilterNumber(101.99)
),
]));
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => []
];
$this->assertEquals($expected, FormatData::formatData($result));
}
}
\ No newline at end of file
<?php
namespace IQDEV\ElasticSearchTests\Filter;
use IQDEV\ElasticSearch\Criteria\Criteria;
use IQDEV\ElasticSearch\Criteria\Query\SearchQuery;
use IQDEV\ElasticSearch\Criteria\Search\Search;
use IQDEV\ElasticSearch\Document\Property\Property;
use IQDEV\ElasticSearch\Document\Property\PropertyType;
use IQDEV\ElasticSearchTests\AbstractTestCase;
use IQDEV\ElasticSearchTests\Helpers\FormatData;
use IQDEV\ElasticSearchTests\Service\SearchClient;
class SearchItemsTest extends AbstractTestCase
{
/**
* Поиск всех элементов раздела
*
* @return void
*/
public function testExistByCategory(): void
{
$criteria = new Criteria();
$criteria->getSearch()->add(
new Search(
new Property('category_id'),
'prices'
)
);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
'p1'
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatData($result));
}
/**
* Поиск всех элементов раздела
*
* @return void
*/
public function testExistByEmptyCategory(): void
{
$criteria = new Criteria();
$criteria->getSearch()->add(
new Search(
new Property('category_id'),
'',
)
);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => []
];
$this->assertEquals($expected, FormatData::formatData($result));
}
/**
* Поиск всех элементов
*
* @return void
*/
public function testExistAllItems(): void
{
$criteria = new Criteria();
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
's1',
's2',
's3',
's4',
'h1',
'h2',
'h3',
'p1'
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatData($result));
}
/**
* Поиск существующего элемента по части строки поиска
*
* @return void
*/
public function testExistItemsByPart(): void
{
$criteria = new Criteria();
$criteria->getSearch()->add(
new Search(
new Property('full_search_content', PropertyType::TEXT),
'Nike'
)
);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
's4',
'h1',
'h2',
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatData($result));
}
/**
* Поиск существующего элемента по полному названию
*
* @return void
*/
public function testExistItemsByName(): void
{
$criteria = new Criteria();
$criteria->getSearch()->add(
new Search(
new Property('full_search_content', PropertyType::TEXT),
'Nike Dri-FIT Strike',
),
);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
'h1',
'h2',
's4'
]
];
$this->assertEquals($expected, FormatData::formatData($result));
}
/**
* Поиск существующего элемента по полному названию
*
* @return void
*/
public function testExistItemsByCyrillicName(): void
{
$criteria = new Criteria();
$criteria->getSearch()->add(
new Search(
new Property('full_search_content', PropertyType::TEXT),
'Товар с ценой',
),
);
$q = new SearchQuery($criteria);
$handler = SearchClient::getInstance();
$result = $handler->handle($q)->result;
$expected = [
'hits' => [
'p1',
]
];
$this->assertEqualsCanonicalizing($expected, FormatData::formatData($result));
}
}