Sylius / SyliusElasticSearchPlugin

DEPRECATED! Use https://github.com/BitBagCommerce/SyliusElasticsearchPlugin instead.
22 stars 26 forks source link

Selecting an option in a filter makes other option groups disappear #82

Open MartijnHarte opened 6 years ago

MartijnHarte commented 6 years ago

When selecting an option in a filter using the default configuration makes other option groups disappear.

The default configuration of options is:

options:
    type: sylius_elastic_search.multi_dynamic_aggregate_options
    request_field: options
    document_field: variants>variants.options>variants.options.value.raw
        options:
            name_field: variants.options.name.raw
            sort_type: _term
            sort_order: asc
            size: 100

The document field variants>variants.options>variants.options.value.raw defines a double nested aggregation to get to the variant's options. Because of this double nested aggregation the sylius_elastic_search.multi_dynamic_aggregate_options filter type is needed to override ONGR's default functionality. This override however, breaks the default functionality.

Changing the document field to variants.options>variants.options.value.raw (directly pointed to the variant's options in a single nested aggregation) makes the need for an overridden filter type redundant. By changing this document field, we're able to make use of the sylius_elastic_search.multi_dynamic_aggregate filter type. The excess OptionMultiDynamicAggregate could be removed.

By making use of the MultiDynamicAggregate filter type, functionality of multiple option groups is restored.

The new configuration for options would be:

options:
    type: sylius_elastic_search.multi_dynamic_aggregate
    request_field: options
    document_field: variants.options>variants.options.value.raw
        options:
            name_field: variants.options.name.raw
            sort_type: _term
            sort_order: asc
            size: 100
psihius commented 6 years ago

@MartijnHarte Could you give a db dump or some extended info so I can properly replicate the issue on my side and adjust? I think I may understand what is happening, but i'm not 100% sure. I guess this is related to when there are more than a single option for a product, so there are various combinations, like Color and Size of a t-shirt, if you select a color, you can't filter by size anymore, am I right?

The extended multi_dynamic_aggregate_options is there for a reason, because it has additional functionality that is lacking in the ONGR base and is needed for Sylius, so it can't be just dropped.

MartijnHarte commented 6 years ago

@psihius

Here are the steps to reproduce:

Create a controller (click for code)

```php */ final class DefaultController extends Controller { /** * @var ViewHandlerInterface */ private $viewHandler; /** * @var ProductListViewFactoryInterface */ private $productListViewFactory; /** * @var FilterManagerInterface */ private $filterManager; /** * @param ViewHandlerInterface $viewHandler * @param ProductListViewFactoryInterface $productListViewFactory * @param FilterManagerInterface $filterManager */ public function __construct( ViewHandlerInterface $viewHandler, ProductListViewFactoryInterface $productListViewFactory, FilterManagerInterface $filterManager ) { $this->viewHandler = $viewHandler; $this->productListViewFactory = $productListViewFactory; $this->filterManager = $filterManager; } /** * @param Request $request * * @return Response * @throws \InvalidArgumentException */ public function __invoke(Request $request) { $response = $this->filterManager->handleRequest($request); /** @var \Sylius\ElasticSearchPlugin\Controller\ProductListView $data */ $data = $this->productListViewFactory->createFromSearchResponse( $response ); $view = View::create($data, Response::HTTP_OK); $view->setTemplate($request->attributes->get('template')); $view->setTemplateVar('results'); return $this->viewHandler->handle($view); } } ```

Create a route (click for code)

```yaml product_index: path: /{_locale}/c/{slug} methods: [GET] defaults: _controller: 'AppBundle\Controller\DefaultController' template: "@AppBundle/list.html.twig" requirements: slug: .+ ```

Define controller as service (click for code)

```yaml services: AppBundle\Controller\DefaultController: arguments: - '@fos_rest.view_handler' - '@sylius_elastic_search.factory.product_list_view' - '@ongr_filter_manager.manager.search_list' - '@sylius.context.channel' ```

Create a template (click for code)

```twig {% extends '@SyliusShop/layout.html.twig' %} {% block content %}

Filters

{% for filter in results.filters.options.items %}

{{ filter.name }}

    {% for choice in filter.choices %}
  • {% endfor %}
{% endfor %}
{% endblock %} ```

Changing the following configuration solves the problem:

options:
    type: sylius_elastic_search.multi_dynamic_aggregate_options
    request_field: options
    document_field: variants>variants.options>variants.options.value.raw
        options:
            name_field: variants.options.name.raw
            sort_type: _term
            sort_order: asc
            size: 100
options:
    type: sylius_elastic_search.multi_dynamic_aggregate
    request_field: options
    document_field: variants.options>variants.options.value.raw
        options:
            name_field: variants.options.name.raw
            sort_type: _term
            sort_order: asc
            size: 100