Passed
Push — hans/added-tests ( ef5224...c0541a )
by Simon
07:18 queued 05:26
created

BaseIndex::initFromConfig()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

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