1
|
|
|
<?php |
2
|
|
|
use Elastica\Aggregation\Terms; |
3
|
|
|
use Elastica\Query; |
4
|
|
|
use Elastica\Aggregation\TopHits; |
5
|
|
|
use Elastica\Aggregation\Range; |
6
|
|
|
use SilverStripe\Elastica\RangedAggregation; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class FlickrPhotoTOElasticaSearchHelper implements ElasticaSearchHelperInterface,TestOnly { |
|
|
|
|
10
|
|
|
|
11
|
10 |
|
public function __construct() { |
12
|
10 |
|
$aspectAgg = new RangedAggregation('Aspect', 'AspectRatio'); |
13
|
10 |
|
$aspectAgg->addRange(0.0000001, 0.3, 'Panoramic'); |
14
|
10 |
|
$aspectAgg->addRange(0.3, 0.9, 'Horizontal'); |
15
|
10 |
|
$aspectAgg->addRange(0.9, 1.2, 'Square'); |
16
|
10 |
|
$aspectAgg->addRange(1.2, 1.79, 'Vertical'); |
17
|
10 |
|
$aspectAgg->addRange(1.79, 1e7, 'Tallest'); |
18
|
10 |
|
} |
19
|
|
|
|
20
|
|
|
private static $titleFieldMapping = array( |
21
|
|
|
'ShutterSpeed' => 'Shutter Speed', |
22
|
|
|
'FocalLength35mm' => 'Focal Length' |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Add a number of facets to the FlickrPhotoTO query |
28
|
|
|
* @param \Elastica\Query &$query the existing query object to be augmented. |
29
|
|
|
*/ |
30
|
9 |
|
public function augmentQuery(&$query) { |
31
|
|
|
//FIXME probably move this back as default behaviour |
32
|
|
|
// set the order to be taken at in reverse if query is blank other than aggs |
33
|
9 |
|
$params = $query->getParams(); |
|
|
|
|
34
|
|
|
|
35
|
|
|
$wildcard = array( |
|
|
|
|
36
|
9 |
|
'query_string' => array('query' => '*') |
37
|
9 |
|
); |
38
|
|
|
|
39
|
9 |
|
if ($query->OriginalQueryText == '') { |
40
|
3 |
|
$query->setSort(array('TakenAt'=> 'desc')); |
41
|
3 |
|
} |
42
|
|
|
|
43
|
|
|
// add Aperture aggregate |
44
|
9 |
|
$agg1 = new Terms("Aperture"); |
45
|
9 |
|
$agg1->setField("Aperture"); |
46
|
9 |
|
$agg1->setSize(0); |
47
|
9 |
|
$agg1->setOrder('_term', 'asc'); |
48
|
9 |
|
$query->addAggregation($agg1); |
49
|
|
|
|
50
|
|
|
// add shutter speed aggregate |
51
|
9 |
|
$agg2 = new Terms("ShutterSpeed"); |
52
|
9 |
|
$agg2->setField("ShutterSpeed"); |
53
|
9 |
|
$agg2->setSize(0); |
54
|
9 |
|
$agg2->setOrder('_term', 'asc'); |
55
|
9 |
|
$query->addAggregation($agg2); |
56
|
|
|
|
57
|
|
|
// this currently needs to be same as the field name |
58
|
|
|
// needs fixed |
59
|
|
|
// Add focal length aggregate, may range this |
60
|
9 |
|
$agg3 = new Terms("FocalLength35mm"); |
61
|
9 |
|
$agg3->setField("FocalLength35mm"); |
62
|
9 |
|
$agg3->setSize(0); |
63
|
9 |
|
$agg3->setOrder('_term', 'asc'); |
64
|
9 |
|
$query->addAggregation($agg3); |
65
|
|
|
|
66
|
|
|
// add film speed |
67
|
9 |
|
$agg4 = new Terms("ISO"); |
68
|
9 |
|
$agg4->setField("ISO"); |
69
|
9 |
|
$agg4->setSize(0); |
70
|
9 |
|
$agg4->setOrder('_term', 'asc'); |
71
|
9 |
|
$query->addAggregation($agg4); |
72
|
|
|
|
73
|
9 |
|
$aspectRangedAgg = RangedAggregation::getByTitle('Aspect'); |
74
|
9 |
|
$query->addAggregation($aspectRangedAgg->getRangeAgg()); |
75
|
|
|
|
76
|
|
|
// remove NearestTo from the request so it does not get used as a term filter |
77
|
9 |
|
unset(Controller::curr()->request['NearestTo']); |
78
|
9 |
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Update filters, perhaps remaps them, prior to performing a search. |
83
|
|
|
* This allows for aggregation values to be updated prior to rendering. |
84
|
|
|
* @param array &$filters array of key/value pairs for query filtering |
85
|
|
|
*/ |
86
|
9 |
|
public function updateFilters(&$filters) { |
87
|
|
|
|
88
|
|
|
// shutter speed is stored as decimal to 6 decimal places, then a |
89
|
|
|
// vertical bar followed by the displayed speed as a fraction or a |
90
|
|
|
// whole number. This puts the decimal back for matching purposes |
91
|
9 |
|
if (isset($filters['ShutterSpeed'])) { |
92
|
|
|
|
93
|
1 |
|
$sortable = $filters['ShutterSpeed']; |
94
|
|
|
|
95
|
1 |
|
$sortable = explode('/', $sortable); |
96
|
1 |
|
if (sizeof($sortable) == 1) { |
97
|
1 |
|
$sortable = trim($sortable[0]); |
98
|
|
|
|
99
|
1 |
|
if ($sortable === '1') { |
100
|
1 |
|
$sortable = '1.000000|1'; |
101
|
1 |
|
} |
102
|
|
|
|
103
|
1 |
|
} else if (sizeof($sortable) == 2) { |
104
|
1 |
|
if ($sortable[0] === '' || $sortable[1] === '') { |
105
|
1 |
|
$sortable = ''; |
106
|
|
|
|
107
|
1 |
|
} else { |
108
|
1 |
|
$sortable = floatval($sortable[0])/intval($sortable[1]); |
109
|
1 |
|
$sortable = round($sortable,6); |
110
|
1 |
|
$sortable = $sortable . '|' . $filters['ShutterSpeed']; |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
} |
114
|
|
|
|
115
|
1 |
|
$filters['ShutterSpeed'] = $sortable; |
116
|
1 |
|
} |
117
|
9 |
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Manipulate the results, e.g. fixing up values if issues with ordering in Elastica |
123
|
|
|
* @param array &$aggs Aggregates from an Elastica search to be tweaked |
124
|
|
|
*/ |
125
|
9 |
|
public function updateAggregation(&$aggs) { |
126
|
|
|
// the shutter speeds are of the form decimal number | fraction, keep the latter half |
127
|
9 |
|
$shutterSpeeds = $aggs['ShutterSpeed']['buckets']; |
128
|
9 |
|
$ctr = 0; |
129
|
9 |
|
foreach ($shutterSpeeds as $bucket) { |
130
|
9 |
|
$key = $bucket['key']; |
131
|
9 |
|
$splits = explode('|', $key); |
132
|
9 |
|
$shutterSpeeds[$ctr]['key'] = end($splits); |
133
|
9 |
|
$ctr++; |
134
|
9 |
|
} |
135
|
9 |
|
$aggs['ShutterSpeed']['buckets'] = $shutterSpeeds; |
136
|
9 |
|
} |
137
|
|
|
|
138
|
|
|
/* |
139
|
|
|
In the event of aggregates being used and no query provided, sort by this (<field> => <order>) |
140
|
|
|
*/ |
141
|
3 |
|
public function getDefaultSort() { |
142
|
3 |
|
return array('TakenAt' => 'desc'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
|
146
|
9 |
|
public function getIndexFieldTitleMapping() { |
147
|
9 |
|
return self::$titleFieldMapping; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|