Skip to content

Commit 534ccc2

Browse files
authored
Add isTopSearchResult() to SearchQueryCollection.
isTopSearchResult looks up the number of matches in a single query, without loading the records/data.
1 parent 9de4a7e commit 534ccc2

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

app/code/Magento/Search/Model/ResourceModel/Query/Collection.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,36 @@ public function setPopularQueryFilter($storeIds = null)
149149
return $this;
150150
}
151151

152+
/**
153+
* Determines whether a Search Term belongs to the top results for given storeId
154+
*
155+
* @param string $term
156+
* @param int $storeId
157+
* @param int $maxCountCacheableSearchTerms
158+
* @return bool
159+
*/
160+
public function isTopSearchResult(string $term, int $storeId, int $maxCountCacheableSearchTerms):bool
161+
{
162+
$select = $this->getSelect();
163+
$select->reset(\Magento\Framework\DB\Select::FROM);
164+
$select->reset(\Magento\Framework\DB\Select::COLUMNS);
165+
$select->distinct(true);
166+
$select->from(['main_table' => $this->getTable('search_query')], ['query_text']);
167+
$select->where('main_table.store_id IN (?)', $storeId);
168+
$select->where('num_results > 0');
169+
$select->order(['popularity desc']);
170+
171+
$select->limit($maxCountCacheableSearchTerms);
172+
173+
$subQuery = new \Zend_Db_Expr('(' . $select->assemble() . ')');
174+
175+
$select->reset();
176+
$select->from(['result' => $subQuery ], []);
177+
$select->where('result.query_text = ?', $term);
178+
179+
return $this->getSize() > 0;
180+
}
181+
152182
/**
153183
* Set Recent Queries Order
154184
*

0 commit comments

Comments
 (0)