Skip to content
This repository has been archived by the owner on Aug 3, 2018. It is now read-only.

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

Open
MartijnHarte opened this issue Dec 6, 2017 · 2 comments
Open

Comments

@MartijnHarte
Copy link

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
Copy link

psihius commented Mar 14, 2018

@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
Copy link
Author

@psihius

Here are the steps to reproduce:

  • Install standard sylius (sylius/sylius-standard)
  • Install elastic search plugin (sylius/elastic-search-plugin)
  • Configure sylius & elastic search plugin as explained in readme
  • Load sylius fixtures and create elastic search index
Create a controller (click for code)

<?php

declare(strict_types=1);

namespace AppBundle\Controller;

use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use ONGR\FilterManagerBundle\Search\FilterManagerInterface;
use Sylius\ElasticSearchPlugin\Factory\View\ProductListViewFactoryInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * @author Arkadiusz Krakowiak <[email protected]>
 */
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)

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)

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)

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

{% block content %}
    <div>
        <h2>Filters</h2>
        {% for filter in results.filters.options.items %}
            <div>
                <h3>{{ filter.name }}</h3>
                <ul>
                    {% for choice in filter.choices %}
                        <li>
                            <div class="ui checkbox {% if choice.isActive() %}checked{% endif %}">
                                <label>{{ choice.getLabel() | capitalize }}</label>
                                <input type="checkbox" {% if choice.isActive() %}checked{% endif %}
                                       onchange="window.location='{{ path(app.request.get('_route'), choice.urlParameters | merge({'slug': app.request.get('slug')}) )|raw }}'">
                            </div>
                        </li>
                    {% endfor %}
                </ul>
            </div>
        {% endfor %}
    </div>
{% endblock %}

  • Now navigate to /en_US/c/t-shirts?channel=US_WEB&enabled=1&taxonSlug=t-shirts
  • Activate a filter, now all other filters disappear

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants