5

When using magento search REST API you can search with logical AND search

http://devdocs.magento.com/guides/v2.0/howdoi/webapi/search-criteria.html#logical-and-search

For example I want to search all product that contains shower and bath in the name. With an api request this is pretty simple.

http://local.webshop/rest/V1/products?
    searchCriteria[filter_groups][0][filters][0][field]=name&
    searchCriteria[filter_groups][0][filters][0][value]=%25bath%25&
    searchCriteria[filter_groups][0][filters][0][condition_type]=like&
    searchCriteria[filter_groups][1][filters][0][field]=name&
    searchCriteria[filter_groups][1][filters][0][value]=%25shower%25&
    searchCriteria[filter_groups][1][filters][0][condition_type]=like

Now I want to search like this in my magento2 website using the default search input text. I don't want to make an API request. How would I change this? The default magento search functionality uses Logical OR search

I updated the _renderFiltersBefore() in Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection

/**
 * @inheritdoc
 */
protected function _renderFiltersBefore()
{
    $this->getSearchCriteriaBuilder();
    $this->getFilterBuilder();
    $this->getSearch();

    $priceRangeCalculation = $this->_scopeConfig->getValue(
        \Magento\Catalog\Model\Layer\Filter\Dynamic\AlgorithmFactory::XML_PATH_RANGE_CALCULATION,
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE
    );

    $searchCriteria = $this->searchCriteriaBuilder->create();

    $this->filterGroupBuilder = ObjectManager::getInstance()->get('\Magento\Framework\Api\Search\FilterGroupBuilder');
    /*
    Filter 1
     */
    $filter1 = $this->filterBuilder->setField('search_term')
        ->setValue('bath')
        ->create();
    $filterGroup1 = $this->filterGroupBuilder->addFilter($filter1)->create();

    /*
    Filter 2
     */
    $filter2 = $this->filterBuilder->setField('search_term')
        ->setValue('tap')
        ->create();
    $filterGroup2 = $this->filterGroupBuilder->addFilter($filter2)->create();

    if ($priceRangeCalculation) {
        $priceFilter = $this->filterBuilder->setField('price_dynamic_algorithm')
        ->setValue($priceRangeCalculation)
        ->create();
        $filterPriceGroup = $this->filterGroupBuilder->addFilter($priceFilter)->create();
    }

    $searchCriteria->setFilterGroups([$filterPriceGroup, $filterGroup2, $filterGroup1]);
    $searchCriteria->setRequestName($this->searchRequestName);

    try {
        $this->searchResult = $this->getSearch()->search($searchCriteria);
    } catch (EmptyRequestDataException $e) {
        /** @var \Magento\Framework\Api\Search\SearchResultInterface $searchResult */
        $this->searchResult = $this->searchResultFactory->create()->setItems([]);
    } catch (NonExistingRequestNameException $e) {
        $this->_logger->error($e->getMessage());
        throw new LocalizedException(__('Sorry, something went wrong. You can find out more in the error log.'));
    }

    $temporaryStorage = $this->temporaryStorageFactory->create();
    $table = $temporaryStorage->storeApiDocuments($this->searchResult->getItems());

    $this->getSelect()->joinInner(
        [
            'search_result' => $table->getName(),
        ],
        'e.entity_id = search_result.' . TemporaryStorage::FIELD_ENTITY_ID,
        []
    );

    $this->_totalRecords = $this->searchResult->getTotalCount();

    if ($this->order && 'relevance' === $this->order['field']) {
        $this->getSelect()->order('search_result.'. TemporaryStorage::FIELD_SCORE . ' ' . $this->order['dir']);
    }
    return parent::_renderFiltersBefore();
}

This is not returning the expected results.

Thanks

0

In this question here (that I made): Magento 2.1.6 Product Grid page count and record count issue when programmatically adding filter using different methods

I explore several different solutions to add filters (with the AND logical operator) to the Product Collection. See my answer to the question for the final solution.

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.