1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\SearchBackend\Indexes; |
4
|
|
|
|
5
|
|
|
use Firesphere\SearchBackend\Traits\IndexingTraits\CoreIndexTrait; |
6
|
|
|
use LogicException; |
7
|
|
|
use SilverStripe\Core\Config\Configurable; |
8
|
|
|
use SilverStripe\Core\Extensible; |
9
|
|
|
use SilverStripe\Dev\Deprecation; |
10
|
|
|
|
11
|
|
|
abstract class CoreIndex |
12
|
|
|
{ |
13
|
|
|
use Extensible; |
14
|
|
|
use Configurable; |
15
|
|
|
use CoreIndexTrait; |
16
|
|
|
/** |
17
|
|
|
* Field types that can be added |
18
|
|
|
* Used in init to call build methods from configuration yml |
19
|
|
|
* |
20
|
|
|
* @array |
21
|
|
|
*/ |
22
|
|
|
protected static $fieldTypes = [ |
23
|
|
|
'FulltextFields', |
24
|
|
|
'SortFields', |
25
|
|
|
'FilterFields', |
26
|
|
|
'BoostedFields', |
27
|
|
|
'CopyFields', |
28
|
|
|
'DefaultField', |
29
|
|
|
'FacetFields', |
30
|
|
|
'StoredFields', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Required to initialise the fields. |
35
|
|
|
* It's loaded in to the non-static properties for backward compatibility with FTS |
36
|
|
|
* Also, it's a tad easier to use this way, loading the other way around would be very |
37
|
|
|
* memory intensive, as updating the config for each item is not efficient |
38
|
|
|
*/ |
39
|
|
|
public function init() |
40
|
|
|
{ |
41
|
|
|
$config = static::config()->get($this->getIndexName()); |
|
|
|
|
42
|
|
|
if (!$config) { |
43
|
|
|
Deprecation::notice('5', 'Please set an index name and use a config yml'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (!empty($this->getClasses())) { |
|
|
|
|
47
|
|
|
if (!$this->usedAllFields) { |
48
|
|
|
Deprecation::notice('5', 'It is advised to use a config YML for most cases'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->initFromConfig($config); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Generate the config from yml if possible |
59
|
|
|
* @param array|null $config |
60
|
|
|
*/ |
61
|
|
|
protected function initFromConfig($config): void |
62
|
|
|
{ |
63
|
|
|
if (!$config || !array_key_exists('Classes', $config)) { |
64
|
|
|
throw new LogicException('No classes or config to index found!'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->setClasses($config['Classes']); |
|
|
|
|
68
|
|
|
|
69
|
|
|
// For backward compatibility, copy the config to the protected values |
70
|
|
|
// Saves doubling up further down the line |
71
|
|
|
foreach (self::$fieldTypes as $type) { |
72
|
|
|
if (array_key_exists($type, $config)) { |
73
|
|
|
$method = 'set' . $type; |
74
|
|
|
if (method_exists($this, $method)) { |
75
|
|
|
$this->$method($config[$type]); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get all fields that are required for indexing in a unique way |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
public function getFieldsForIndexing(): array |
87
|
|
|
{ |
88
|
|
|
$facets = []; |
89
|
|
|
foreach ($this->getFacetFields() as $field) { |
90
|
|
|
$facets[] = $field['Field']; |
91
|
|
|
} |
92
|
|
|
// Return values to make the key reset |
93
|
|
|
// Only return unique values |
94
|
|
|
// And make it all a single array |
95
|
|
|
$fields = array_values( |
96
|
|
|
array_unique( |
97
|
|
|
array_merge( |
98
|
|
|
$this->getFulltextFields(), |
99
|
|
|
$this->getSortFields(), |
100
|
|
|
$facets, |
101
|
|
|
$this->getFilterFields() |
102
|
|
|
) |
103
|
|
|
) |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$this->extend('updateFieldsForIndexing', $fields); |
107
|
|
|
|
108
|
|
|
return $fields; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|