6

We have loaded product collection based on category ID, we are further trying to filter the product collection result with query string.

We have tried :

Query string set and get is working fine with below code.

$query = $this->_queryFactory->create();
$query->setQueryText('Joust');
$query->getQueryText();

Add search filter is not working to my product collection.

use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection;

$collection->addSearchFilter($query->getQueryText());

Can any one help me on this query.

0

2 Answers 2

3
+50

addSearchFilter does not do much if you don't apply the filter:

public function addSearchFilter($query)
{
    $this->queryText = trim($this->queryText . ' ' . $query);
    return $this;
}

What you need to do is to inject \Magento\Framework\Api\FilterBuilder, \Magento\Framework\Api\Search\SearchCriteriaBuilder as well as \Magento\Search\Api\SearchInterface and do the following:

$this->filterBuilder->setField('search_term');
$this->filterBuilder->setValue('Joust');
$this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
$searchCriteria = $this->searchCriteriaBuilder->create();
$searchCriteria->setRequestName("search");
$this->searchResult = $this->searchInterface->search($searchCriteria);
5
  • if we join custom table and need to add that custom table column for search how to do it ? that is think there is custom table with FK product id and custom sku if customer search with custom sku the result should also display how to do it? Dec 6, 2017 at 11:06
  • 1
    Getting error "Request name 'search' doesn't exist." at line $this->searchInterface->search($searchCriteria);
    – Magecode
    Apr 11, 2019 at 6:44
  • @Raphael Can you plz help look at my question. I'm trying to truncate the search query magento.stackexchange.com/questions/311115/… Apr 23, 2020 at 0:16
  • @Magecode did you find a solution to this? Removing the line doesn’t fix the issue because I get Request name not defined.
    – bfontaine
    Mar 31, 2021 at 15:28
  • It works if I use "quick_search_container" instead but I don’t know why
    – bfontaine
    Mar 31, 2021 at 16:59
2

It doesn't work how exactly?

The collection results do not change?

If the collection is loaded, than no amount of filtering will have effect on the results.

You would have to clear the collection and load it again:

$collection->clear();
$collection->load();

Or add the filters before it gets loaded.

Is the collection returning no or incorrect results?

Have a look at the addSearchFilter function and check what is content of the queryText

public function addSearchFilter($query)
{
    $this->queryText = trim($this->queryText . ' ' . $query);
    return $this;
}

You can always look directly at the query and see if there are issues:

var_dump($collection->getSelect()->__toString());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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