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 :)