1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Elasticsearch\SearchAdapter\Query\Preprocessor;
use Magento\Framework\Filesystem\Directory\ReadFactory;
use Magento\Elasticsearch\Model\Adapter\Index\Config\EsConfigInterface;
use Magento\Framework\Search\Adapter\Preprocessor\PreprocessorInterface;
use Magento\Framework\Module\Dir;
/**
* @api
* @since 100.1.0
*/
class Stopwords implements PreprocessorInterface
{
/**
* Cache id for elasticsearch stopwords
*/
const CACHE_ID = 'elasticsearch_stopwords';
/**
* Stopwords file modification time gap, seconds
*/
const STOPWORDS_FILE_MODIFICATION_TIME_GAP = 900;
/**
* @var \Magento\Store\Model\StoreManagerInterface
* @since 100.1.0
*/
protected $storeManager;
/**
* @var \Magento\Framework\Locale\Resolver
* @since 100.1.0
*/
protected $localeResolver;
/**
* @var ReadFactory
* @since 100.1.0
*/
protected $readFactory;
/**
* @var \Magento\Framework\App\Cache\Type\Config
* @since 100.1.0
*/
protected $configCache;
/**
* @var EsConfigInterface
* @since 100.1.0
*/
protected $esConfig;
/**
* @var \Magento\Framework\Module\Dir\Reader
* @since 100.1.0
*/
protected $moduleDirReader;
/**
* @var string
*/
private $stopwordsModule;
/**
* @var string
*/
private $stopwordsDirectory;
/**
* @var \Magento\Framework\Serialize\SerializerInterface
*/
private $serializer;
/**
* Initialize dependencies.
*
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\Locale\Resolver $localeResolver
* @param ReadFactory $readFactory
* @param \Magento\Framework\App\Cache\Type\Config $configCache
* @param EsConfigInterface $esConfig
* @param \Magento\Framework\Module\Dir\Reader $moduleDirReader
* @param string $stopwordsModule
* @param string $stopwordsDirectory
*/
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\Locale\Resolver $localeResolver,
ReadFactory $readFactory,
\Magento\Framework\App\Cache\Type\Config $configCache,
EsConfigInterface $esConfig,
\Magento\Framework\Module\Dir\Reader $moduleDirReader,
$stopwordsModule = '',
$stopwordsDirectory = ''
) {
$this->storeManager = $storeManager;
$this->localeResolver = $localeResolver;
$this->readFactory = $readFactory;
$this->configCache = $configCache;
$this->esConfig = $esConfig;
$this->moduleDirReader = $moduleDirReader;
$this->stopwordsModule = $stopwordsModule;
$this->stopwordsDirectory = $stopwordsDirectory;
}
/**
* {@inheritdoc}
* @since 100.1.0
*/
public function process($query)
{
$stopwords = $this->getStopwordsList();
$queryParts = explode(' ', $query);
$query = implode(' ', array_diff($queryParts, $stopwords));
return trim($query);
}
/**
* Get stopwords list for current locale
*
* @return array
* @since 100.1.0
*/
protected function getStopwordsList()
{
$filename = $this->getStopwordsFile();
$fileDir = $this->moduleDirReader->getModuleDir(Dir::MODULE_ETC_DIR, $this->stopwordsModule)
. '/' . $this->stopwordsDirectory;
$source = $this->readFactory->create($fileDir);
$fileStats = $source->stat($filename);
if (((time() - $fileStats['mtime']) > self::STOPWORDS_FILE_MODIFICATION_TIME_GAP)
&& ($cachedValue = $this->configCache->load(self::CACHE_ID))) {
$stopwords = $this->getSerializer()->unserialize($cachedValue);
} else {
$fileContent = $source->readFile($filename);
$stopwords = explode("\n", $fileContent);
$this->configCache->save($this->getSerializer()->serialize($stopwords), self::CACHE_ID);
}
return $stopwords;
}
/**
* Get stopwords file for current locale
*
* @return string
* @since 100.1.0
*/
protected function getStopwordsFile()
{
$stopwordsInfo = $this->esConfig->getStopwordsInfo();
$storeId = $this->storeManager->getStore()->getId();
$this->localeResolver->emulate($storeId);
$locale = $this->localeResolver->getLocale();
$stopwordsFile = isset($stopwordsInfo[$locale]) ? $stopwordsInfo[$locale] : $stopwordsInfo['default'];
return $stopwordsFile;
}
/**
* Get serializer
*
* @return \Magento\Framework\Serialize\SerializerInterface
* @deprecated 100.2.0
*/
private function getSerializer()
{
if (null === $this->serializer) {
$this->serializer = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\SerializerInterface::class);
}
return $this->serializer;
}
}