123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Elasticsearch\Model\DataProvider;
- use Magento\Store\Model\ScopeInterface;
- use Magento\Search\Model\QueryInterface;
- use Magento\AdvancedSearch\Model\SuggestedQueriesInterface;
- use Magento\Elasticsearch\Model\Config;
- use Magento\Elasticsearch\SearchAdapter\ConnectionManager;
- use Magento\Search\Model\QueryResultFactory;
- use Magento\Framework\App\Config\ScopeConfigInterface;
- use Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver;
- use Magento\Store\Model\StoreManagerInterface as StoreManager;
- /**
- * Class Suggestions
- */
- class Suggestions implements SuggestedQueriesInterface
- {
- /**
- * @deprecated
- * @see SuggestedQueriesInterface::SEARCH_SUGGESTION_COUNT
- */
- const CONFIG_SUGGESTION_COUNT = 'catalog/search/search_suggestion_count';
- /**
- * @deprecated
- * @see SuggestedQueriesInterface::SEARCH_SUGGESTION_COUNT_RESULTS_ENABLED
- */
- const CONFIG_SUGGESTION_COUNT_RESULTS_ENABLED = 'catalog/search/search_suggestion_count_results_enabled';
- /**
- * @deprecated
- * @see SuggestedQueriesInterface::SEARCH_SUGGESTION_ENABLED
- */
- const CONFIG_SUGGESTION_ENABLED = 'catalog/search/search_suggestion_enabled';
- /**
- * @var Config
- */
- private $config;
- /**
- * @var QueryResultFactory
- */
- private $queryResultFactory;
- /**
- * @var ConnectionManager
- */
- private $connectionManager;
- /**
- * @var ScopeConfigInterface
- */
- private $scopeConfig;
- /**
- * @var SearchIndexNameResolver
- */
- private $searchIndexNameResolver;
- /**
- * @var StoreManager
- */
- private $storeManager;
- /**
- * Suggestions constructor.
- *
- * @param ScopeConfigInterface $scopeConfig
- * @param Config $config
- * @param QueryResultFactory $queryResultFactory
- * @param ConnectionManager $connectionManager
- * @param SearchIndexNameResolver $searchIndexNameResolver
- * @param StoreManager $storeManager
- */
- public function __construct(
- ScopeConfigInterface $scopeConfig,
- Config $config,
- QueryResultFactory $queryResultFactory,
- ConnectionManager $connectionManager,
- SearchIndexNameResolver $searchIndexNameResolver,
- StoreManager $storeManager
- ) {
- $this->queryResultFactory = $queryResultFactory;
- $this->connectionManager = $connectionManager;
- $this->scopeConfig = $scopeConfig;
- $this->config = $config;
- $this->searchIndexNameResolver = $searchIndexNameResolver;
- $this->storeManager = $storeManager;
- }
- /**
- * @inheritdoc
- */
- public function getItems(QueryInterface $query)
- {
- $result = [];
- if ($this->isSuggestionsAllowed()) {
- $isResultsCountEnabled = $this->isResultsCountEnabled();
- foreach ($this->getSuggestions($query) as $suggestion) {
- $count = null;
- if ($isResultsCountEnabled) {
- $count = isset($suggestion['freq']) ? $suggestion['freq'] : null;
- }
- $result[] = $this->queryResultFactory->create(
- [
- 'queryText' => $suggestion['text'],
- 'resultsCount' => $count,
- ]
- );
- }
- }
- return $result;
- }
- /**
- * @inheritdoc
- */
- public function isResultsCountEnabled()
- {
- return $this->scopeConfig->isSetFlag(
- self::CONFIG_SUGGESTION_COUNT_RESULTS_ENABLED,
- ScopeInterface::SCOPE_STORE
- );
- }
- /**
- * Get Suggestions
- *
- * @param QueryInterface $query
- *
- * @return array
- * @throws \Magento\Framework\Exception\NoSuchEntityException
- */
- private function getSuggestions(QueryInterface $query)
- {
- $suggestions = [];
- $searchSuggestionsCount = $this->getSearchSuggestionsCount();
- $suggestRequest = [
- 'index' => $this->searchIndexNameResolver->getIndexName(
- $this->storeManager->getStore()->getId(),
- Config::ELASTICSEARCH_TYPE_DEFAULT
- ),
- 'body' => [
- 'suggestions' => [
- 'text' => $query->getQueryText(),
- 'phrase' => [
- 'field' => '_all',
- 'analyzer' => 'standard',
- 'size' => $searchSuggestionsCount,
- 'max_errors' => 2,
- 'direct_generator' => [
- [
- 'field' => '_all',
- 'min_word_length' => 3,
- 'min_doc_freq' => 1
- ]
- ],
- ]
- ]
- ]
- ];
- $result = $this->fetchQuery($suggestRequest);
- if (is_array($result)) {
- foreach ($result['suggestions'] as $token) {
- foreach ($token['options'] as $key => $suggestion) {
- $suggestions[$suggestion['score'] . '_' . $key] = $suggestion;
- }
- }
- ksort($suggestions);
- $suggestions = array_slice($suggestions, 0, $searchSuggestionsCount);
- }
- return $suggestions;
- }
- /**
- * Fetch Query
- *
- * @param array $query
- * @return array
- */
- private function fetchQuery(array $query)
- {
- return $this->connectionManager->getConnection()->suggest($query);
- }
- /**
- * Get search suggestions Max Count from config
- *
- * @return int
- */
- private function getSearchSuggestionsCount()
- {
- return (int)$this->scopeConfig->getValue(
- self::CONFIG_SUGGESTION_COUNT,
- ScopeInterface::SCOPE_STORE
- );
- }
- /**
- * Is Search Suggestions Allowed
- *
- * @return bool
- */
- private function isSuggestionsAllowed()
- {
- $isSuggestionsEnabled = $this->scopeConfig->isSetFlag(
- self::CONFIG_SUGGESTION_ENABLED,
- ScopeInterface::SCOPE_STORE
- );
- $isEnabled = $this->config->isElasticsearchEnabled();
- $isSuggestionsAllowed = ($isEnabled && $isSuggestionsEnabled);
- return $isSuggestionsAllowed;
- }
- }
|