Passed
Push — hans/bufferadd ( dd4951...8221cf )
by Simon
08:32 queued 06:25
created

BaseIndex::doSearch()   A

Complexity

Conditions 3
Paths 9

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3.0123

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 18
c 3
b 0
f 0
nc 9
nop 1
dl 0
loc 31
ccs 16
cts 18
cp 0.8889
crap 3.0123
rs 9.6666
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 73
    public function init()
159
    {
160 73
        if (!self::config()->get($this->getIndexName())) {
161 73
            Deprecation::notice('5', 'Please set an index name and use a config yml');
162
163 73
            return;
164
        }
165
166 73
        if (!empty($this->getClasses()) && !$this->usedAllFields) {
167
            Deprecation::notice('5', 'It is adviced to use a config YML for most cases');
168
        }
169
170 73
        $this->initFromConfig();
171 73
    }
172
173
    /**
174
     * Generate the config from yml if possible
175
     */
176 73
    protected function initFromConfig(): void
177
    {
178 73
        $config = self::config()->get($this->getIndexName());
179
180 73
        if (!array_key_exists('Classes', $config)) {
181
            throw new LogicException('No classes to index found!');
182
        }
183
184 73
        $this->setClasses($config['Classes']);
185
186
        // For backward compatibility, copy the config to the protected values
187
        // Saves doubling up further down the line
188 73
        foreach (self::$fieldTypes as $type) {
189 73
            if (array_key_exists($type, $config)) {
190 73
                $method = 'set' . $type;
191 73
                $this->$method($config[$type]);
192
            }
193
        }
194 73
    }
195
196
    /**
197
     * Default returns a SearchResult. It can return an ArrayData if FTS Compat is enabled
198
     *
199
     * @param BaseQuery $query
200
     * @return SearchResult|ArrayData|mixed
201
     * @throws GuzzleException
202
     * @throws ValidationException
203
     * @throws ReflectionException
204
     */
205 5
    public function doSearch(BaseQuery $query)
206
    {
207 5
        SiteState::alterQuery($query);
208
        // Build the actual query parameters
209 5
        $clientQuery = $this->buildSolrQuery($query);
210
211 5
        $this->extend('onBeforeSearch', $query, $clientQuery);
212
213
        try {
214 5
            $result = $this->client->select($clientQuery);
215 5
            $this->rawQuery = $result;
216
217
            // Handle the after search first. This gets a raw search result
218 5
            $this->extend('onAfterSearch', $result);
219 5
            $searchResult = new SearchResult($result, $query, $this);
220
            // Return yourself but with a spellcheck
221 5
            if ($this->doRetry($query, $result, $searchResult)) {
222 1
                return $this->spellcheckRetry($query, $searchResult);
223
            }
224
225
            // And then handle the search results, which is a useable object for SilverStripe
226 5
            $this->extend('updateSearchResults', $searchResult);
227
228 5
            Controller::curr() // @todo make this useful. It's not doing it's job properly
229 5
            ->getRequest() // @todo Also, it's convoluted and pointless
230 5
            ->getSession()
231 5
                ->set(self::SEARCH_HISTORY_KEY, $this->getHistory());
232
233 5
            return $searchResult;
234
        } catch (Exception $e) {
235
            (new SolrLogger())->saveSolrLog();
236
        }
237
    }
238
239
    /**
240
     * From the given BaseQuery, generate a Solarium ClientQuery object
241
     *
242
     * @param BaseQuery $query
243
     * @return Query
244
     */
245 5
    public function buildSolrQuery(BaseQuery $query): Query
246
    {
247 5
        $clientQuery = $this->client->createSelect();
248 5
        $factory = $this->buildFactory($query, $clientQuery);
249
250 5
        $clientQuery = $factory->buildQuery();
251 5
        $this->queryTerms = $factory->getQueryArray();
252
253 5
        $queryData = implode(' ', $this->queryTerms);
254 5
        $clientQuery->setQuery($queryData);
255
256 5
        return $clientQuery;
257
    }
