/** * Retrieve query model object * * @return Query */ public function getQuery() { if (!$this->_query) { $this->_query = $this->_queryFactory->create()->loadByQuery($this->getQueryText()); if (!$this->_query->getId()) { $this->_query->setQueryText($this->getQueryText()); } } return $this->_query; }
/** * 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; }