Ejemplo n.º 1
0
 /**
  * @param string $string
  * @return string
  */
 public function prepareDataIndex($string)
 {
     $string = strip_tags($string);
     $expressions = $this->config->getLongTailExpressions();
     foreach ($expressions as $expr) {
         $matches = null;
         preg_match_all($expr['match_expr'], $string, $matches);
         foreach ($matches[0] as $math) {
             $math = preg_replace($expr['replace_expr'], $expr['replace_char'], $math);
             $string .= ' ' . $math;
         }
     }
     return ' ' . $string . ' ';
 }
Ejemplo n.º 2
0
 /**
  * Synonym files
  *
  * @return array
  */
 protected function getFiles()
 {
     $options = [];
     $path = $this->config->getSynonymDirectoryPath();
     if (file_exists($path)) {
         $dh = opendir($path);
         while (false !== ($filename = readdir($dh))) {
             if (substr($filename, 0, 1) != '.') {
                 $info = pathinfo($filename);
                 $options[$path . DIRECTORY_SEPARATOR . $filename] = $info['filename'];
             }
         }
     }
     return $options;
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  * @param Observer $observer
  * @return bool
  */
 public function execute(Observer $observer)
 {
     /** @var \Magento\Framework\App\Request\Http $request */
     $request = $observer->getEvent()->getData('request');
     // search page && feature enabled && not misspelled
     if ($request->getControllerModule() == 'Magento_CatalogSearch' && $this->config->isRedirectOnSingleResult() && !$request->getParam('o')) {
         // if one result and this is product
         if ($this->query->getNumResults() == 1 && $this->layerResolver->get()->getProductCollection()->getSize() == 1) {
             /** @var \Magento\Catalog\Model\Product $product */
             $product = $this->layerResolver->get()->getProductCollection()->getFirstItem();
             /** @var \Magento\Framework\App\Response\Http\Interceptor $response */
             $response = $observer->getEvent()->getData('response');
             $response->setRedirect($product->getProductUrl())->setStatusCode(301)->sendResponse();
         }
     }
     return true;
 }
Ejemplo n.º 4
0
 /**
  * Save number of results + highlight
  *
  * @param string $html
  *
  * @return string
  */
 protected function _afterToHtml($html)
 {
     $numResults = 0;
     /** @var \Mirasvit\Search\Model\Index $index */
     foreach ($this->getIndexes() as $index) {
         $numResults += $index->getSearchCollection()->getSize();
     }
     $this->searchQueryFactory->get()->saveNumResults($numResults);
     if (!$this->config->isHighlightingEnabled()) {
         return $html;
     }
     $html = $this->highlighter->highlight($html, $this->searchQueryFactory->get()->getQueryText(), $this->getCurrentIndex()->getCode());
     return $html;
 }
Ejemplo n.º 5
0
 /**
  * @return array
  */
 public function getAvailableFiles()
 {
     $files = [];
     $path = $this->config->getSynonymDirectoryPath();
     if (file_exists($path)) {
         $dh = opendir($path);
         while (false !== ($filename = readdir($dh))) {
             if (substr($filename, 0, 1) != '.') {
                 $files[] = $path . DIRECTORY_SEPARATOR . $filename;
             }
         }
     }
     return $files;
 }
Ejemplo n.º 6
0
 /**
  * {@inheritdoc}
  * @param Observer $observer
  * @return bool
  */
 public function execute(Observer $observer)
 {
     if ($this->config->isNorouteToSearchEnabled()) {
         /** @var \Magento\Framework\App\Request\Http $request */
         $request = $observer->getEvent()->getData('request');
         /** @var \Magento\Framework\App\Response\Http\Interceptor $response */
         $response = $observer->getEvent()->getData('response');
         $extension = pathinfo($request->getRequestString(), PATHINFO_EXTENSION);
         if ($response->getStatusCode() != 404 || !$request->isGet() || in_array($extension, $this->mediaTypes)) {
             return false;
         } else {
             $searchQuery = $this->getSearchQuery($request);
             if (!$searchQuery) {
                 return false;
             }
             $message = __('The page you requested was not found, but we have searched for relevant content.');
             $this->messageManager->addNotice($message);
             $url = $this->urlFactory->create()->addQueryParams(['q' => $searchQuery])->getUrl('catalogsearch/result');
             $response->setRedirect($url)->setStatusCode(301)->sendResponse();
         }
     }
     return true;
 }
Ejemplo n.º 7
0
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     if ($input->getOption('remove')) {
         $store = $input->getOption('store');
         $collection = $this->synonymFactory->create()->getCollection();
         if ($store) {
             $collection->addFieldToFilter('store_id', $store);
         }
         $cnt = 0;
         foreach ($collection as $item) {
             $item->delete();
             $cnt++;
             if ($cnt % 1000 == 0) {
                 $output->writeln("<info>{$cnt} synonyms are removed...</info>");
             }
         }
         $output->writeln("<info>{$cnt} synonyms are removed.</info>");
         return;
     }
     if ($input->getOption('file') && $input->getOption('store')) {
         $file = $this->config->getSynonymDirectoryPath() . DIRECTORY_SEPARATOR . $input->getOption('file');
         $store = $input->getOption('store');
         $result = $this->synonymFactory->create()->import($file, $store);
         $output->writeln("<info>Imported {$result['synonyms']} synonyms</info>");
     } else {
         $output->writeln('<info>Available files:</info>');
         foreach ($this->synonymFactory->create()->getAvailableFiles() as $file) {
             $info = pathinfo($file);
             $output->writeln("    {$info['basename']}");
         }
         $output->writeln('<info>Available stores:</info>');
         foreach ($this->storeManager->getStores(true) as $store) {
             $output->writeln("    {$store->getId()} [{$store->getCode()}]");
         }
     }
 }
