0

I am using Magento 2 with elastic search enabled. I have a custom filtering of the product collection.

Now, when elastic search is enabled, custom filtering don't work however with MySQL search, it does work.

Any idea what's causing the issue or how elastic search is creating the product collection query?

  • have you already tried to launch a full reindex? Once you set ElasticSearch you need to generate all the documents reindexing the catalog. – Mattia Kozianowski Apr 1 at 14:34
  • Did you ever figure this out? I am facing the same issue. Which version of magento are you on? – Brandon Irwin Apr 24 at 20:27
2

Just been through the same fun! Here is the solution for custom collections layered nav, specifically for Elastic Search;

Create a plugin based on your version;

Elastic Search 5 (built in)

<type name="Magento\Elasticsearch\Model\Client\Elasticsearch">
    <plugin name="pixie_customcollection_elastic_product_index" type="PixieMedia\CustomCollection\Model\Plugin\ElasticLayerPlugin" />
</type>

Elastic Search 6 (built in)

<type name="Magento\Elasticsearch6\Model\Client\Elasticsearch">
    <plugin name="pixie_customcollection_elastic_product_index" type="PixieMedia\CustomCollection\Model\Plugin\ElasticLayerPlugin" />
</type>

Amasty Elastic Search module (third party)

 <type name="Amasty\ElasticSearch\Model\Client\Elasticsearch">
    <plugin name="pixie_customcollection_elastic_product_index" type="PixieMedia\CustomCollection\Model\Plugin\ElasticLayerPlugin" />
</type>

Then the plugin itself - note the difference;

This version for built in ES 5 or 6;

namespace PixieMedia\CustomCollection\Model\Plugin;


class ElasticLayerPlugin
{


protected $cHelper;


public function __construct(
\PixieMedia\CustomCollection\Helper\Search $cHelper
) {
$this->cHelper = $cHelper;
}



public function beforeQuery($subject,$query) {


    // This is where to fetch the entity_id array to filter the Elastic collection with
    $filteredIds   = $this->cHelper->filterCollectionIds();

    if(!$filteredIds || count($filteredIds) < 1)  { 

        return [$query]; 
    }

    // Add the entity_id filter to the Elastic collection
    $query['body']['query']['bool']['filter'] = ['ids' => [ 'values' => $filteredIds]];


    return [$query];

}




}

This version for Amasty Elastic Search module;

namespace PixieMedia\CustomCollection\Model\Plugin;


class ElasticLayerPlugin
{


protected $cHelper;


public function __construct(
\PixieMedia\CustomCollection\Helper\Search $cHelper
) {
$this->cHelper = $cHelper;
}



public function beforeSearch($subject,$query) {


    // This is where to fetch the entity_id array to filter the Elastic collection with
    $filteredIds   = $this->cHelper->filterCollectionIds();

    if(!$filteredIds || count($filteredIds) < 1)  { 

        return [$query]; 
    }

    // Add the entity_id filter to the Elastic collection
    $query['body']['query']['bool']['filter'] = ['ids' => [ 'values' => $filteredIds]];


    return [$query];

}




}

Problem solved :)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.