/** * After change Catalog Search Type process * * @return $this */ protected function _afterSave() { $newValue = $this->getValue(); $oldValue = $this->_config->getValue(Fulltext::XML_PATH_CATALOG_SEARCH_TYPE, $this->getScope(), $this->getScopeId()); if ($newValue != $oldValue) { $this->_catalogSearchFulltext->resetSearchResults(); } return $this; }
/** * After change Catalog Search Type process * * @return $this */ protected function _afterSave() { $newValue = $this->getValue(); $oldValue = $this->_config->getValue(Fulltext::XML_PATH_CATALOG_SEARCH_TYPE, $this->getScope(), $this->getScopeId()); if ($newValue != $oldValue) { $this->_catalogSearchFulltext->resetSearchResults(); $indexer = $this->indexerFactory->create(); $indexer->load(FulltextIndexer::INDEXER_ID); $indexer->invalidate(); } return $this; }
/** * Prepare results for query * * @param \Magento\CatalogSearch\Model\Fulltext $object * @param string $queryText * @param \Magento\CatalogSearch\Model\Query $query * @return $this */ public function prepareResult($object, $queryText, $query) { $adapter = $this->_getWriteAdapter(); if (!$query->getIsProcessed()) { $searchType = $object->getSearchType($query->getStoreId()); $bind = array(); $like = array(); $likeCond = ''; if ($searchType == \Magento\CatalogSearch\Model\Fulltext::SEARCH_TYPE_LIKE || $searchType == \Magento\CatalogSearch\Model\Fulltext::SEARCH_TYPE_COMBINE) { $words = $this->filter->splitWords($queryText, array('uniqueOnly' => true, 'wordsQty' => $query->getMaxQueryWords())); foreach ($words as $word) { $like[] = $this->_resourceHelper->getCILike('s.data_index', $word, array('position' => 'any')); } if ($like) { $likeCond = '(' . join(' OR ', $like) . ')'; } } $mainTableAlias = 's'; $fields = array('query_id' => new \Zend_Db_Expr($query->getId()), 'product_id'); $select = $adapter->select()->from(array($mainTableAlias => $this->getMainTable()), $fields)->joinInner(array('e' => $this->getTable('catalog_product_entity')), 'e.entity_id = s.product_id', array())->where($mainTableAlias . '.store_id = ?', (int) $query->getStoreId()); $where = ''; if ($searchType == \Magento\CatalogSearch\Model\Fulltext::SEARCH_TYPE_FULLTEXT || $searchType == \Magento\CatalogSearch\Model\Fulltext::SEARCH_TYPE_COMBINE) { $preparedTerms = $this->_resourceHelper->prepareTerms($queryText, $query->getMaxQueryWords()); $bind[':query'] = implode(' ', $preparedTerms[0]); $where = $this->_resourceHelper->chooseFulltext($this->getMainTable(), $mainTableAlias, $select); } if ($likeCond != '' && $searchType == \Magento\CatalogSearch\Model\Fulltext::SEARCH_TYPE_COMBINE) { $where .= ($where ? ' OR ' : '') . $likeCond; } elseif ($likeCond != '' && $searchType == \Magento\CatalogSearch\Model\Fulltext::SEARCH_TYPE_LIKE) { $select->columns(array('relevance' => new \Zend_Db_Expr(0))); $where = $likeCond; } if ($where != '') { $select->where($where); } $sql = $adapter->insertFromSelect($select, $this->getTable('catalogsearch_result'), array(), \Magento\Framework\DB\Adapter\AdapterInterface::INSERT_ON_DUPLICATE); $adapter->query($sql, $bind); $query->setIsProcessed(1); } return $this; }
/** * Add search query filter * * @param string $query * @return $this */ public function addSearchFilter($query) { $this->_catalogSearchFulltext->prepareResult(); $this->getSelect()->joinInner(array('search_result' => $this->getTable('catalogsearch_result')), $this->getConnection()->quoteInto('search_result.product_id=e.entity_id AND search_result.query_id=?', $this->_getQuery()->getId()), array('relevance' => 'relevance')); return $this; }