/** * Set search query text to filter * * @param string $query * @return $this */ public function setQueryFilter($query) { $ifSynonymFor = $this->getConnection()->getIfNullSql('synonym_for', 'query_text'); $this->getSelect()->reset(\Zend_Db_Select::FROM)->distinct(true)->from(array('main_table' => $this->getTable('catalogsearch_query')), array('query' => $ifSynonymFor, 'num_results'))->where('num_results > 0 AND display_in_terms = 1 AND query_text LIKE ?', $this->_resourceHelper->addLikeEscape($query, array('position' => 'start')))->order('popularity ' . \Magento\Framework\DB\Select::SQL_DESC); if ($this->getStoreId()) { $this->getSelect()->where('store_id = ?', (int) $this->getStoreId()); } return $this; }
/** * Multi add entities data to fulltext search table * * @param int $storeId * @param array $entityIndexes * @param string $entity 'product'|'cms' * @return $this */ public function saveEntityIndexes($storeId, $entityIndexes, $entity = 'product') { $data = array(); $storeId = (int) $storeId; foreach ($entityIndexes as $entityId => $index) { $data[] = array('product_id' => (int) $entityId, 'store_id' => $storeId, 'data_index' => $index); } if ($data) { $this->_resourceHelper->insertOnDuplicate($this->getMainTable(), $data, array('data_index')); } 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; }