Magento 2 Layered Navigation – Does It Always Use Configured Search Engine?

catalogsearchlayered-navigationmagento-2.1magento2search

In Magento 2.1 custom search engines were introduced, so that MySQL could be replaced with ElasticSearch in EE.

My own search adapter works well in the search but now I realized that it is also used to fetch filters in layered navigation on category pages.

This leads to an error if my search result does not contain "buckets" for filterable attributes, like this:

Exception #0 (Magento\Framework\Exception\StateException): Bucket does not exist
#0 [...]/var/generation/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection/Interceptor.php(115): Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection->getFacetedData('style_bags')

While it makes sense to use the search adapter here, my problem is that it does it with the exact same method in Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection that is also used to fetch search results.

This does not work because I distinguish between attributes configured as filterable in search and as filterable in categories, so the normal search result does not contain the buckets that are needed on the category page.

I'll probably implement a switch based on the current controller, although that's not the cleanest solution.

But my question for now is: Is it really necessary to use the search engine in layered navigation or is there a way to fall back to the old method?


Update

It turns out that I get enough information in the $request parameter passed to the query method of my adapter implementation to figure out the context:

debugger screenshot

This makes at least a cleaner solution for my switch.

But when I try to fall back to the default MySQL adapter like this (with adapter injected as \Magento\Framework\Search\Adapter\Mysql\Adapter\Proxy):

if ($request->getName() === 'catalog_view_container') {
    return $this->mysqlAdapter->query($request);
}

there are errors in other places like Magento\Framework\Search\Dynamic\IntervalFactory where my adapter is still used based on di.xml and the system configuration.

Best Answer

Magento 2 have layered navigation on search result page too. If you do not cover filtering and aggregation in your adapter this functionality will brocken, so you still need it not only for category page

Related Topic