/
FacetSet.php
261 lines (237 loc) · 9.32 KB
/
FacetSet.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
*
* @link http://www.solarium-project.org/
*/
/**
* @namespace
*/
namespace Solarium\Component\RequestBuilder;
use Solarium\Core\Client\Request;
use Solarium\QueryType\Select\RequestBuilder;
use Solarium\Component\FacetSet as FacetsetComponent;
use Solarium\Component\Facet\Field as FacetField;
use Solarium\Component\Facet\MultiQuery as FacetMultiQuery;
use Solarium\Component\Facet\Query as FacetQuery;
use Solarium\Component\Facet\Range as FacetRange;
use Solarium\Component\Facet\Pivot as FacetPivot;
use Solarium\Component\Facet\Interval as FacetInterval;
use Solarium\Exception\UnexpectedValueException;
/**
* Add select component FacetSet to the request.
*/
class FacetSet extends RequestBuilder implements ComponentRequestBuilderInterface
{
/**
* Add request settings for FacetSet.
*
* @throws UnexpectedValueException
*
* @param FacetsetComponent $component
* @param Request $request
*
* @return Request
*/
public function buildComponent($component, $request)
{
$facets = $component->getFacets();
if (count($facets) !== 0) {
// enable faceting
$request->addParam('facet', 'true');
// global facet params
$request->addParam('facet.sort', $component->getSort());
$request->addParam('facet.prefix', $component->getPrefix());
$request->addParam('facet.contains', $component->getContains());
$request->addParam('facet.contains.ignoreCase', is_null($ignoreCase = $component->getContainsIgnoreCase()) ? null : ($ignoreCase ? 'true' : 'false'));
$request->addParam('facet.missing', $component->getMissing());
$request->addParam('facet.mincount', $component->getMinCount());
$request->addParam('facet.limit', $component->getLimit());
foreach ($facets as $facet) {
switch ($facet->getType()) {
case FacetsetComponent::FACET_FIELD:
$this->addFacetField($request, $facet);
break;
case FacetsetComponent::FACET_QUERY:
$this->addFacetQuery($request, $facet);
break;
case FacetsetComponent::FACET_MULTIQUERY:
$this->addFacetMultiQuery($request, $facet);
break;
case FacetsetComponent::FACET_RANGE:
$this->addFacetRange($request, $facet);
break;
case FacetsetComponent::FACET_PIVOT:
$this->addFacetPivot($request, $facet);
break;
case FacetsetComponent::FACET_INTERVAL:
$this->addFacetInterval($request, $facet);
break;
default:
throw new UnexpectedValueException('Unknown facet type');
}
}
}
return $request;
}
/**
* Add params for a field facet to request.
*
* @param Request $request
* @param FacetField $facet
*/
public function addFacetField($request, $facet)
{
$field = $facet->getField();
$request->addParam(
'facet.field',
$this->renderLocalParams(
$field,
array('key' => $facet->getKey(), 'ex' => $facet->getExcludes())
)
);
$request->addParam("f.$field.facet.limit", $facet->getLimit());
$request->addParam("f.$field.facet.sort", $facet->getSort());
$request->addParam("f.$field.facet.prefix", $facet->getPrefix());
$request->addParam("f.$field.facet.contains", $facet->getContains());
$request->addParam("f.$field.facet.contains.ignoreCase", is_null($ignoreCase = $facet->getContainsIgnoreCase()) ? null : ($ignoreCase ? 'true' : 'false'));
$request->addParam("f.$field.facet.offset", $facet->getOffset());
$request->addParam("f.$field.facet.mincount", $facet->getMinCount());
$request->addParam("f.$field.facet.missing", $facet->getMissing());
$request->addParam("f.$field.facet.method", $facet->getMethod());
}
/**
* Add params for a facet query to request.
*
* @param Request $request
* @param FacetQuery $facet
*/
public function addFacetQuery($request, $facet)
{
$request->addParam(
'facet.query',
$this->renderLocalParams(
$facet->getQuery(),
array('key' => $facet->getKey(), 'ex' => $facet->getExcludes())
)
);
}
/**
* Add params for a multiquery facet to request.
*
* @param Request $request
* @param FacetMultiQuery $facet
*/
public function addFacetMultiQuery($request, $facet)
{
foreach ($facet->getQueries() as $facetQuery) {
$this->addFacetQuery($request, $facetQuery);
}
}
/**
* Add params for a range facet to request.
*
* @param Request $request
* @param FacetRange $facet
*/
public function addFacetRange($request, $facet)
{
$field = $facet->getField();
$request->addParam(
'facet.range',
$this->renderLocalParams(
$field,
array('key' => $facet->getKey(), 'ex' => $facet->getExcludes())
)
);
$request->addParam("f.$field.facet.range.start", $facet->getStart());
$request->addParam("f.$field.facet.range.end", $facet->getEnd());
$request->addParam("f.$field.facet.range.gap", $facet->getGap());
$request->addParam("f.$field.facet.range.hardend", $facet->getHardend());
$request->addParam("f.$field.facet.mincount", $facet->getMinCount());
foreach ($facet->getOther() as $otherValue) {
$request->addParam("f.$field.facet.range.other", $otherValue);
}
foreach ($facet->getInclude() as $includeValue) {
$request->addParam("f.$field.facet.range.include", $includeValue);
}
}
/**
* Add params for a range facet to request.
*
* @param Request $request
* @param FacetPivot $facet
*/
public function addFacetPivot($request, $facet)
{
$stats = $facet->getStats();
if (count($stats) > 0) {
$key = array('stats' => implode('', $stats));
// when specifying stats, solr sets the field as key
$facet->setKey(implode(',', $facet->getFields()));
} else {
$key = array('key' => $facet->getKey());
}
$request->addParam(
'facet.pivot',
$this->renderLocalParams(
implode(',', $facet->getFields()),
array_merge($key, array('ex' => $facet->getExcludes()))
)
);
$request->addParam('facet.pivot.mincount', $facet->getMinCount(), true);
}
/**
* Add params for a interval facet to request
*
* @param Request $request
* @param FacetInterval $facet
* @return void
*/
public function addFacetInterval($request, $facet)
{
$field = $facet->getField();
$request->addParam(
'facet.interval',
$this->renderLocalParams(
$field,
array('key' => $facet->getKey(), 'ex' => $facet->getExcludes())
)
);
foreach ($facet->getSet() as $key => $setValue) {
if(is_string($key)) {
$setValue = '{!key="'.$key.'"}'.$setValue;
}
$request->addParam("f.$field.facet.interval.set", $setValue);
}
}
}