3

I want to search product in custom page so how search products programmatically in custom controller ?

Below Magento 1.x search programmatically

$queryText = Mage::helper('catalogSearch')->getQueryText();
$collection = Mage::getResourceModel('catalogsearch/query_collection')
    ->setStoreId($storeId)
    ->setQueryFilter($queryText);

this type of search code available in magento 2 ?

3

For a quick reference, we can take a look:

vendor/magento/module-search/Model/ResourceModel/Query/Collection.php

public function setQueryFilter($query)
{
        ......
}

In Magento 1, it's Mage_CatalogSearch_Model_Resource_Query_Collection public function setQueryFilter($query) {......}

And then navigate to vendor/magento/module-search/Model/Query.php

 public function getSuggestCollection()
 {
        $collection = $this->getData('suggest_collection');
        if ($collection === null) {
            $collection = $this->_queryCollectionFactory->create()->setStoreId(
                $this->getStoreId()
            )->setQueryFilter(
                $this->getQueryText()
            );
            $this->setData('suggest_collection', $collection);
        }
        return $collection;
 }

We can see how it works.

0

Try below code its work for me:

$this->filterBuilder->setField('search_term');
$this->filterBuilder->setValue('wat');
$this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
$searchCriteria = $this->searchCriteriaBuilder->create();
$searchCriteria->setRequestName("quick_search_container");
$this->searchResult = $this->searchInterface->search($searchCriteria);

//Create Temp table for store result 
$temporaryStorage = $this->temporaryStorageFactory->create();
$table = $temporaryStorage->storeApiDocuments($this->searchResult->getItems());        
$productCollection = $this->_productCollectionFactory->create();
$productCollection->getSelect()->joinInner(
       ['search_result' => $table->getName()],
       'e.entity_id = search_result.' . TemporaryStorage::FIELD_ENTITY_ID,
       []
);


// Classes need to load

use Magento\Search\Api\SearchInterface;  //$this->searchInterface
use Magento\Framework\Search\Adapter\Mysql\TemporaryStorageFactory; // $this->temporaryStorageFactory
use Magento\Framework\Api\Search\SearchCriteriaBuilder; // $this->searchCriteriaBuilder
use Magento\Framework\Search\Adapter\Mysql\TemporaryStorage; //TemporaryStorage

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.