1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
// @phpcs:disable SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed |
4
|
|
|
|
5
|
|
|
namespace Suilven\FreeTextSearch; |
6
|
|
|
|
7
|
|
|
use SilverStripe\Core\Config\Config; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Indexes |
11
|
|
|
* |
12
|
|
|
* @package Suilven\FreeTextSearch |
13
|
|
|
*/ |
14
|
|
|
class Indexes |
15
|
|
|
{ |
16
|
|
|
/** @var array<string, \Suilven\FreeTextSearch\Index>|null */ |
17
|
|
|
private $indexesByName; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Get an Index object by name from the config |
22
|
|
|
*/ |
23
|
|
|
public function getIndex(string $name): Index |
24
|
|
|
{ |
25
|
|
|
if (\is_null($this->indexesByName)) { |
26
|
|
|
$this->getIndexes(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return $this->indexesByName[$name]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get indexes from config |
35
|
|
|
* |
36
|
|
|
* @return array<string, \Suilven\FreeTextSearch\Index> ClassName -> Index |
37
|
|
|
*/ |
38
|
|
|
public function getIndexes(): array |
39
|
|
|
{ |
40
|
|
|
$indexesConfig = Config::inst()->get('Suilven\FreeTextSearch\Indexes', 'indexes') ; |
41
|
|
|
|
42
|
|
|
// reset |
43
|
|
|
$this->indexesByName = []; |
44
|
|
|
|
45
|
|
|
foreach ($indexesConfig as $indexConfig) { |
46
|
|
|
$name = $indexConfig['index']['name']; |
47
|
|
|
$indexAlreadyExists = isset($this->indexesByName[$name]); |
48
|
|
|
|
49
|
|
|
// get the existing index, to tweak, or create a new one |
50
|
|
|
$index = $indexAlreadyExists |
51
|
|
|
? $this->indexesByName[$name] |
52
|
|
|
: new Index(); |
53
|
|
|
$index->setName($name); |
54
|
|
|
|
55
|
|
|
if (isset($indexConfig['index']['class'])) { |
56
|
|
|
$index->setClass($indexConfig['index']['class']); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (isset($indexConfig['index']['fields'])) { |
60
|
|
|
foreach ($indexConfig['index']['fields'] as $fieldname) { |
61
|
|
|
$index->addField($fieldname); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (isset($indexConfig['index']['tokens'])) { |
66
|
|
|
foreach ($indexConfig['index']['tokens'] as $token) { |
67
|
|
|
$index->addToken($token); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// has one fields |
72
|
|
|
if (isset($indexConfig['index']['has_one'])) { |
73
|
|
|
foreach ($indexConfig['index']['has_one'] as $hasOneField) { |
74
|
|
|
$index->addHasOneField($hasOneField); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// has many fields |
79
|
|
|
// NB many many may need to be treated as bipartisan has many |
80
|
|
|
if (isset($indexConfig['index']['has_many'])) { |
81
|
|
|
foreach ($indexConfig['index']['has_many'] as $hasManyField) { |
82
|
|
|
$index->addHasManyField($hasManyField['name'], [ |
83
|
|
|
'relationship' => $hasManyField['relationship'], |
84
|
|
|
'field' => $hasManyField['field'], |
85
|
|
|
]); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// fields that will be used for highlighting |
90
|
|
|
if (isset($indexConfig['index']['highlighted_fields'])) { |
91
|
|
|
foreach ($indexConfig['index']['highlighted_fields'] as $highlightedField) { |
92
|
|
|
$index->addHighlightedField($highlightedField); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// fields that will be used for storage, but not indexed |
97
|
|
|
if (isset($indexConfig['index']['stored_fields'])) { |
98
|
|
|
foreach ($indexConfig['index']['stored_fields'] as $storedField) { |
99
|
|
|
$index->addStoredField($storedField); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// tokenizer |
104
|
|
|
if (isset($indexConfig['index']['tokenizer'])) { |
105
|
|
|
$index->setTokenizer($indexConfig['index']['tokenizer']); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->indexesByName[$index->getName()] = $index; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this->indexesByName; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
/** @return array<string> An array of facet fields, such as ['ISO', 'Aperture', 'ShutterSpeed'] */ |
116
|
|
|
public function getFacetFields(string $indexName): array |
117
|
|
|
{ |
118
|
|
|
$indexesConfig = Config::inst()->get('Suilven\FreeTextSearch\Indexes', 'indexes'); |
119
|
|
|
$result = []; |
120
|
|
|
foreach ($indexesConfig as $indexConfig) { |
121
|
|
|
$name = ($indexConfig['index']['name']); |
122
|
|
|
|
123
|
|
|
if ($name !== $indexName) { |
124
|
|
|
continue; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if (isset($indexConfig['index']['tokens'])) { |
128
|
|
|
foreach ($indexConfig['index']['tokens'] as $token) { |
129
|
|
|
$result[] = $token; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $result; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** @return array<string> */ |
139
|
|
|
public function getHasOneFields(string $indexName): array |
140
|
|
|
{ |
141
|
|
|
$indexesConfig = Config::inst()->get('Suilven\FreeTextSearch\Indexes', 'indexes'); |
142
|
|
|
|
143
|
|
|
$result = []; |
144
|
|
|
foreach ($indexesConfig as $indexConfig) { |
145
|
|
|
$name = ($indexConfig['index']['name']); |
146
|
|
|
|
147
|
|
|
if ($name !== $indexName) { |
148
|
|
|
continue; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if (isset($indexConfig['index']['has_one'])) { |
152
|
|
|
foreach ($indexConfig['index']['has_one'] as $hasOne) { |
153
|
|
|
$result[] = $hasOne; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $result; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
/** @return array<string> */ |
163
|
|
|
public function getHasManyFields(string $indexName): array |
164
|
|
|
{ |
165
|
|
|
$indexesConfig = Config::inst()->get('Suilven\FreeTextSearch\Indexes', 'indexes'); |
166
|
|
|
|
167
|
|
|
$result = []; |
168
|
|
|
foreach ($indexesConfig as $indexConfig) { |
169
|
|
|
$name = ($indexConfig['index']['name']); |
170
|
|
|
|
171
|
|
|
if ($name !== $indexName) { |
172
|
|
|
continue; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if (isset($indexConfig['index']['has_many'])) { |
176
|
|
|
foreach ($indexConfig['index']['has_many'] as $hasManyField) { |
177
|
|
|
$result[] = $hasManyField; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $result; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|