258
259
    /**
260
     * Build a factory to use in the SolrQuery building. {@link static::buildSolrQuery()}
261
     *
262
     * @param BaseQuery $query
263
     * @param Query $clientQuery
264
     * @return QueryComponentFactory|mixed
265
     * @todo Building a factory is like factoring a factory.
266
     */
267 5
    protected function buildFactory(BaseQuery $query, Query $clientQuery)
268
    {
269 5
        $factory = $this->queryFactory;
270
271 5
        $helper = $clientQuery->getHelper();
272
273 5
        $factory->setQuery($query);
274 5
        $factory->setClientQuery($clientQuery);
275 5
        $factory->setHelper($helper);
276 5
        $factory->setIndex($this);
277
278 5
        return $factory;
279
    }
280
281
    /**
282
     * Check if the query should be retried with spellchecking
283
     * Conditions are:
284
     * It is not already a retry with spellchecking
285
     * Spellchecking is enabled
286
     * If spellchecking is enabled and nothing is found OR it should follow spellchecking none the less
287
     * There is a spellcheck output
288
     *
289
     * @param BaseQuery $query
290
     * @param Result $result
291
     * @param SearchResult $searchResult
292
     * @return bool
293
     */
294 5
    protected function doRetry(BaseQuery $query, Result $result, SearchResult $searchResult): bool
295
    {
296 5
        return !$this->retry &&
297 5
            $query->hasSpellcheck() &&
298 5
            ($query->shouldFollowSpellcheck() || $result->getNumFound() === 0) &&
299 5
            $searchResult->getCollatedSpellcheck();
300
    }
301
302
    /**
303
     * Retry the query with the first collated spellcheck found.
304
     *
305
     * @param BaseQuery $query
306
     * @param SearchResult $searchResult
307
     * @return SearchResult|mixed|ArrayData
308
     * @throws GuzzleException
309
     * @throws ValidationException
310
     * @throws ReflectionException
311
     */
312 1
    protected function spellcheckRetry(BaseQuery $query, SearchResult $searchResult)
313
    {
314 1
        $terms = $query->getTerms();
315 1
        $spellChecked = $searchResult->getCollatedSpellcheck();
316
        // Remove the fuzzyness from the collated check
317 1
        $term = preg_replace('/~\d+/', '', $spellChecked);
318 1
        $terms[0]['text'] = $term;
319
        // Reset the terms and continue to retry
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 32
        $synonyms = $this->getSynonyms();
374
375
        // Upload synonyms
376 32
        $store->uploadString(
377 32
            $this->getIndexName(),
378 32
            'synonyms.txt',
379 32
            $synonyms
380
        );
381
382
        // Upload additional files
383 32
        foreach (glob($this->schemaService->getExtrasPath() . '/*') as $file) {
384 32
            if (is_file($file)) {
385 32
                $store->uploadFile($this->getIndexName(), $file);
386
            }
387
        }
388 32
    }
389
390
    /**
391
     * Add synonyms. Public to be extendable
392
     *
393
     * @param bool $defaults Include UK to US synonyms
394
     * @return string
395
     */
396 32
    public function getSynonyms($defaults = true): string
397
    {
398 32
        $synonyms = Synonyms::getSynonymsAsString($defaults);
399 32
        $siteConfigSynonyms = SiteConfig::current_site_config()->getField('SearchSynonyms');
400
401 32
        return sprintf('%s%s', $synonyms, $siteConfigSynonyms);
402
    }
403
404
    // @todo these getters should probably go in to a trait
405
406
    /**
407
     * Get the final, generated terms
408
     *
409
     * @return array
410
     */
411 2
    public function getQueryTerms(): array
412
    {
413 2
        return $this->queryTerms;
414
    }
415
416
    /**
417
     * Get the QueryComponentFactory. {@link QueryComponentFactory}
418
     *
419
     * @return QueryComponentFactory
420
     */
421 1
    public function getQueryFactory(): QueryComponentFactory
422
    {
423 1
        return $this->queryFactory;
424
    }
425
}
426