13 use Magento\Search\Model\QueryResultFactory;
46 private $queryResultFactory;
51 private $connectionManager;
61 private $searchIndexNameResolver;
66 private $storeManager;
79 QueryResultFactory $queryResultFactory,
82 StoreManager $storeManager
84 $this->queryResultFactory = $queryResultFactory;
85 $this->connectionManager = $connectionManager;
86 $this->scopeConfig = $scopeConfig;
88 $this->searchIndexNameResolver = $searchIndexNameResolver;
100 if ($this->isSuggestionsAllowed()) {
103 foreach ($this->getSuggestions(
$query) as $suggestion) {
105 if ($isResultsCountEnabled) {
106 $count = isset($suggestion[
'freq']) ? $suggestion[
'freq'] :
null;
108 $result[] = $this->queryResultFactory->create(
110 'queryText' => $suggestion[
'text'],
125 return (
bool)$this->scopeConfig->getValue(
126 self::CONFIG_SUGGESTION_COUNT_RESULTS_ENABLED,
138 $searchSuggestionsCount = $this->getSearchSuggestionsCount();
141 'index' => $this->searchIndexNameResolver->getIndexName(
142 $this->storeManager->getStore()->getId(),
147 'text' =>
$query->getQueryText(),
150 'analyzer' =>
'standard',
151 'size' => $searchSuggestionsCount,
153 'direct_generator' => [
156 'min_word_length' => 3,
165 $result = $this->fetchQuery($suggestRequest);
169 foreach (
$token[
'options'] as $key => $suggestion) {
170 $suggestions[$suggestion[
'score'] .
'_' . $key] = $suggestion;
174 $suggestions = array_slice($suggestions, 0, $searchSuggestionsCount);
184 private function fetchQuery(array
$query)
186 return $this->connectionManager->getConnection()->suggest(
$query);
194 private function getSearchSuggestionsCount()
196 return (
int)$this->scopeConfig->getValue(
197 self::CONFIG_SUGGESTION_COUNT,
205 private function isSuggestionsAllowed()
207 $isSearchSuggestionsEnabled = (bool)$this->scopeConfig->getValue(
208 self::CONFIG_SUGGESTION_ENABLED,
211 $isEnabled = $this->config->isElasticsearchEnabled();
212 $isSuggestionsAllowed = ($isEnabled && $isSearchSuggestionsEnabled);
213 return $isSuggestionsAllowed;
const ELASTICSEARCH_TYPE_DEFAULT
__construct(ScopeConfigInterface $scopeConfig, Config $config, QueryResultFactory $queryResultFactory, ConnectionManager $connectionManager, SearchIndexNameResolver $searchIndexNameResolver, StoreManager $storeManager)
const CONFIG_SUGGESTION_COUNT
const CONFIG_SUGGESTION_ENABLED
const CONFIG_SUGGESTION_COUNT_RESULTS_ENABLED
getItems(QueryInterface $query, $limit=null, $additionalFilters=null)