_storeManager = $storeManager; $this->_catalogSession = $catalogSession; $this->_queryFactory = $queryFactory; $this->layerResolver = $layerResolver; } /** * Display search result * * @return void * * @throws \Magento\Framework\Exception\LocalizedException */ public function execute() { $this->layerResolver->create(Resolver::CATALOG_LAYER_SEARCH); /* @var $query \Magento\Search\Model\Query */ $query = $this->_queryFactory->get(); $storeId = $this->_storeManager->getStore()->getId(); $query->setStoreId($storeId); $queryText = $query->getQueryText(); if ($queryText != '') { $catalogSearchHelper = $this->_objectManager->get(\Magento\CatalogSearch\Helper\Data::class); $getAdditionalRequestParameters = $this->getRequest()->getParams(); unset($getAdditionalRequestParameters[QueryFactory::QUERY_VAR_NAME]); $handles = null; if ($query->getNumResults() == 0) { $this->_view->getPage()->initLayout(); $handles = $this->_view->getLayout()->getUpdate()->getHandles(); $handles[] = static::DEFAULT_NO_RESULT_HANDLE; } if (empty($getAdditionalRequestParameters) && $this->_objectManager->get(PopularSearchTerms::class)->isCacheable($queryText, $storeId) ) { $this->getCacheableResult($catalogSearchHelper, $query, $handles); } else { $this->getNotCacheableResult($catalogSearchHelper, $query, $handles); } } else { $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl()); } } /** * Return cacheable result * * @param \Magento\CatalogSearch\Helper\Data $catalogSearchHelper * @param \Magento\Search\Model\Query $query * @param array $handles * @return void */ private function getCacheableResult($catalogSearchHelper, $query, $handles) { if (!$catalogSearchHelper->isMinQueryLength()) { $redirect = $query->getRedirect(); if ($redirect && $this->_url->getCurrentUrl() !== $redirect) { $this->getResponse()->setRedirect($redirect); return; } } $catalogSearchHelper->checkNotes(); $this->_view->loadLayout($handles); $this->_view->renderLayout(); } /** * Return not cacheable result * * @param \Magento\CatalogSearch\Helper\Data $catalogSearchHelper * @param \Magento\Search\Model\Query $query * @param array $handles * @return void * * @throws \Magento\Framework\Exception\LocalizedException */ private function getNotCacheableResult($catalogSearchHelper, $query, $handles) { if ($catalogSearchHelper->isMinQueryLength()) { $query->setId(0)->setIsActive(1)->setIsProcessed(1); } else { $query->saveIncrementalPopularity(); $redirect = $query->getRedirect(); if ($redirect && $this->_url->getCurrentUrl() !== $redirect) { $this->getResponse()->setRedirect($redirect); return; } } $catalogSearchHelper->checkNotes(); $this->_view->loadLayout($handles); $this->getResponse()->setNoCacheHeaders(); $this->_view->renderLayout(); } }