Ejemplo n.º 8
0
 /**
  * {@inheritdoc}
  */
 public function query(RequestInterface $request)
 {
     $query = $this->mapper->buildQuery($request);
     if ($request->getName() == 'quick_search_container') {
         $query->limit($this->searchConfig->getResultsLimit());
     }
     if (isset($_GET) && isset($_GET['debug'])) {
         echo '<hr>' . $query . '<hr>';
     }
     $temporaryStorage = $this->temporaryStorageFactory->create();
     $table = $temporaryStorage->storeDocumentsFromSelect($query);
     $this->searchHelper->prepareTemporaryTable($table);
     $documents = $this->getDocuments($table);
     $aggregations = $this->aggregationBuilder->build($request, $table);
     $response = ['documents' => $documents, 'aggregations' => $aggregations];
     return $this->responseFactory->create($response);
 }
Ejemplo n.º 9
0
 /**
  * @param RequestInterface $request
  * @return \Magento\Framework\Search\Response\QueryResponse
  */
 public function query(RequestInterface $request)
 {
     try {
         $query = $this->mapper->buildQuery($request);
         $query->limit($this->searchConfig->getResultsLimit());
     } catch (\Exception $e) {
         // fallback engine
         $objectManager = ObjectManager::getInstance();
         return $objectManager->create('Mirasvit\\SearchMysql\\Model\\Adapter')->query($request);
     }
     $temporaryStorage = $this->temporaryStorageFactory->create();
     $table = $temporaryStorage->storeDocumentsFromSelect($query);
     $this->searchHelper->prepareTemporaryTable($table);
     $documents = $this->getDocuments($table);
     $aggregations = $this->aggregationBuilder->build($request, $table);
     $response = ['documents' => $documents, 'aggregations' => $aggregations];
     return $this->responseFactory->create($response);
 }
Ejemplo n.º 10
0
 /**
  * {@inheritdoc}
  */
 protected function buildSearchCollection()
 {
     /** @var \Magento\Catalog\Model\Layer\Search $layer */
     $layer = $this->layerResolver->get();
     if ($this->config->isMultiStoreModeEnabled()) {
         $originalCategory = $layer->getData('current_category');
         // set random category for multi-store mode
         // this mode can be not compatible with some layered navigation
         $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
         $category = $objectManager->create('Magento\\Catalog\\Model\\Category');
         $category->setId(rand(100000, 900000));
         $layer->setData('current_category', $category);
     }
     $collection = $layer->getProductCollection();
     if ($this->config->isMultiStoreModeEnabled()) {
         $layer->setData('current_category', $originalCategory);
     }
     return $collection;
 }
Ejemplo n.º 11
0
 /**
  * @param array    &$to
  * @param array    $words
  * @param int|null $wildcard
  * @return void
  */
 protected function addWord(&$to, $words, $wildcard = null)
 {
     $exceptions = $this->config->getWildcardExceptions();
     if ($wildcard == null) {
         $wildcard = $this->config->getWildcardMode();
     }
     if (!is_array($words)) {
         $words = [$words];
     }
     foreach ($words as $word) {
         if ($wildcard == Config::WILDCARD_PREFIX) {
             $word = $word . ' ';
         } elseif ($wildcard == Config::WILDCARD_SUFFIX) {
             $word = ' ' . $word;
         } elseif ($wildcard == Config::WILDCARD_DISABLED || in_array($word, $exceptions)) {
             $word = ' ' . $word . ' ';
         }
         if (trim($word) !== '') {
             $to[$word] = $word;
         }
     }
     ksort($to);
 }
Ejemplo n.º 12
0
 /**
  * Is enabled?
  *
  * @return bool
  */
 public function isEnabled()
 {
     return $this->config->isGoogleSitelinksEnabled();
 }