1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\Indexes; |
5
|
|
|
|
6
|
|
|
use Exception; |
7
|
|
|
use Firesphere\SolrSearch\Factories\QueryComponentFactory; |
8
|
|
|
use Firesphere\SolrSearch\Helpers\SolrLogger; |
9
|
|
|
use Firesphere\SolrSearch\Helpers\Synonyms; |
10
|
|
|
use Firesphere\SolrSearch\Interfaces\ConfigStore; |
11
|
|
|
use Firesphere\SolrSearch\Queries\BaseQuery; |
12
|
|
|
use Firesphere\SolrSearch\Results\SearchResult; |
13
|
|
|
use Firesphere\SolrSearch\Services\SchemaService; |
14
|
|
|
use Firesphere\SolrSearch\Services\SolrCoreService; |
15
|
|
|
use Firesphere\SolrSearch\Traits\BaseIndexTrait; |
16
|
|
|
use Firesphere\SolrSearch\Traits\GetterSetterTrait; |
17
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
18
|
|
|
use LogicException; |
19
|
|
|
use SilverStripe\Control\Director; |
20
|
|
|
use SilverStripe\Core\Config\Config; |
21
|
|
|
use SilverStripe\Core\Config\Configurable; |
22
|
|
|
use SilverStripe\Core\Extensible; |
23
|
|
|
use SilverStripe\Core\Injector\Injector; |
24
|
|
|
use SilverStripe\Dev\Deprecation; |
25
|
|
|
use SilverStripe\ORM\ValidationException; |
26
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
27
|
|
|
use SilverStripe\View\ArrayData; |
28
|
|
|
use Solarium\Core\Client\Adapter\Guzzle; |
29
|
|
|
use Solarium\Core\Client\Client; |
30
|
|
|
use Solarium\QueryType\Select\Query\Query; |
31
|
|
|
use Solarium\QueryType\Select\Result\Result; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Class BaseIndex |
35
|
|
|
* @package Firesphere\SolrSearch\Indexes |
36
|
|
|
*/ |
37
|
|
|
abstract class BaseIndex |
38
|
|
|
{ |
39
|
|
|
use Extensible; |
40
|
|
|
use Configurable; |
41
|
|
|
use GetterSetterTrait; |
42
|
|
|
use BaseIndexTrait; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Field types that can be added |
46
|
|
|
* Used in init to call build methods from configuration yml |
47
|
|
|
* @array |
48
|
|
|
*/ |
49
|
|
|
private static $fieldTypes = [ |
50
|
|
|
'FulltextFields', |
51
|
|
|
'SortFields', |
52
|
|
|
'FilterFields', |
53
|
|
|
'BoostedFields', |
54
|
|
|
'CopyFields', |
55
|
|
|
'DefaultField', |
56
|
|
|
'FacetFields', |
57
|
|
|
'StoredFields' |
58
|
|
|
]; |
59
|
|
|
/** |
60
|
|
|
* @var \Solarium\Core\Query\Result\Result |
61
|
|
|
*/ |
62
|
|
|
protected $rawQuery; |
63
|
|
|
/** |
64
|
|
|
* @var SchemaService |
65
|
|
|
*/ |
66
|
|
|
protected $schemaService; |
67
|
|
|
/** |
68
|
|
|
* @var QueryComponentFactory |
69
|
|
|
*/ |
70
|
|
|
protected $queryFactory; |
71
|
|
|
/** |
72
|
|
|
* The query terms as an array |
73
|
|
|
* @var array |
74
|
|
|
*/ |
75
|
|
|
protected $queryTerms = []; |
76
|
|
|
/** |
77
|
|
|
* @var bool |
78
|
|
|
*/ |
79
|
|
|
private $retry = false; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* BaseIndex constructor. |
83
|
|
|
*/ |
84
|
68 |
|
public function __construct() |
85
|
|
|
{ |
86
|
|
|
// Set up the client |
87
|
68 |
|
$config = Config::inst()->get(SolrCoreService::class, 'config'); |
88
|
68 |
|
$config['endpoint'] = $this->getConfig($config['endpoint']); |
89
|
68 |
|
$this->client = new Client($config); |
90
|
68 |
|
$this->client->setAdapter(new Guzzle()); |
91
|
|
|
|
92
|
|
|
// Set up the schema service, only used in the generation of the schema |
93
|
68 |
|
$schemaService = Injector::inst()->get(SchemaService::class, false); |
94
|
68 |
|
$schemaService->setIndex($this); |
95
|
68 |
|
$schemaService->setStore(Director::isDev()); |
96
|
68 |
|
$this->schemaService = $schemaService; |
97
|
68 |
|
$this->queryFactory = Injector::inst()->get(QueryComponentFactory::class, false); |
98
|
|
|
|
99
|
68 |
|
$this->extend('onBeforeInit'); |
100
|
68 |
|
$this->init(); |
101
|
68 |
|
$this->extend('onAfterInit'); |
102
|
68 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Build a full config for all given endpoints |
106
|
|
|
* This is to add the current index to e.g. an index or select |
107
|
|
|
* @param array $endpoints |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
68 |
|
public function getConfig($endpoints): array |
111
|
|
|
{ |
112
|
68 |
|
foreach ($endpoints as $host => $endpoint) { |
113
|
68 |
|
$endpoints[$host]['core'] = $this->getIndexName(); |
114
|
|
|
} |
115
|
|
|
|
116
|
68 |
|
return $endpoints; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
abstract public function getIndexName(); |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Required to initialise the fields. |
126
|
|
|
* It's loaded in to the non-static properties for backward compatibility with FTS |
127
|
|
|
* Also, it's a tad easier to use this way, loading the other way around would be very |
128
|
|
|
* memory intensive, as updating the config for each item is not efficient |
129
|
|
|
*/ |
130
|
67 |
|
public function init() |
131
|
|
|
{ |
132
|
67 |
|
if (!self::config()->get($this->getIndexName())) { |
133
|
67 |
|
Deprecation::notice('5', 'Please set an index name and use a config yml'); |
134
|
|
|
|
135
|
|
|
// If the old init method is found, skip the config based init |
136
|
67 |
|
if (!count($this->getClasses())) { |
137
|
|
|
Deprecation::notice( |
138
|
|
|
'5', |
139
|
|
|
'No classes to add to index found, did you maybe call parent::init() too early?' |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
67 |
|
return; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
67 |
|
$this->initFromConfig(); |
148
|
67 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Generate the config from yml if possible |
152
|
|
|
*/ |
153
|
67 |
|
protected function initFromConfig(): void |
154
|
|
|
{ |
155
|
67 |
|
$config = self::config()->get($this->getIndexName()); |
156
|
|
|
|
157
|
67 |
|
if (!array_key_exists('Classes', $config)) { |
158
|
|
|
throw new LogicException('No classes to index found!'); |
159
|
|
|
} |
160
|
|
|
|
161
|
67 |
|
$this->setClasses($config['Classes']); |
162
|
|
|
|
163
|
|
|
// For backward compatibility, copy the config to the protected values |
164
|
|
|
// Saves doubling up further down the line |
165
|
67 |
|
foreach (self::$fieldTypes as $type) { |
166
|
67 |
|
if (array_key_exists($type, $config)) { |
167
|
67 |
|
$method = 'set' . $type; |
168
|
67 |
|
$this->$method($config[$type]); |
169
|
|
|
} |
170
|
|
|
} |
171
|
67 |
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Default returns a SearchResult. It can return an ArrayData if FTS Compat is enabled |
175
|
|
|
* |
176
|
|
|
* @param BaseQuery $query |
177
|
|
|
* @return SearchResult|ArrayData|mixed |
178
|
|
|
* @throws GuzzleException |
179
|
|
|
* @throws ValidationException |
180
|
|
|
*/ |
181
|
5 |
|
public function doSearch(BaseQuery $query) |
182
|
|
|
{ |
183
|
|
|
// Build the actual query parameters |
184
|
5 |
|
$clientQuery = $this->buildSolrQuery($query); |
185
|
|
|
|
186
|
5 |
|
$this->extend('onBeforeSearch', $query, $clientQuery); |
187
|
|
|
|
188
|
|
|
try { |
189
|
5 |
|
$result = $this->client->select($clientQuery); |
190
|
|
|
} catch (Exception $e) { |
191
|
|
|
$logger = new SolrLogger(); |
192
|
|
|
$logger->saveSolrLog('Query'); |
193
|
|
|
} |
194
|
|
|
|
195
|
5 |
|
$this->rawQuery = $result; |
196
|
|
|
|
197
|
|
|
// Handle the after search first. This gets a raw search result |
198
|
5 |
|
$this->extend('onAfterSearch', $result); |
199
|
5 |
|
$searchResult = new SearchResult($result, $query, $this); |
200
|
5 |
|
if ($this->doRetry($query, $result, $searchResult)) { |
201
|
1 |
|
return $this->spellcheckRetry($query, $searchResult); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
// And then handle the search results, which is a useable object for SilverStripe |
205
|
5 |
|
$this->extend('updateSearchResults', $searchResult); |
206
|
|
|
|
207
|
5 |
|
return $searchResult; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param BaseQuery $query |
212
|
|
|
* @return Query |
213
|
|
|
*/ |
214
|
5 |
|
public function buildSolrQuery(BaseQuery $query): Query |
215
|
|
|
{ |
216
|
5 |
|
$clientQuery = $this->client->createSelect(); |
217
|
5 |
|
$factory = $this->buildFactory($query, $clientQuery); |
218
|
|
|
|
219
|
5 |
|
$clientQuery = $factory->buildQuery(); |
220
|
5 |
|
$this->queryTerms = $factory->getQueryArray(); |
221
|
|
|
|
222
|
5 |
|
$queryData = implode(' ', $this->queryTerms); |
223
|
5 |
|
$clientQuery->setQuery($queryData); |
224
|
|
|
|
225
|
5 |
|
return $clientQuery; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param BaseQuery $query |
230
|
|
|
* @param Query $clientQuery |
231
|
|
|
* @return QueryComponentFactory|mixed |
232
|
|
|
*/ |
233
|
5 |
|
protected function buildFactory(BaseQuery $query, Query $clientQuery) |
234
|
|
|
{ |
235
|
5 |
|
$factory = $this->queryFactory; |
236
|
|
|
|
237
|
5 |
|
$helper = $clientQuery->getHelper(); |
238
|
|
|
|
239
|
5 |
|
$factory->setQuery($query); |
240
|
5 |
|
$factory->setClientQuery($clientQuery); |
241
|
5 |
|
$factory->setHelper($helper); |
242
|
5 |
|
$factory->setIndex($this); |
243
|
|
|
|
244
|
5 |
|
return $factory; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Check if the query should be retried with spellchecking |
249
|
|
|
* Conditions are: |
250
|
|
|
* It is not already a retry with spellchecking |
251
|
|
|
* Spellchecking is enabled |
252
|
|
|
* If spellchecking is enabled and nothing is found OR it should follow spellchecking none the less |
253
|
|
|
* There is a spellcheck output |
254
|
|
|
* @param BaseQuery $query |
255
|
|
|
* @param Result $result |
256
|
|
|
* @param SearchResult $searchResult |
257
|
|
|
* @return bool |
258
|
|
|
*/ |
259
|
5 |
|
protected function doRetry(BaseQuery $query, Result $result, SearchResult $searchResult): bool |
260
|
|
|
{ |
261
|
5 |
|
return !$this->retry && |
262
|
5 |
|
$query->hasSpellcheck() && |
263
|
5 |
|
($query->shouldFollowSpellcheck() || $result->getNumFound() === 0) && |
264
|
5 |
|
$searchResult->getCollatedSpellcheck(); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Retry the query with the first collated spellcheck found. |
269
|
|
|
* |
270
|
|
|
* @param BaseQuery $query |
271
|
|
|
* @param SearchResult $searchResult |
272
|
|
|
* @return SearchResult|mixed|ArrayData |
273
|
|
|
* @throws GuzzleException |
274
|
|
|
* @throws ValidationException |
275
|
|
|
*/ |
276
|
1 |
|
protected function spellcheckRetry(BaseQuery $query, SearchResult $searchResult) |
277
|
|
|
{ |
278
|
1 |
|
$terms = $query->getTerms(); |
279
|
1 |
|
$spellChecked = $searchResult->getCollatedSpellcheck(); |
280
|
|
|
// Remove the fuzzyness from the collated check |
281
|
1 |
|
$term = preg_replace('/~\d+/', '', $spellChecked); |
282
|
1 |
|
$terms[0]['text'] = $term; |
283
|
1 |
|
$query->setTerms($terms); |
284
|
1 |
|
$this->retry = true; |
285
|
|
|
|
286
|
1 |
|
return $this->doSearch($query); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @return array |
291
|
|
|
*/ |
292
|
6 |
|
public function getFieldsForIndexing(): array |
293
|
|
|
{ |
294
|
6 |
|
$facets = []; |
295
|
6 |
|
foreach ($this->getFacetFields() as $field) { |
296
|
5 |
|
$facets[] = $field['Field']; |
297
|
|
|
} |
298
|
|
|
// Return values to make the key reset |
299
|
|
|
// Only return unique values |
300
|
|
|
// And make it all a single array |
301
|
6 |
|
$fields = array_values( |
302
|
6 |
|
array_unique( |
303
|
6 |
|
array_merge( |
304
|
6 |
|
$this->getFulltextFields(), |
305
|
6 |
|
$this->getSortFields(), |
306
|
6 |
|
$facets, |
307
|
6 |
|
$this->getFilterFields() |
308
|
|
|
) |
309
|
|
|
) |
310
|
|
|
); |
311
|
|
|
|
312
|
6 |
|
$this->extend('updateFieldsForIndexing', $fields); |
313
|
|
|
|
314
|
6 |
|
return $fields; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Upload config for this index to the given store |
319
|
|
|
* |
320
|
|
|
* @param ConfigStore $store |
321
|
|
|
*/ |
322
|
30 |
|
public function uploadConfig(ConfigStore $store): void |
323
|
|
|
{ |
324
|
|
|
// @todo use types/schema/elevate rendering |
325
|
|
|
// Upload the config files for this index |
326
|
|
|
// Create a default schema which we can manage later |
327
|
30 |
|
$schema = (string)$this->schemaService->generateSchema(); |
328
|
30 |
|
$store->uploadString( |
329
|
30 |
|
$this->getIndexName(), |
330
|
30 |
|
'schema.xml', |
331
|
30 |
|
$schema |
332
|
|
|
); |
333
|
|
|
|
334
|
|
|
|
335
|
30 |
|
$synonyms = $this->getSynonyms(); |
336
|
|
|
|
337
|
|
|
// Upload synonyms |
338
|
30 |
|
$store->uploadString( |
339
|
30 |
|
$this->getIndexName(), |
340
|
30 |
|
'synonyms.txt', |
341
|
30 |
|
$synonyms |
342
|
|
|
); |
343
|
|
|
|
344
|
|
|
// Upload additional files |
345
|
30 |
|
foreach (glob($this->schemaService->getExtrasPath() . '/*') as $file) { |
346
|
30 |
|
if (is_file($file)) { |
347
|
30 |
|
$store->uploadFile($this->getIndexName(), $file); |
348
|
|
|
} |
349
|
|
|
} |
350
|
30 |
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Add synonyms. Public to be extendable |
354
|
|
|
* @param bool $defaults Include UK to US synonyms |
355
|
|
|
* @return string |
356
|
|
|
*/ |
357
|
30 |
|
public function getSynonyms($defaults = true): string |
358
|
|
|
{ |
359
|
30 |
|
$synonyms = Synonyms::getSynonymsAsString($defaults); |
360
|
30 |
|
$siteConfigSynonyms = SiteConfig::current_site_config()->getField('SearchSynonyms'); |
361
|
|
|
|
362
|
30 |
|
return sprintf('%s%s', $synonyms, $siteConfigSynonyms); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* @return array |
367
|
|
|
*/ |
368
|
2 |
|
public function getQueryTerms(): array |
369
|
|
|
{ |
370
|
2 |
|
return $this->queryTerms; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* @return QueryComponentFactory |
375
|
|
|
*/ |
376
|
1 |
|
public function getQueryFactory(): QueryComponentFactory |
377
|
|
|
{ |
378
|
1 |
|
return $this->queryFactory; |
379
|
|
|
} |
380
|
|
|
} |
381
|
|
|
|