Building a custom module where I need to search on the third-party table for data.
I have executed and filtered data from the third-party table and retrieve the entity_id from it and added the entity_id the below query.
$this->queryResponse = $this->searchEngine->search($queryRequest);
$ids = [0];
$productIds = $this->crossReferenceFactory->getProductIdsBySearch($this->queryText);
if(count($productIds)>0){
foreach($productIds as $id)
$ids[] = $id;
}
/** @var \Magento\Framework\Search\Document $document */
foreach ($this->queryResponse as $document) {
$ids[] = $document->getId();
}
parent::addFieldToFilter('entity_id', ['in' => $ids]);
When I do such filter it lists the products perfectly fine, But when I query for words which exist only in the third-party table it doesn't build the layered navigation.
After debugging I come to know that layered navigation uses below method.
public function getFacetedData($field)
{
$this->_renderFilters();
$aggregations = $this->queryResponse->getAggregations();
$bucket = $aggregations->getBucket($field . '_bucket');
if (!$bucket) {
throw new StateException(new Phrase('Bucket does not exist'));
}
$result = [];
foreach ($bucket->getValues() as $value) {
$metrics = $value->getMetrics();
$result[$metrics['value']] = $metrics;
}
return $result;
}
So, When I search for any particular word it builds the layered navigation using queryResponse
data.
My concern is how can I add my table to $queryRequest
so that I can have a consistent result. Any help would highly appreciate.
Best Answer
if you want to remove the dependency on Magento Fulltext catalog search then rewrite
Magento/CatalogSearch/Model/Search/IndexBuilder.php
file in your custom module and change SQL query(which will get data from your custom table) in function public function build (RequestInterface $request) then layer navigation filters also work properly.