|
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
|
|
|
public function __construct() { |
|
12
|
|
|
$aspectAgg = new RangedAggregation('Aspect', 'AspectRatio'); |
|
13
|
|
|
$aspectAgg->addRange(0.0000001, 0.3, 'Panoramic'); |
|
14
|
|
|
$aspectAgg->addRange(0.3, 0.9, 'Horizontal'); |
|
15
|
|
|
$aspectAgg->addRange(0.9, 1.2, 'Square'); |
|
16
|
|
|
$aspectAgg->addRange(1.2, 1.79, 'Vertical'); |
|
17
|
|
|
$aspectAgg->addRange(1.79, 1e7, 'Tallest'); |
|
18
|
|
|
} |
|
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
|
|
|
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
|
|
|
$params = $query->getParams(); |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
$wildcard = array( |
|
|
|
|
|
|
36
|
|
|
'query_string' => array('query' => '*') |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
if ($query->OriginalQueryText == '') { |
|
40
|
|
|
$query->setSort(array('TakenAt'=> 'desc')); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// add Aperture aggregate |
|
44
|
|
|
$agg1 = new Terms("Aperture"); |
|
45
|
|
|
$agg1->setField("Aperture"); |
|
46
|
|
|
$agg1->setSize(0); |
|
47
|
|
|
$agg1->setOrder('_term', 'asc'); |
|
48
|
|
|
$query->addAggregation($agg1); |
|
49
|
|
|
|
|
50
|
|
|
// add shutter speed aggregate |
|
51
|
|
|
$agg2 = new Terms("ShutterSpeed"); |
|
52
|
|
|
$agg2->setField("ShutterSpeed"); |
|
53
|
|
|
$agg2->setSize(0); |
|
54
|
|
|
$agg2->setOrder('_term', 'asc'); |
|
55
|
|
|
$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
|
|
|
$agg3 = new Terms("FocalLength35mm"); |
|
61
|
|
|
$agg3->setField("FocalLength35mm"); |
|
62
|
|
|
$agg3->setSize(0); |
|
63
|
|
|
$agg3->setOrder('_term', 'asc'); |
|
64
|
|
|
$query->addAggregation($agg3); |
|
65
|
|
|
|
|
66
|
|
|
// add film speed |
|
67
|
|
|
$agg4 = new Terms("ISO"); |
|
68
|
|
|
$agg4->setField("ISO"); |
|
69
|
|
|
$agg4->setSize(0); |
|
70
|
|
|
$agg4->setOrder('_term', 'asc'); |
|
71
|
|
|
$query->addAggregation($agg4); |
|
72
|
|
|
|
|
73
|
|
|
$aspectRangedAgg = RangedAggregation::getByTitle('Aspect'); |
|
74
|
|
|
$query->addAggregation($aspectRangedAgg->getRangeAgg()); |
|
75
|
|
|
|
|
76
|
|
|
// remove NearestTo from the request so it does not get used as a term filter |
|
77
|
|
|
unset(Controller::curr()->request['NearestTo']); |
|
78
|
|
|
} |
|
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
|
|
|
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
|
|
|
if (isset($filters['ShutterSpeed'])) { |
|
92
|
|
|
|
|
93
|
|
|
$sortable = $filters['ShutterSpeed']; |
|
94
|
|
|
|
|
95
|
|
|
$sortable = explode('/', $sortable); |
|
96
|
|
|
if (sizeof($sortable) == 1) { |
|
97
|
|
|
$sortable = trim($sortable[0]); |
|
98
|
|
|
|
|
99
|
|
|
if ($sortable === '1') { |
|
100
|
|
|
$sortable = '1.000000|1'; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
} else if (sizeof($sortable) == 2) { |
|
104
|
|
|
if ($sortable[0] === '' || $sortable[1] === '') { |
|
105
|
|
|
$sortable = ''; |
|
106
|
|
|
|
|
107
|
|
|
} else { |
|
108
|
|
|
$sortable = floatval($sortable[0])/intval($sortable[1]); |
|
109
|
|
|
$sortable = round($sortable,6); |
|
110
|
|
|
$sortable = $sortable . '|' . $filters['ShutterSpeed']; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$filters['ShutterSpeed'] = $sortable; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
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
|
|
|
public function updateAggregation(&$aggs) { |
|
126
|
|
|
// the shutter speeds are of the form decimal number | fraction, keep the latter half |
|
127
|
|
|
$shutterSpeeds = $aggs['ShutterSpeed']['buckets']; |
|
128
|
|
|
$ctr = 0; |
|
129
|
|
|
foreach ($shutterSpeeds as $bucket) { |
|
130
|
|
|
$key = $bucket['key']; |
|
131
|
|
|
$splits = explode('|', $key); |
|
132
|
|
|
$shutterSpeeds[$ctr]['key'] = end($splits); |
|
133
|
|
|
$ctr++; |
|
134
|
|
|
} |
|
135
|
|
|
$aggs['ShutterSpeed']['buckets'] = $shutterSpeeds; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/* |
|
139
|
|
|
In the event of aggregates being used and no query provided, sort by this (<field> => <order>) |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getDefaultSort() { |
|
142
|
|
|
return array('TakenAt' => 'desc'); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
public function getIndexFieldTitleMapping() { |
|
147
|
|
|
return self::$titleFieldMapping; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|