storeManager = $storeManager; $this->catalogSearchHelper = $catalogSearchHelper; $this->queryFactory = $queryFactory; $this->resultJsonFactory = $resultJsonFactory; } /** * Save search term * * @return Json */ public function execute() { /* @var $query \Magento\Search\Model\Query */ $query = $this->queryFactory->get(); $query->setStoreId($this->storeManager->getStore()->getId()); if ($query->getQueryText() != '') { try { if ($this->catalogSearchHelper->isMinQueryLength()) { $query->setId(0)->setIsActive(1)->setIsProcessed(1); } else { $query->saveIncrementalPopularity(); } $responseContent = ['success' => true, 'error_message' => '']; } catch (\Magento\Framework\Exception\LocalizedException $e) { $responseContent = ['success' => false, 'error_message' => $e]; } } else { $responseContent = ['success' => false, 'error_message' => __('Search term is empty')]; } /** @var Json $resultJson */ $resultJson = $this->resultJsonFactory->create(); return $resultJson->setData($responseContent); } }