Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Suggestions.php
Go to the documentation of this file.
1 <?php
7 
13 use Magento\Search\Model\QueryResultFactory;
17 
19 {
24  const CONFIG_SUGGESTION_COUNT = 'catalog/search/search_suggestion_count';
25 
30  const CONFIG_SUGGESTION_COUNT_RESULTS_ENABLED = 'catalog/search/search_suggestion_count_results_enabled';
31 
36  const CONFIG_SUGGESTION_ENABLED = 'catalog/search/search_suggestion_enabled';
37 
41  private $config;
42 
46  private $queryResultFactory;
47 
51  private $connectionManager;
52 
56  private $scopeConfig;
57 
61  private $searchIndexNameResolver;
62 
66  private $storeManager;
67 
76  public function __construct(
77  ScopeConfigInterface $scopeConfig,
78  Config $config,
79  QueryResultFactory $queryResultFactory,
80  ConnectionManager $connectionManager,
81  SearchIndexNameResolver $searchIndexNameResolver,
82  StoreManager $storeManager
83  ) {
84  $this->queryResultFactory = $queryResultFactory;
85  $this->connectionManager = $connectionManager;
86  $this->scopeConfig = $scopeConfig;
87  $this->config = $config;
88  $this->searchIndexNameResolver = $searchIndexNameResolver;
89  $this->storeManager = $storeManager;
90  }
91 
97  public function getItems(QueryInterface $query, $limit = null, $additionalFilters = null)
98  {
99  $result = [];
100  if ($this->isSuggestionsAllowed()) {
101  $isResultsCountEnabled = $this->isResultsCountEnabled();
102 
103  foreach ($this->getSuggestions($query) as $suggestion) {
104  $count = null;
105  if ($isResultsCountEnabled) {
106  $count = isset($suggestion['freq']) ? $suggestion['freq'] : null;
107  }
108  $result[] = $this->queryResultFactory->create(
109  [
110  'queryText' => $suggestion['text'],
111  'resultsCount' => $count,
112  ]
113  );
114  }
115  }
116 
117  return $result;
118  }
119 
123  public function isResultsCountEnabled()
124  {
125  return (bool)$this->scopeConfig->getValue(
126  self::CONFIG_SUGGESTION_COUNT_RESULTS_ENABLED,
128  );
129  }
130 
135  private function getSuggestions(QueryInterface $query)
136  {
137  $suggestions = [];
138  $searchSuggestionsCount = $this->getSearchSuggestionsCount();
139 
140  $suggestRequest = [
141  'index' => $this->searchIndexNameResolver->getIndexName(
142  $this->storeManager->getStore()->getId(),
144  ),
145  'body' => [
146  'suggestions' => [
147  'text' => $query->getQueryText(),
148  'phrase' => [
149  'field' => '_all',
150  'analyzer' => 'standard',
151  'size' => $searchSuggestionsCount,
152  'max_errors' => 2,
153  'direct_generator' => [
154  [
155  'field' => '_all',
156  'min_word_length' => 3,
157  'min_doc_freq' => 1
158  ]
159  ],
160  ]
161  ]
162  ]
163  ];
164 
165  $result = $this->fetchQuery($suggestRequest);
166 
167  if (is_array($result)) {
168  foreach ($result['suggestions'] as $token) {
169  foreach ($token['options'] as $key => $suggestion) {
170  $suggestions[$suggestion['score'] . '_' . $key] = $suggestion;
171  }
172  }
173  ksort($suggestions);
174  $suggestions = array_slice($suggestions, 0, $searchSuggestionsCount);
175  }
176 
177  return $suggestions;
178  }
179 
184  private function fetchQuery(array $query)
185  {
186  return $this->connectionManager->getConnection()->suggest($query);
187  }
188 
194  private function getSearchSuggestionsCount()
195  {
196  return (int)$this->scopeConfig->getValue(
197  self::CONFIG_SUGGESTION_COUNT,
199  );
200  }
201 
205  private function isSuggestionsAllowed()
206  {
207  $isSearchSuggestionsEnabled = (bool)$this->scopeConfig->getValue(
208  self::CONFIG_SUGGESTION_ENABLED,
210  );
211  $isEnabled = $this->config->isElasticsearchEnabled();
212  $isSuggestionsAllowed = ($isEnabled && $isSearchSuggestionsEnabled);
213  return $isSuggestionsAllowed;
214  }
215 }
$config
Definition: fraud_order.php:17
$count
Definition: recent.phtml:13
$storeManager
__construct(ScopeConfigInterface $scopeConfig, Config $config, QueryResultFactory $queryResultFactory, ConnectionManager $connectionManager, SearchIndexNameResolver $searchIndexNameResolver, StoreManager $storeManager)
Definition: Suggestions.php:76
getItems(QueryInterface $query, $limit=null, $additionalFilters=null)
Definition: Suggestions.php:97