Completed
Push — dev2 ( a77d0b...0f20ec )
by Gordon
02:57
created

ElasticSearchPage_Controller::index()   B

Complexity

Conditions 2
Paths 7

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 24
rs 8.9714
cc 2
eloc 17
nc 7
nop 0
1
<?php
2
3
use Elastica\Document;
4
use Elastica\Query;
5
use \SilverStripe\Elastica\ResultList;
6
use Elastica\Query\QueryString;
7
use Elastica\Aggregation\Filter;
8
use Elastica\Filter\Term;
9
use Elastica\Filter\BoolAnd;
10
use Elastica\Aggregation\Terms;
11
use Elastica\Query\Filtered;
12
use Elastica\Query\Range;
13
use \SilverStripe\Elastica\ElasticSearcher;
14
use \SilverStripe\Elastica\Searchable;
15
use \SilverStripe\Elastica\QueryGenerator;
16
use \SilverStripe\Elastica\ElasticaUtil;
17
18
class ElasticSearchPage_Controller extends Page_Controller {
19
20
	private static $allowed_actions = array('SearchForm', 'submit', 'index', 'similar');
21
22
	public function init() {
23
		parent::init();
24
25
		Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
26
		Requirements::javascript("elastica/javascript/jquery.autocomplete.js");
27
		Requirements::javascript("elastica/javascript/elastica.js");
28
		Requirements::css("elastica/css/elastica.css");
29
30
		$this->SearchPage = Controller::curr()->dataRecord;
31
	}
32
33
34
35
	/*
36
	Find DataObjects in Elasticsearch similar to the one selected.  Note that aggregations are not
37
	taken into account, merely the text of the selected document.
38
	 */
39
	public function similar() {
40
		//FIXME double check security, ie if escaping needed
41
		$class = $this->request->param('ID');
42
		$instanceID = $this->request->param('OtherID');
43
44
		$es = $this->primeElasticSearcherFromRequest();
45
		$data = $this->initialiseDataArray();
46
		$this->setMoreLikeThisParamsFromRequest($es);
47
		$this->addSiteTreeFilterIfRequired($es);
48
		$this->getSelectedSearchFields();
49
50
		try {
51
			$this->checkForSimulatedServerDown();
52
			if(class_exists($class)) {
53
				$instance = \DataObject::get_by_id($class, $instanceID);
54
55
				$paginated = $es->moreLikeThis($instance, $this->FieldsToSearch);
56
57
				$this->Aggregations = $es->getAggregations();
58
				$this->successfulSearch($data, $paginated);
59
				$data['SimilarTo'] = $instance;
60
61
62
				$moreLikeThisTerms = $paginated->getList()->MoreLikeThisTerms;
63
				$fieldToTerms = new ArrayList();
64
				foreach(array_keys($moreLikeThisTerms) as $fieldName) {
65
					$readableFieldName = str_replace('.standard', '', $fieldName);
66
					$fieldTerms = new ArrayList();
67
					foreach($moreLikeThisTerms[$fieldName] as $value) {
68
						$do = new DataObject();
69
						$do->Term = $value;
70
						$fieldTerms->push($do);
71
					}
72
73
					$do = new DataObject();
74
					$do->FieldName = $readableFieldName;
75
					$do->Terms = $fieldTerms;
76
					$fieldToTerms->push($do);
77
				}
78
79
				$data['SimilarSearchTerms'] = $fieldToTerms;
80
			} else {
81
				// class does not exist
82
				$data['ErrorMessage'] = "Class $class is either not found or not searchable\n";
83
			}
84
		} catch (\InvalidArgumentException $e) {
85
			$data['ErrorMessage'] = "Class $class is either not found or not searchable\n";
86
		} catch (Elastica\Exception\Connection\HttpException $e) {
87
			$data['ErrorMessage'] = 'Unable to connect to search server';
88
		}
89
90
		$data['ElapsedTime'] = $this->calculateTime();
91
		return $this->renderResults($data);
92
	}
93
94
95
96
	/*
97
	Display the search form. If the query parameter exists, search against Elastica
98
	and render results accordingly.
99
	 */
100
	public function index() {
101
		$es = $this->primeElasticSearcherFromRequest();
102
		$data = $this->initialiseDataArray();
103
		$this->dealWithAggregation($es);
104
		$this->addSiteTreeFilterIfRequired($es);
105
		$this->getSelectedSearchFields();
106
107
		$paginated = null;
108
		try {
109
			$this->checkForSimulatedServerDown();
110
111
			// now actually perform the search using the original query
112
			$paginated = $es->search($this->QueryText, $this->FieldsToSearch,$this->TestMode);
113
			$this->dealWithSuggestedQuery($es, $data, $paginated);
114
115
			$data['ElapsedTime'] = $this->calculateTime();
116
			$this->Aggregations = $es->getAggregations();
117
			$this->successfulSearch($data, $paginated);
118
		} catch (Elastica\Exception\Connection\HttpException $e) {
119
			$data['ErrorMessage'] = 'Unable to connect to search server';
120
		}
121
122
		return $this->renderResults($data);
123
	}
124
125
126
	private function dealWithSuggestedQuery(&$es, &$data, &$paginated) {
127
		// This is the case of the original query having a better one suggested.  Do a
128
		// second search for the suggested query, throwing away the original
129
		if($es->hasSuggestedQuery() && !$this->IgnoreSuggestions) {
130
			$data['SuggestedQuery'] = $es->getSuggestedQuery();
131
			$data['SuggestedQueryHighlighted'] = $es->getSuggestedQueryHighlighted();
132
			//Link for if the user really wants to try their original query
133
			$sifLink = rtrim($this->Link(), '/') . '?q=' . $this->QueryText . '&is=1';
134
			$data['SearchInsteadForLink'] = $sifLink;
135
			$paginated = $es->search($es->getSuggestedQuery(), $this->FieldsToSearch);
136
		}
137
	}
138
139
140
	private function successfulSearch(&$data, $paginated) {
141
		$data['SearchResults'] = $paginated;
142
		$data['SearchPerformed'] = true;
143
		$data['NumberOfResults'] = $paginated->getTotalItems();
144
		$data['SearchPageLink'] = $this->SearchPage->Link();
145
	}
146
147
148
	/*
149
	Return true if the query is not empty
150
	 */
151
	public function QueryIsEmpty() {
152
		return empty($this->request->getVar('q'));
153
	}
154
155
156
	/**
157
	 * Process submission of the search form, redirecting to a URL that will render search results
158
	 * @param  array $data form data
159
	 * @param  Form $form form
160
	 */
161
	public function submit($data, $form) {
162
		$queryText = $data['q'];
163
		$url = $this->Link();
164
		$url = rtrim($url, '/');
165
		$link = rtrim($url, '/') . '?q=' . $queryText . '&sfid=' . $data['identifier'];
166
		$this->redirect($link);
167
	}
168
169
170
	/*
171
	Obtain an instance of the form
172
	*/
173
	public function SearchForm() {
174
		$form = new ElasticSearchForm($this, 'SearchForm');
175
		$fields = $form->Fields();
176
		$elasticaSearchPage = Controller::curr()->dataRecord;
177
		$identifierField = new HiddenField('identifier');
178
		$identifierField->setValue($elasticaSearchPage->Identifier);
179
180
		$fields->push($identifierField);
181
		$queryField = $fields->fieldByName('q');
182
183
		 if($this->isParamSet('q') && $this->isParamSet('sfid')) {
184
		 	$sfid = $this->request->getVar('sfid');
185
			if($sfid == $elasticaSearchPage->Identifier) {
186
187
				$queryText = $this->request->getVar('q');
188
				$queryField->setValue($queryText);
189
			}
190
191
		}
192
193
		if($this->action == 'similar') {
194
			$queryField->setDisabled(true);
195
			$actions = $form->Actions();
196
			if(!empty($actions)) {
197
				foreach($actions as $field) {
198
					$field->setDisabled(true);
199
				}
200
			}
201
202
		}
203
204
		if($this->AutoCompleteFieldID > 0) {
205
			ElasticaUtil::addAutocompleteToQueryField(
206
				$queryField,
207
				$this->ClassesToSearch,
208
				$this->SiteTreeOnly,
209
				$this->Link(),
210
				$this->AutocompleteFunction()->Slug
211
			);
212
		}
213
		return $form;
214
	}
215
216
217
	/**
218
	 * @param string $paramName
219
	 */
220
	private function isParamSet($paramName) {
221
		return !empty($this->request->getVar($paramName));
222
	}
223
224
225
	/**
226
	 * Set the start page from the request and results per page for a given searcher object
227
	 */
228
	private function primeElasticSearcherFromRequest() {
229
		$elasticSearcher = new ElasticSearcher();
230
		// start, and page length, i.e. pagination
231
		$startParam = $this->request->getVar('start');
232
		$start = isset($startParam) ? $startParam : 0;
233
		$elasticSearcher->setStart($start);
234
		$this->StartTime = microtime(true);
235
		$elasticSearcher->setPageLength($this->SearchPage->ResultsPerPage);
236
237
		// Do not show suggestions if this flag is set
238
		$this->IgnoreSuggestions = null !== $this->request->getVar('is');
239
240
		// query string
241
		$queryTextParam = $this->request->getVar('q');
242
		$queryText = !empty($queryTextParam) ? $queryTextParam : '';
243
		$this->QueryText = $queryText;
244
245
		$this->TestMode = !empty($this->request->getVar('TestMode'));
246
		return $elasticSearcher;
247
	}
248
249
250
	/**
251
	 * Set the admin configured similarity parameters
252
	 * @param \SilverStripe\Elastica\ElasticSearcher &$elasticSearcher ElasticaSearcher object
253
	 */
254
	private function setMoreLikeThisParamsFromRequest(&$elasticSearcher) {
255
		$elasticSearcher->setMinTermFreq($this->MinTermFreq);
256
		$elasticSearcher->setMaxTermFreq($this->MaxTermFreq);
257
		$elasticSearcher->setMinDocFreq($this->MinDocFreq);
258
		$elasticSearcher->setMaxDocFreq($this->MaxDocFreq);
259
		$elasticSearcher->setMinWordLength($this->MinWordLength);
260
		$elasticSearcher->setMaxWordLength($this->MaxWordLength);
261
		$elasticSearcher->setMinShouldMatch($this->MinShouldMatch);
262
		$elasticSearcher->setSimilarityStopWords($this->SimilarityStopWords);
263
	}
264
265
266
	private function dealWithAggregation(&$es) {
267
		$ignore = \Config::inst()->get('Elastica', 'BlackList');
268
		foreach($this->request->getVars() as $key => $value) {
269
			if(!in_array($key, $ignore)) {
270
				$es->addFilter($key, $value);
271
			}
272
		}
273
274
		// set the optional aggregation manipulator
275
		// In the event of a manipulator being present, show all the results for search
276
		// Otherwise aggregations are all zero
277
		if($this->SearchHelper) {
278
			$es->setQueryResultManipulator($this->SearchHelper);
279
			$es->showResultsForEmptySearch();
280
		} else {
281
			$es->hideResultsForEmptySearch();
282
		}
283
	}
284
285
286
	private function addSiteTreeFilterIfRequired(&$es) {
287
		// filter by class or site tree
288
		if($this->SearchPage->SiteTreeOnly) {
289
			$es->addFilter('IsInSiteTree', true);
290
		} else {
291
			$es->setClasses($this->SearchPage->ClassesToSearch);
292
		}
293
	}
294
295
296
	private function getSelectedSearchFields() {
297
		// get the edited fields to search from the database for this search page
298
		// Convert this into a name => weighting array
299
		$fieldsToSearch = array();
300
		$editedSearchFields = $this->ElasticaSearchableFields()->filter(array(
301
			'Active' => true,
302
			'Searchable' => true
303
		));
304
305
		foreach($editedSearchFields->getIterator() as $searchField) {
306
			$fieldsToSearch[$searchField->Name] = $searchField->Weight;
307
		}
308
309
		// For a 'more like this' search, use the original unstemmed words
310
		// This means using the .standard named field of text
311
		if ($this->action == 'similar') {
312
			// Use the standard field for more like this, ie not stemmed
313
			foreach($fieldsToSearch as $field => $value) {
314
				$fieldsToSearch[$field . '.standard'] = $value;
315
				unset($fieldsToSearch[$field]);
316
			}
317
		}
318
		$this->FieldsToSearch = $fieldsToSearch;
319
	}
320
321
322
	private function initialiseDataArray() {
323
		return array(
324
			'Content' => $this->Content,
325
			'Title' => $this->Title,
326
			'SearchPerformed' => false,
327
			'OriginalQuery' => $this->QueryText,
328
			'IgnoreSuggestions' => $this->IgnoreSuggestions
329
		);
330
	}
331
332
333
	private function renderResults($data) {
334
		// allow the optional use of overriding the search result page, e.g. for photos, maps or facets
335
		if($this->hasExtension('PageControllerTemplateOverrideExtension')) {
336
			return $this->useTemplateOverride($data);
337
		} else {
338
			return $data;
339
		}
340
	}
341
342
343
	private function calculateTime() {
344
		$endTime = microtime(true);
345
		$elapsed = round(100 * ($endTime - $this->StartTime)) / 100;
346
		return $elapsed;
347
	}
348
349
350
	private function checkForSimulatedServerDown() {
351
		// Simulate server being down for testing purposes
352
		if(!empty($this->request->getVar('ServerDown'))) {
353
			throw new Elastica\Exception\Connection\HttpException('Unable to reach search server');
354
		}
355
	}
356
}
357