Question

I've tried looking for products programmatically that get the same results as magento 2 searches (In the search box). I have used:

    $result = array();


    $filter1 = $this->_filterBuilder->setField('name')
        ->setValue('%' . $searchTerm . '%')
        ->setConditionType("like")
        ->create();

    $filter2 = $this->_filterBuilder->setField('description')
        ->setValue('%' . $searchTerm . '%')
        ->setConditionType("like")
        ->create();

    $filter3 = $this->_filterBuilder->setField('visibility')
        ->setValue("1")
        ->setConditionType("neq")
        ->create();

    $filter4 = $this->_filterBuilder->setField('status')
        ->setValue("1")
        ->setConditionType("eq")
        ->create();

    if($searchOrder == "relevance") $searchOrder = "name";

    //filter1 OR filter2
    $filterGroup = $this->_filterGroupBuilder->setFilters(array($filter1, $filter2))->create();

    //filterGroup AND filterGroup2
    $filterGroup2 = $this->_filterGroupBuilder->setFilters(array($filter3))->create();

    //filterGroup AND filterGroup2 AND filterGroup3
    $filterGroup3 = $this->_filterGroupBuilder->setFilters(array($filter4))->create();


    $searchDirection = ($searchDir == "desc") ? SortOrder::SORT_DESC : SortOrder::SORT_ASC;

    $this->_searchCriteriaBuilder->addSortOrder($searchOrder, $searchDirection);
    $searchCriteria = $this->_searchCriteriaBuilder->setCurrentPage($page)->setPageSize($pageSize)->create();
    $searchCriteria->setFilterGroups([$filterGroup, $filterGroup2, $filterGroup3]);
    $products = $this->_productRepository->getList($searchCriteria);
    $totalSearchResults = $products->getTotalCount();
    $products = $products->getItems();

But I do not always get the same results. Besides that if I put two words, my method does not work.

I have come to the conclusion that the only way would be to use magento-catalog-search to get exactly the same results. but I can not find a way to do it.

Has anyone used catalog-search to programmatically search?

Thanks

Answer:

$search_criteria = $this->_searchCriteriaBuilder->create();

        $search_criteria->setRequestName("quick_search_container");

        $filter = $this->_filterBuilder->setField('search_term')
            ->setValue($searchTerm)
            ->setConditionType("like")
            ->create();

        $filterGroup = $this->_filterGroupBuilder->addFilter($filter)->create();

        $searchDirection = ($searchDir == "desc") ? SortOrder::SORT_DESC : SortOrder::SORT_ASC;

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $sortOrder = $objectManager->create('Magento\Framework\Api\SortOrder');
        $sortOrder->setField('name');
        $sortOrder->setDirection($searchDirection);

        $search_criteria->setFilterGroups([$filterGroup])
            ->setCurrentPage($page);

        $search_criteria->setSortOrders([$sortOrder]);

        $resultx = $this->_SearchInterface->search($search_criteria);

        $totalSearchResults = $resultx->getTotalCount();

        $products = $resultx->getItems();



        foreach ($products as $product) {

            $id = $product->getId();
            $result[] = $id;

        }

        return $result;

the only thing that does not work is to sort the results

Was it helpful?

Solution

I think what you need is the SearchInterface of the Search Module.

Have a look at this post: Multiselect Filter in Magento 2 search Rest API

Basically, you see the filters and filter groups being build in the URL.

Then the webapi module does nothing less than executing the function Magento\Search\Api\SearchInterface::search() with the filters and filter groups as search criteria.

So in your case, I think you can use the SearchInterface directly instead of the ProductRepository::getList() function.

OTHER TIPS

Alternate solution, you can render the same template by add the below content to the needed template.

<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magento_Search::form.mini.phtml")->toHtml(); ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top