Skip to content
Snippets Groups Projects
QueryAndPostFilterTest.php 57.9 KiB
Newer Older
<?php

namespace IQDEV\ElasticSearchTests\Filter;

Nikita Chernykh's avatar
Nikita Chernykh committed
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 = [
Vadim Galizyanov's avatar
Vadim Galizyanov committed
            'hits' => [],
            'facets' => []
Vadim Galizyanov's avatar
Vadim Galizyanov committed
        $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',
Vadim Galizyanov's avatar
Vadim Galizyanov committed
            ],
            '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,
                                ]
                            ]
                        ]
                    ]
                ]
            ],
Vadim Galizyanov's avatar
Vadim Galizyanov committed
        $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',
Vadim Galizyanov's avatar
Vadim Galizyanov committed
            ],
            '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,
                                ]
                            ]
                        ]
                    ]
                ]
Vadim Galizyanov's avatar
Vadim Galizyanov committed
        $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',
Vadim Galizyanov's avatar
Vadim Galizyanov committed
            ],
            '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,
                                ]
                            ]
                        ]
                    ]
                ]
Vadim Galizyanov's avatar
Vadim Galizyanov committed
        $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',
Vadim Galizyanov's avatar
Vadim Galizyanov committed
            ],
            '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,
                                ]
                            ]
                        ]
                    ]
                ]
Vadim Galizyanov's avatar
Vadim Galizyanov committed
        $this->assertEqualsCanonicalizing($expected, FormatData::formatDataWFacets($result));
    }

    /**
     * query по сущ бренду и post_filter по бренду и цвету
     * @return void
     */
Vadim Galizyanov's avatar
Vadim Galizyanov committed
    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,
Vadim Galizyanov's avatar
Vadim Galizyanov committed
                new FilterKeyword('red')
        $criteria->getFilters()->add($filterCollectionColor);

        $q = new SearchQuery($criteria);
        $handler = SearchClient::getInstance();
        $result = $handler->handle($q)->result;

        $expected = [
            'hits' => [
Vadim Galizyanov's avatar
Vadim Galizyanov committed
                '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,
                                ]
                            ]
                        ]
                    ]
                ]
Vadim Galizyanov's avatar
Vadim Galizyanov committed
        $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 = [
Vadim Galizyanov's avatar
Vadim Galizyanov committed
            'hits' => [],
            'facets' => [],
Vadim Galizyanov's avatar
Vadim Galizyanov committed
        $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',
Vadim Galizyanov's avatar
Vadim Galizyanov committed
            ],
            '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,
                                ]
                            ]
                        ]
                    ]
                ]
Vadim Galizyanov's avatar
Vadim Galizyanov committed
        $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',
Vadim Galizyanov's avatar
Vadim Galizyanov committed
            ],
            '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 => [