/
SolrSearch.php
527 lines (448 loc) · 14 KB
/
SolrSearch.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
<?php
/**
* A page type specifically used for displaying search results.
*
* This is an alternative encapsulation of search logic as it comprises much more than the out of the
* box example. To use this instead of the default implementation, your search form call in Page should first
* retrieve the ExtensibleSearchPage to use as its context.
*
* @author Nathan Glasl <nathan@silverstripe.com.au>
* @author Marcus Nyeholt <marcus@silverstripe.com.au>
* @license http://silverstripe.org/bsd-license/
*/
if(class_exists('ExtensibleSearchPage')) {
class SolrSearch extends DataExtension {
const RESULTS_ACTION = 'results';
// Define the additional DB fields that are supported by solr search customisation.
public static $support = array(
'QueryType' => 1,
'SearchType' => 1,
'SearchOnFields' => 1,
'BoostFields' => 1,
'BoostMatchFields' => 1,
'FacetFields' => 1,
'CustomFacetFields' => 1,
'FacetMapping' => 1,
'FacetQueries' => 1,
'MinFacetCount' => 1,
'FilterFields' => 1
);
public $default_search = '*:*';
/**
*
* The facets we're interested in for this search page. This will be made a little more
* flexible in later releases.
*
* for example with the alchemiser module -
*
* array (
* 'AlcKeywords_ms',
* 'AlcPerson_ms',
* 'AlcCompany_ms',
* 'AlcOrganization_ms',
* );
*
* @var array
*/
public static $facets = array();
/**
* A local cache of the current query the user is executing based
* on data in the request
*
* @var SolrResultSet
*/
protected $query;
/**
* @var SolrSearchService
*/
protected $solr;
/**
* Used for the url param
*
* @var String
*/
public static $filter_param = 'filter';
public static $dependencies = array(
'solrSearchService' => '%$SolrSearchService',
);
/**
* @var SolrSearchService
*/
public $solrSearchService;
public function updateCMSFields(FieldList $fields) {
// Make sure previously existing hooks are carried across.
$this->owner->extend('updateSolrCMSFields', $fields);
}
/**
* Return the fields that can be selected for sorting operations.
*
* @param String $listType
* @return array
*/
public function getSelectableFields($listType = null, $excludeGeo = true) {
if (!$listType) {
$listType = $this->owner->searchableTypes('Page');
}
$availableFields = $this->solrSearchService->getAllSearchableFieldsFor($listType);
$objFields = array_combine(array_keys($availableFields), array_keys($availableFields));
$objFields['LastEdited'] = 'LastEdited';
$objFields['Created'] = 'Created';
$objFields['ID'] = 'ID';
$objFields['score'] = 'Score';
if ($excludeGeo) {
// need to filter out any fields that are of geopoint type, as we can't use those for search
if (!is_array($listType)) {
$listType = array($listType);
}
foreach ($listType as $classType) {
$db = Config::inst()->get($classType, 'db');
if ($db && count($db)) {
foreach ($db as $name => $type) {
$type = current(explode("(", $type));
if (is_subclass_of($type, 'SolrGeoPoint') || $type == 'SolrGeoPoint') {
unset($objFields[$name]);
}
}
}
}
}
ksort($objFields);
return $objFields;
}
/**
* Get the solr instance.
*
* Note that we do this as a method just in case we decide in future
* that different pages can utilise different solr instances..
*/
public function getSolr() {
if (!$this->solr) {
$this->solr = $this->solrSearchService;
}
return $this->solr;
}
/**
* Figures out the list of fields to use in faceting, based on configured / defaults
*/
public function fieldsForFacets() {
$fields = self::$facets;
$facetFields = array('FacetFields', 'CustomFacetFields');
if (!$fields) {
$fields = array();
}
foreach ($facetFields as $name) {
if ($this->owner->$name && $ff = $this->owner->$name->getValues()) {
$types = $this->owner->searchableTypes('Page');
foreach ($ff as $f) {
$fieldName = $this->getSolr()->getSolrFieldName($f, $types);
if (!$fieldName) {
$fieldName = $f;
}
$fields[] = $fieldName;
}
}
}
return $fields;
}
/**
* Get the currently active query for this page, if any
*
* @return SolrResultSet
*/
public function getQuery() {
if ($this->query) {
return $this->query;
}
if (!$this->getSolr()->isConnected()) {
return null;
}
$query = null;
$builder = $this->getSolr()->getQueryBuilder($this->owner->QueryType);
if (isset($_GET['Search'])) {
$query = $_GET['Search'];
// lets convert it to a base solr query
$builder->baseQuery($query);
}
$sortBy = isset($_GET['SortBy']) ? $_GET['SortBy'] : $this->owner->SortBy;
$sortDir = isset($_GET['SortDir']) ? $_GET['SortDir'] : $this->owner->SortDir;
$types = $this->owner->searchableTypes();
// allow user to specify specific type
if (isset($_GET['SearchType'])) {
$fixedType = $_GET['SearchType'];
if (in_array($fixedType, $types)) {
$types = array($fixedType);
}
}
// (strlen($this->SearchType) ? $this->SearchType : null);
$fields = $this->getSelectableFields();
// if we've explicitly set a sort by, then we want to make sure we have a type
// so we can resolve what the field name in solr is. Otherwise we don't care about type
// overly much
if (!count($types) && $sortBy) {
// default to page
$types = array('Page');
}
if (!isset($fields[$sortBy])) {
$sortBy = 'score';
}
$sortDir = $sortDir == 'Ascending' ? 'asc' : 'desc';
$activeFacets = $this->getActiveFacets();
if (count($activeFacets)) {
foreach ($activeFacets as $facetName => $facetValues) {
foreach ($facetValues as $value) {
$builder->addFilter($facetName, $value);
}
}
}
$offset = isset($_GET['start']) ? $_GET['start'] : 0;
$limit = isset($_GET['limit']) ? $_GET['limit'] : ($this->owner->ResultsPerPage ? $this->owner->ResultsPerPage : 10);
if (count($types)) {
$sortBy = $this->solrSearchService->getSortFieldName($sortBy, $types);
$filterQ = array();
foreach ($types as $t) {
$filterQ[] = 'ClassNameHierarchy_ms:' . $t;
}
$builder->addFilter(implode(' OR ', $filterQ));
}
if ($this->owner->SearchTrees()->count()) {
$parents = $this->owner->SearchTrees()->column('ID');
$builder->addFilter('ParentsHierarchy_ms', implode(' OR ', $parents));
}
if (!$sortBy) {
$sortBy = 'score';
}
$builder->sortBy($sortBy, $sortDir);
$selectedFields = $this->owner->SearchOnFields->getValues();
// the following serves two purposes; filter out the searched on fields to only those that
// are in the actually searched on types, and to map them to relevant solr types
if (count($selectedFields)) {
$mappedFields = array();
foreach ($selectedFields as $field) {
$mappedField = $this->getSolr()->getSolrFieldName($field, $types);
// some fields that we're searching on don't exist in the types that the user has selected
// to search within
if ($mappedField) {
$mappedFields[] = $mappedField;
}
}
$builder->queryFields($mappedFields);
}
if ($boost = $this->owner->BoostFields->getValues()) {
$boostSetting = array();
foreach ($boost as $field => $amount) {
if ($amount > 0) {
$boostSetting[$this->getSolr()->getSolrFieldName($field, $types)] = $amount;
}
}
$builder->boost($boostSetting);
}
if ($boost = $this->owner->BoostMatchFields->getValues()) {
if (count($boost)) {
$builder->boostFieldValues($boost);
}
}
if ($filters = $this->owner->FilterFields->getValues()) {
if (count($filters)) {
foreach ($filters as $filter => $val) {
$builder->addFilter($filter, $val);
}
}
}
$params = array(
'facet' => 'true',
'facet.field' => $this->fieldsForFacets(),
'facet.limit' => 10,
'facet.mincount' => $this->owner->MinFacetCount ? $this->owner->MinFacetCount : 1,
'fl' => '*,score'
);
$fq = $this->owner->queryFacets();
if (count($fq)) {
$params['facet.query'] = array_keys($fq);
}
$this->owner->extend('updateQueryBuilder', $builder);
$this->query = $this->getSolr()->query($builder, $offset, $limit, $params);
return $this->query;
}
/**
* Gets a list of facet based filters
*/
public function getActiveFacets() {
return isset($_GET[self::$filter_param]) ? $_GET[self::$filter_param] : array();
}
/**
* Retrieve all facets in the result set in a way that can be iterated
* over conveniently.
*
* @return \ArrayList
*/
public function AllFacets() {
if (!$this->getQuery()) {
return new ArrayList(array());
}
$facets = $this->getQuery()->getFacets();
$result = array();
$mapping = $this->facetFieldMapping();
foreach ($facets as $title => $items) {
$object = new ViewableData();
$object->Items = $this->currentFacets($title);
$title = isset($mapping[$title]) ? $mapping[$title] : $title;
$object->Title = Varchar::create_field('Varchar', $title);
$result[] = $object;
}
return new ArrayList($result);
}
/**
* Retrieve the mapping of facet field name (eg FieldName_mt)
* mapped to the user entered nice name
*
* @return type
*/
protected function facetFieldMapping() {
$fields = array();
if ($this->owner->FacetMapping && $ff = $this->owner->FacetMapping->getValues()) {
$types = $this->owner->searchableTypes('Page');
foreach ($ff as $f => $mapped) {
$fieldName = $this->getSolr()->getSolrFieldName($f, $types);
if (!$fieldName) {
$fieldName = $f;
}
$fields[$fieldName] = $mapped;
}
}
return $fields;
}
/**
* Get the list of facet values for the given term
*
* @param String $term
*/
public function currentFacets($term=null) {
if (!$this->getQuery()) {
return new ArrayList(array());
}
$facets = $this->getQuery()->getFacets();
$queryFacets = $this->owner->queryFacets();
$me = $this->owner;
$convertFacets = function ($term, $raw) use ($facets, $queryFacets, $me) {
$result = array();
foreach ($raw as $facetTerm) {
// if it's a query facet, then we may have a label for it
if (isset($queryFacets[$facetTerm->Name])) {
$facetTerm->Name = $queryFacets[$facetTerm->Name];
}
$sq = $me->SearchQuery();
$sep = strlen($sq) ? '&' : '';
$facetTerm->SearchLink = $me->Link(self::RESULTS_ACTION) . '?' . $sq .$sep. SolrSearch::$filter_param . "[$term][]=$facetTerm->Query";
$facetTerm->QuotedSearchLink = $me->Link(self::RESULTS_ACTION) . '?' . $sq .$sep. SolrSearch::$filter_param . "[$term][]="$facetTerm->Query"";
$result[] = new ArrayData($facetTerm);
}
return $result;
};
if ($term) {
// return just that term
$ret = isset($facets[$term]) ? $facets[$term] : null;
// lets update them all and add a link parameter
$result = array();
if ($ret) {
$result = $convertFacets($term, $ret);
}
return new ArrayList($result);
} else {
$all = array();
foreach ($facets as $term => $ret) {
$result = $convertFacets($term, $ret);
$all = array_merge($all, $result);
}
return new ArrayList($all);
}
return new ArrayList($facets);
}
/**
* Add in any explicitly configured types.
*
* @param array $source
*/
public function updateSource(&$source) {
$objects = DataObject::get('SolrTypeConfiguration');
if ($objects) {
foreach ($objects as $obj) {
$source[$obj->Title] = $obj->Title;
}
}
}
/**
* Gets the list of query parsers available
*
* @return array
*/
public function getQueryBuilders() {
return $this->solrSearchService->getQueryBuilders();
}
}
class SolrSearch_Controller extends Extension {
protected function getSolr() {
return $this->owner->data()->getSolr();
}
public function FacetCrumbs() {
$activeFacets = $this->owner->data()->getActiveFacets();
$parts = array();
$queryString = $this->owner->data()->SearchQuery();
if (count($activeFacets)) {
foreach ($activeFacets as $facetName => $facetValues) {
foreach ($facetValues as $i => $v) {
$item = new stdClass();
$item->Name = $v;
$paramName = urlencode(SolrSearch::$filter_param . '[' . $facetName . '][' . $i . ']') .'='. urlencode($item->Name);
$item->RemoveLink = $this->owner->Link(self::RESULTS_ACTION) . '?' . str_replace($paramName, '', $queryString);
$parts[] = new ArrayData($item);
}
}
}
return new ArrayList($parts);
}
/**
* Process and render search results
*/
function getSearchResults($data = null, $form = null){
$query = $this->owner->data()->getQuery();
$term = isset($_GET['Search']) ? Convert::raw2xml($_GET['Search']) : '';
$results = $query ? $query->getDataObjects(true) : ArrayList::create();
$elapsed = '< 0.001';
if ($query) {
$resultData = array(
'TotalResults' => (($total = $query->getTotalResults()) ? $total : 0)
);
$time = $query->getTimeTaken();
if($time) {
$elapsed = $time / 1000;
}
} else {
$resultData = array();
}
$data = array(
'Results' => $results,
'Query' => Varchar::create_field('Varchar', $term),
'Title' => $this->owner->data()->Title,
'ResultData' => ArrayData::create($resultData),
'TimeTaken' => $elapsed
);
return $data;
}
/**
* Return the results with a template applied to them based on the page's listing template
*
*/
public function TemplatedResults() {
$query = $this->owner->data()->getQuery();
if ($this->owner->data()->ListingTemplateID && $query) {
$template = DataObject::get_by_id('ListingTemplate', $this->owner->data()->ListingTemplateID);
if ($template && $template->exists()) {
$items = $query ? $query->getDataObjects() : new DataObjectSet();
$item = $this->owner->data()->customise(array('Items' => $items));
$view = SSViewer::fromString($template->ItemTemplate);
return $view->process($item);
}
}
}
}
}