Passed
Push — hans/bufferadd ( 3149c2...a9e093 )
by Simon
06:08 queued 01:33
created

BaseIndex::doSearch()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3.0354

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 19
c 3
b 0
f 0
nc 4
nop 1
dl 0
loc 33
ccs 16
cts 19
cp 0.8421
crap 3.0354
rs 9.6333
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\States\SiteState;
16
use Firesphere\SolrSearch\Traits\BaseIndexTrait;
17
use Firesphere\SolrSearch\Traits\GetterSetterTrait;
18
use GuzzleHttp\Exception\GuzzleException;
19
use LogicException;
20
use ReflectionException;
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\Deprecation;
29
use SilverStripe\ORM\ValidationException;
30
use SilverStripe\SiteConfig\SiteConfig;
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 73
    public function __construct()
108
    {
109
        // Set up the client
110 73
        $config = Config::inst()->get(SolrCoreService::class, 'config');
111 73
        $config['endpoint'] = $this->getConfig($config['endpoint']);
112 73
        $this->client = new Client($config);
113 73
        $this->client->setAdapter(new Guzzle());
114 73
        $session = Controller::curr()->getRequest()->getSession();
115 73
        $this->history = $session->get(self::SEARCH_HISTORY_KEY) ?: [];
116
117
        // Set up the schema service, only used in the generation of the schema
118 73
        $schemaService = Injector::inst()->get(SchemaService::class, false);
119 73
        $schemaService->setIndex($this);
120 73
        $schemaService->setStore(Director::isDev());
121 73
        $this->schemaService = $schemaService;
122 73
        $this->queryFactory = Injector::inst()->get(QueryComponentFactory::class, false);
123
124 73
        $this->extend('onBeforeInit');
125 73
        $this->init();
126 73
        $this->extend('onAfterInit');
127 73
    }
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 73
    public function getConfig($endpoints): array
