|
1
|
|
|
<?php |
|
2
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder; |
|
3
|
|
|
|
|
4
|
|
|
/*************************************************************** |
|
5
|
|
|
* Copyright notice |
|
6
|
|
|
* |
|
7
|
|
|
* (c) 2017 <[email protected]> |
|
8
|
|
|
* All rights reserved |
|
9
|
|
|
* |
|
10
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
|
11
|
|
|
* free software; you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU General Public License as published by |
|
13
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
14
|
|
|
* (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* The GNU General Public License can be found at |
|
17
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
|
18
|
|
|
* |
|
19
|
|
|
* This script is distributed in the hope that it will be useful, |
|
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
22
|
|
|
* GNU General Public License for more details. |
|
23
|
|
|
* |
|
24
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
|
25
|
|
|
***************************************************************/ |
|
26
|
|
|
|
|
27
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\AbstractQueryBuilder; |
|
28
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\QueryBuilder; |
|
29
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\SortingExpression; |
|
30
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The Faceting ParameterProvider is responsible to build the solr query parameters |
|
34
|
|
|
* that are needed for the highlighting. |
|
35
|
|
|
* |
|
36
|
|
|
* @package ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder |
|
37
|
|
|
*/ |
|
38
|
|
|
class Faceting extends AbstractDeactivatable implements ParameterBuilder |
|
39
|
|
|
{ |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $sorting = ''; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var int |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $minCount = 1; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $limit = 10; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var array |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $fields = []; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var array |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $additionalParameters = []; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Faceting constructor. |
|
68
|
|
|
* |
|
69
|
|
|
* @param bool $isEnabled |
|
70
|
|
|
* @param string $sorting |
|
71
|
|
|
* @param int $minCount |
|
72
|
|
|
* @param int $limit |
|
73
|
|
|
* @param array $fields |
|
74
|
|
|
* @param array $additionalParameters |
|
75
|
|
|
*/ |
|
76
|
137 |
|
public function __construct($isEnabled, $sorting = '', $minCount = 1, $limit = 10, $fields = [], $additionalParameters = []) |
|
77
|
|
|
{ |
|
78
|
137 |
|
$this->isEnabled = $isEnabled; |
|
79
|
137 |
|
$this->sorting = $sorting; |
|
80
|
137 |
|
$this->minCount = $minCount; |
|
81
|
137 |
|
$this->limit = $limit; |
|
82
|
137 |
|
$this->fields = $fields; |
|
83
|
137 |
|
$this->additionalParameters = $additionalParameters; |
|
84
|
137 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getSorting() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->sorting; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param string $sorting |
|
96
|
|
|
*/ |
|
97
|
|
|
public function setSorting($sorting) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->sorting = $sorting; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return int |
|
104
|
|
|
*/ |
|
105
|
49 |
|
public function getMinCount() |
|
106
|
|
|
{ |
|
107
|
49 |
|
return $this->minCount; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param int $minCount |
|
112
|
|
|
*/ |
|
113
|
|
|
public function setMinCount($minCount) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->minCount = $minCount; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return mixed |
|
120
|
|
|
*/ |
|
121
|
49 |
|
public function getLimit() |
|
122
|
|
|
{ |
|
123
|
49 |
|
return $this->limit; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param mixed $limit |
|
128
|
|
|
*/ |
|
129
|
|
|
public function setLimit($limit) |
|
130
|
|
|
{ |
|
131
|
|
|
$this->limit = $limit; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return array |
|
136
|
|
|
*/ |
|
137
|
49 |
|
public function getFields() |
|
138
|
|
|
{ |
|
139
|
49 |
|
return $this->fields; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param array $fields |
|
144
|
|
|
*/ |
|
145
|
34 |
|
public function setFields(array $fields) |
|
146
|
|
|
{ |
|
147
|
34 |
|
$this->fields = $fields; |
|
148
|
34 |
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param string $fieldName |
|
152
|
|
|
*/ |
|
153
|
2 |
|
public function addField($fieldName) |
|
154
|
|
|
{ |
|
155
|
2 |
|
$this->fields[] = $fieldName; |
|
156
|
2 |
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @return array |
|
160
|
|
|
*/ |
|
161
|
49 |
|
public function getAdditionalParameters(): array |
|
162
|
|
|
{ |
|
163
|
49 |
|
return $this->additionalParameters; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param array $additionalParameters |
|
168
|
|
|
*/ |
|
169
|
|
|
public function setAdditionalParameters(array $additionalParameters) |
|
170
|
|
|
{ |
|
171
|
|
|
$this->additionalParameters = $additionalParameters; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @param array $value |
|
176
|
|
|
*/ |
|
177
|
42 |
|
public function addAdditionalParameter($key, $value) |
|
178
|
|
|
{ |
|
179
|
42 |
|
$this->additionalParameters[$key] = $value; |
|
180
|
42 |
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Reads the facet sorting configuration and applies it to the queryParameters. |
|
184
|
|
|
* |
|
185
|
|
|
* @param array $facetParameters |
|
186
|
|
|
* @return array |
|
187
|
|
|
*/ |
|
188
|
49 |
|
protected function applySorting(array $facetParameters) |
|
189
|
|
|
{ |
|
190
|
49 |
|
$sortingExpression = new SortingExpression(); |
|
191
|
49 |
|
$globalSortingExpression = $sortingExpression->getForFacet($this->sorting); |
|
192
|
|
|
|
|
193
|
49 |
|
if (!empty($globalSortingExpression)) { |
|
194
|
35 |
|
$facetParameters['facet.sort'] = $globalSortingExpression; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
49 |
|
return $facetParameters; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @param TypoScriptConfiguration $solrConfiguration |
|
202
|
|
|
* @return Faceting |
|
203
|
|
|
*/ |
|
204
|
136 |
|
public static function fromTypoScriptConfiguration(TypoScriptConfiguration $solrConfiguration) |
|
205
|
|
|
{ |
|
206
|
136 |
|
$isEnabled = $solrConfiguration->getSearchFaceting(); |
|
207
|
136 |
|
if (!$isEnabled) { |
|
208
|
92 |
|
return new Faceting(false); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
44 |
|
$minCount = $solrConfiguration->getSearchFacetingMinimumCount(); |
|
212
|
44 |
|
$limit = $solrConfiguration->getSearchFacetingFacetLimit(); |
|
213
|
44 |
|
$sorting = $solrConfiguration->getSearchFacetingSortBy(); |
|
214
|
|
|
|
|
215
|
44 |
|
return new Faceting($isEnabled, $sorting, $minCount, $limit); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @return Faceting |
|
220
|
|
|
*/ |
|
221
|
|
|
public static function getEmpty() |
|
222
|
|
|
{ |
|
223
|
|
|
return new Faceting(false); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Retrieves all parameters that are required for faceting. |
|
228
|
|
|
* |
|
229
|
|
|
* @return array |
|
230
|
|
|
*/ |
|
231
|
49 |
|
protected function getFacetParameters() { |
|
232
|
49 |
|
$facetParameters = []; |
|
233
|
49 |
|
$facetParameters['facet'] = 'true'; |
|
234
|
49 |
|
$facetParameters['facet.mincount'] = $this->getMinCount(); |
|
235
|
49 |
|
$facetParameters['facet.limit'] = $this->getLimit(); |
|
236
|
49 |
|
$facetParameters['facet.field'] = $this->getFields(); |
|
237
|
|
|
|
|
238
|
49 |
|
foreach ($this->getAdditionalParameters() as $additionalParameterKey => $additionalParameterValue) { |
|
239
|
42 |
|
$facetParameters[$additionalParameterKey] = $additionalParameterValue; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
49 |
|
if ($facetParameters['json.facet']) { |
|
243
|
41 |
|
$facetParameters['json.facet'] = json_encode($facetParameters['json.facet']); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
49 |
|
$facetParameters = $this->applySorting($facetParameters); |
|
247
|
49 |
|
return $facetParameters; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* @param AbstractQueryBuilder $parentBuilder |
|
252
|
|
|
* @return QueryBuilder |
|
253
|
|
|
*/ |
|
254
|
137 |
|
public function build(AbstractQueryBuilder $parentBuilder): AbstractQueryBuilder |
|
255
|
|
|
{ |
|
256
|
137 |
|
$query = $parentBuilder->getQuery(); |
|
257
|
137 |
|
if (!$this->getIsEnabled()) { |
|
258
|
|
|
//@todo use unset functionality when present |
|
259
|
92 |
|
$query->addParam('facet', null); |
|
260
|
92 |
|
$query->addParam('lex', null); |
|
261
|
92 |
|
$query->addParam('json.mincount', null); |
|
262
|
92 |
|
$query->addParam('json.limit', null); |
|
263
|
92 |
|
$query->addParam('json.field', null); |
|
264
|
92 |
|
$query->addParam('facet.sort', null); |
|
265
|
|
|
|
|
266
|
92 |
|
$params = $query->getParams(); |
|
267
|
92 |
|
foreach($params as $key => $value) { |
|
268
|
92 |
|
if (strpos($key, 'f.') !== false) { |
|
269
|
92 |
|
$query->addParam($key, null); |
|
270
|
|
|
} |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
92 |
|
return $parentBuilder; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
//@todo check of $this->queryToBuilder->getFacetSet() can be used |
|
277
|
49 |
|
$facetingParameters = $this->getFacetParameters(); |
|
278
|
49 |
|
foreach($facetingParameters as $key => $value) { |
|
279
|
49 |
|
$query->addParam($key, $value); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
49 |
|
return $parentBuilder; |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|