Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Recommendations.php
Go to the documentation of this file.
1 <?php
7 
16 {
17 
23  protected $_searchQueryModel;
24 
32  public function __construct(
33  \Magento\Framework\Model\ResourceModel\Db\Context $context,
34  \Magento\Search\Model\QueryFactory $queryFactory,
35  $connectionName = null
36  ) {
37  parent::__construct($context, $connectionName);
38  $this->_searchQueryModel = $queryFactory->create();
39  }
40 
46  protected function _construct()
47  {
48  $this->_init('catalogsearch_recommendations', 'id');
49  }
50 
58  public function saveRelatedQueries($queryId, $relatedQueries = [])
59  {
60  $connection = $this->getConnection();
61  $whereOr = [];
62  if (count($relatedQueries) > 0) {
63  $whereOr[] = implode(
64  ' AND ',
65  [
66  $connection->quoteInto('query_id=?', $queryId),
67  $connection->quoteInto('relation_id NOT IN(?)', $relatedQueries)
68  ]
69  );
70  $whereOr[] = implode(
71  ' AND ',
72  [
73  $connection->quoteInto('relation_id = ?', $queryId),
74  $connection->quoteInto('query_id NOT IN(?)', $relatedQueries)
75  ]
76  );
77  } else {
78  $whereOr[] = $connection->quoteInto('query_id = ?', $queryId);
79  $whereOr[] = $connection->quoteInto('relation_id = ?', $queryId);
80  }
81  $whereCond = '(' . implode(') OR (', $whereOr) . ')';
82  $connection->delete($this->getMainTable(), $whereCond);
83 
84  $existsRelatedQueries = $this->getRelatedQueries($queryId);
85  $neededRelatedQueries = array_diff($relatedQueries, $existsRelatedQueries);
86  foreach ($neededRelatedQueries as $relationId) {
87  $connection->insert($this->getMainTable(), ["query_id" => $queryId, "relation_id" => $relationId]);
88  }
89  return $this;
90  }
91 
100  public function getRelatedQueries($queryId, $limit = false, $order = false)
101  {
102  $collection = $this->_searchQueryModel->getResourceCollection();
103  $connection = $this->getConnection();
104 
105  $queryIdCond = $connection->quoteInto('main_table.query_id IN (?)', $queryId);
106 
107  $collection->getSelect()->join(
108  ['sr' => $collection->getTable('catalogsearch_recommendations')],
109  '(sr.query_id=main_table.query_id OR sr.relation_id=main_table.query_id) AND ' . $queryIdCond
110  )->reset(
111  \Magento\Framework\DB\Select::COLUMNS
112  )->columns(
113  [
114  'rel_id' => $connection->getCheckSql(
115  'main_table.query_id=sr.query_id',
116  'sr.relation_id',
117  'sr.query_id'
118  ),
119  ]
120  );
121  if (!empty($limit)) {
122  $collection->getSelect()->limit($limit);
123  }
124  if (!empty($order)) {
125  $collection->getSelect()->order($order);
126  }
127 
128  $queryIds = $connection->fetchCol($collection->getSelect());
129  return $queryIds;
130  }
131 
140  public function getRecommendationsByQuery($query, $params, $searchRecommendationsCount)
141  {
142  $this->_searchQueryModel->loadByQueryText($query);
143 
144  if (isset($params['store_id'])) {
145  $this->_searchQueryModel->setStoreId($params['store_id']);
146  }
147  $relatedQueriesIds = $this->loadByQuery($query, $searchRecommendationsCount);
148  $relatedQueries = [];
149  if (count($relatedQueriesIds)) {
150  $connection = $this->getConnection();
151  $mainTable = $this->_searchQueryModel->getResourceCollection()->getMainTable();
152  $select = $connection->select()->from(
153  ['main_table' => $mainTable],
154  ['query_text', 'num_results']
155  )->where(
156  'query_id IN(?)',
157  $relatedQueriesIds
158  )->where(
159  'num_results > 0'
160  );
161  $relatedQueries = $connection->fetchAll($select);
162  }
163 
164  return $relatedQueries;
165  }
166 
174  protected function loadByQuery($query, $searchRecommendationsCount)
175  {
176  $connection = $this->getConnection();
177  $queryId = $this->_searchQueryModel->getId();
178  $relatedQueries = $this->getRelatedQueries($queryId, $searchRecommendationsCount, 'num_results DESC');
179  if ($searchRecommendationsCount - count($relatedQueries) < 1) {
180  return $relatedQueries;
181  }
182 
183  $queryWords = [$query];
184  if (strpos($query, ' ') !== false) {
185  $queryWords = array_unique(array_merge($queryWords, explode(' ', $query)));
186  foreach ($queryWords as $key => $word) {
187  $queryWords[$key] = trim($word);
188  if (strlen($word) < 3) {
189  unset($queryWords[$key]);
190  }
191  }
192  }
193 
194  $likeCondition = [];
195  foreach ($queryWords as $word) {
196  $likeCondition[] = $connection->quoteInto('query_text LIKE ?', $word . '%');
197  }
198  $likeCondition = implode(' OR ', $likeCondition);
199 
200  $select = $connection->select()->from(
201  $this->_searchQueryModel->getResource()->getMainTable(),
202  ['query_id']
203  )->where(
204  new \Zend_Db_Expr($likeCondition)
205  )->where(
206  'store_id=?',
207  $this->_searchQueryModel->getStoreId()
208  )->order(
209  'num_results DESC'
210  )->limit(
211  $searchRecommendationsCount + 1
212  );
213  $ids = $connection->fetchCol($select);
214 
215  if (!is_array($ids)) {
216  $ids = [];
217  }
218 
219  $key = array_search($queryId, $ids);
220  if ($key !== false) {
221  unset($ids[$key]);
222  }
223  $ids = array_unique(array_merge($relatedQueries, $ids));
224  $ids = array_slice($ids, 0, $searchRecommendationsCount);
225  return $ids;
226  }
227 }
$order
Definition: order.php:55
getRecommendationsByQuery($query, $params, $searchRecommendationsCount)
$connection
Definition: bulk.php:13
getRelatedQueries($queryId, $limit=false, $order=false)
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Search\Model\QueryFactory $queryFactory, $connectionName=null)