137
    {
138 73
        foreach ($endpoints as $host => $endpoint) {
139 73
            $endpoints[$host]['core'] = $this->getIndexName();
140
        }
141
142 73
        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 72
    public function init()
159
    {
160 72
        if (!self::config()->get($this->getIndexName())) {
161 72
            Deprecation::notice('5', 'Please set an index name and use a config yml');
162
163 72
            return;
164
        }
165
166 72
        if (!empty($this->getClasses()) && !$this->usedAllFields) {
167
            Deprecation::notice('5', 'It is adviced to use a config YML for most cases');
168
        }
169
170
171 72
        $this->initFromConfig();
172 72
    }
173
174
    /**
175
     * Generate the config from yml if possible
176
     */
177 72
    protected function initFromConfig(): void
178
    {
179 72
        $config = self::config()->get($this->getIndexName());
180
181 72
        if (!array_key_exists('Classes', $config)) {
182
            throw new LogicException('No classes to index found!');
183
        }
184
185 72
        $this->setClasses($config['Classes']);
186
187
        // For backward compatibility, copy the config to the protected values
188
        // Saves doubling up further down the line
189 72
        foreach (self::$fieldTypes as $type) {
190 72
            if (array_key_exists($type, $config)) {
191 72
                $method = 'set' . $type;
192 72
                $this->$method($config[$type]);
193
            }
194
        }
195 72
    }
196
197
    /**
198
     * Default returns a SearchResult. It can return an ArrayData if FTS Compat is enabled
199
     *
200
     * @param BaseQuery $query
201
     * @return SearchResult|ArrayData|mixed
202
     * @throws GuzzleException
203
     * @throws ValidationException
204
     * @throws ReflectionException
205
     */
206 5
    public function doSearch(BaseQuery $query)
207
    {
208 5
        SiteState::alterQuery($query);
209
        // Build the actual query parameters
210 5
        $clientQuery = $this->buildSolrQuery($query);
211
212 5
        $this->extend('onBeforeSearch', $query, $clientQuery);
213
214
        try {
215 5
            $result = $this->client->select($clientQuery);
216
        } catch (Exception $e) {
217
            $logger = new SolrLogger();
218
            $logger->saveSolrLog('Query');
219
        }
220
221 5
        $this->rawQuery = $result;
222
223
        // Handle the after search first. This gets a raw search result
224 5
        $this->extend('onAfterSearch', $result);
225 5
        $searchResult = new SearchResult($result, $query, $this);
226 5
        if ($this->doRetry($query, $result, $searchResult)) {
227 1
            return $this->spellcheckRetry($query, $searchResult);
228
        }
229
230
        // And then handle the search results, which is a useable object for SilverStripe
231 5
        $this->extend('updateSearchResults', $searchResult);
232
233 5
        Controller::curr()
234 5
            ->getRequest()
235 5
            ->getSession()
236 5
            ->set(self::SEARCH_HISTORY_KEY, $this->getHistory());
237
238 5
        return $searchResult;
239
    }
240
241
    /**
242
     * From the given BaseQuery, generate a Solarium ClientQuery object
243
     *
244
     * @param BaseQuery $query
245
     * @return Query
246
     */
247 5
    public function buildSolrQuery(BaseQuery $query): Query
248
    {
249 5
        $clientQuery = $this->client->createSelect();
250 5
        $factory = $this->buildFactory($query, $clientQuery);
251
252 5
        $clientQuery = $factory->buildQuery();
253 5
        $this->queryTerms = $factory->getQueryArray();
254
255 5
        $queryData = implode(' ', $this->queryTerms);
256 5
        $clientQuery->setQuery($queryData);
257
258 5
        return $clientQuery;
259
    }
260
261
    /**
262
     * Build a factory to use in the SolrQuery building. {@link static::buildSolrQuery()}
263
     *
264
     * @param BaseQuery $query
265
     * @param Query $clientQuery
266
     * @return QueryComponentFactory|mixed
267
     */
268 5
    protected function buildFactory(BaseQuery $query, Query $clientQuery)
269
    {
270 5
        $factory = $this->queryFactory;
271
272 5
        $helper = $clientQuery->getHelper();
273
274 5
        $factory->setQuery($query);
275 5
        $factory->setClientQuery($clientQuery);
276 5
        $factory->setHelper($helper);
277 5
        $factory->setIndex($this);
278
279 5
        return $factory;
280
    }
281
282
    /**
283
     * Check if the query should be retried with spellchecking
284
     * Conditions are:
285
     * It is not already a retry with spellchecking
286
     * Spellchecking is enabled
287
     * If spellchecking is enabled and nothing is found OR it should follow spellchecking none the less
288
     * There is a spellcheck output
289
     *
290
     * @param BaseQuery $query
291
     * @param Result $result
292
     * @param SearchResult $searchResult
293
     * @return bool
294
     */
295 5
    protected function doRetry(BaseQuery $query, Result $result, SearchResult $searchResult): bool
296
    {
297 5
        return !$this->retry &&
298 5
            $query->hasSpellcheck() &&
299 5
            ($query->shouldFollowSpellcheck() || $result->getNumFound() === 0) &&
300 5
            $searchResult->getCollatedSpellcheck();
301
    }
302
303
    /**
304
     * Retry the query with the first collated spellcheck found.
305
     *
306
     * @param BaseQuery $query
307
     * @param SearchResult $searchResult
308
     * @return SearchResult|mixed|ArrayData
309
     * @throws GuzzleException
310
     * @throws ValidationException
311
     * @throws ReflectionException
312
     */
313 1
    protected function spellcheckRetry(BaseQuery $query, SearchResult $searchResult)
314
    {
315 1
        $terms = $query->getTerms();
316 1
        $spellChecked = $searchResult->getCollatedSpellcheck();
317
        // Remove the fuzzyness from the collated check
318 1
        $term = preg_replace('/~\d+/', '', $spellChecked);
319 1
        $terms[0]['text'] = $term;
320 1
        $query->setTerms($terms);
321 1
        $this->retry = true;
322
323 1
        return $this->doSearch($query);
324
    }
325
326
    /**
327
     * Get all fields that are required for indexing in a unique way
328
     *
329
     * @return array
330
     */
331 8
    public function getFieldsForIndexing(): array
332
    {
333 8
        $facets = [];
334 8
        foreach ($this->getFacetFields() as $field) {
335 7
            $facets[] = $field['Field'];
336
        }
337
        // Return values to make the key reset
338
        // Only return unique values
339
        // And make it all a single array
340 8
        $fields = array_values(
341 8
            array_unique(
342 8
                array_merge(
343 8
                    $this->getFulltextFields(),
344 8
                    $this->getSortFields(),
345 8
                    $facets,
346 8
                    $this->getFilterFields()
347
                )
348
            )
349
        );
350
351 8
        $this->extend('updateFieldsForIndexing', $fields);
352
353 8
        return $fields;
354
    }
355
356
    /**
357
     * Upload config for this index to the given store
358
     *
359
     * @param ConfigStore $store
360
     */
361 32
    public function uploadConfig(ConfigStore $store): void
362
    {
363
        // @todo use types/schema/elevate rendering
364
        // Upload the config files for this index
365
        // Create a default schema which we can manage later
366 32
        $schema = (string)$this->schemaService->generateSchema();
367 32
        $store->uploadString(
368 32
            $this->getIndexName(),
369 32
            'schema.xml',
370 32
            $schema
371
        );
372
373
374 32
        $synonyms = $this->getSynonyms();
375
376
        // Upload synonyms
377 32
        $store->uploadString(
378 32
            $this->getIndexName(),
379 32
            'synonyms.txt',
380 32
            $synonyms
381
        );
382
383
        // Upload additional files
384 32
        foreach (glob($this->schemaService->getExtrasPath() . '/*') as $file) {
385 32
            if (is_file($file)) {
386 32
                $store->uploadFile($this->getIndexName(), $file);
387
            }
388
        }
389 32
    }
390
391
    /**
392
     * Add synonyms. Public to be extendable
393
     *
394
     * @param bool $defaults Include UK to US synonyms
395
     * @return string
396
     */
397 32
    public function getSynonyms($defaults = true): string
398
    {
399 32
        $synonyms = Synonyms::getSynonymsAsString($defaults);
400 32
        $siteConfigSynonyms = SiteConfig::current_site_config()->getField('SearchSynonyms');
401
402 32
        return sprintf('%s%s', $synonyms, $siteConfigSynonyms);
403
    }
404
405
    /**
406
     * Get the final, generated terms
407
     *
408
     * @return array
409
     */
410 2
    public function getQueryTerms(): array
411
    {
412 2
        return $this->queryTerms;
413
    }
414
415
    /**
416
     * Get the QueryComponentFactory. {@link QueryComponentFactory}
417
     *
418
     * @return QueryComponentFactory
419
     */
420 1
    public function getQueryFactory(): QueryComponentFactory
421
    {
422 1
        return $this->queryFactory;
423
    }
424
}
425