2

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

3

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.

  • you were right, thanks!! the only fault is that it does not sort by name, ASC & DESC. I have updated my question – Miguel Sep 4 '17 at 16:03
  • I'm not sure how exactly the search algorithm of magento works, but I think it sorts by relevance, whatever the relevance is exactly. I think it's based on search weights you can define for the attributes. So if the name for example has a high search weight and the search word is contained in the proiduct name, it will most likely be first – David Verholen Sep 4 '17 at 16:14
  • on the front you can change if you want to sort by name, price or relevance.... – Miguel Sep 4 '17 at 16:35
  • ok, sorting seems not to be supported by the basic search engine. You should have a look at the catalogsearch fulltext collection. It has a function setOrder, which should be used by the default search result page I assume github.com/magento/magento2/blob/develop/app/code/Magento/… – David Verholen Sep 4 '17 at 16:58
  • I have no idea how to convert the answer to using fulltext ... – Miguel Sep 4 '17 at 17:58
1

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(); ?>

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.