|
1
|
|
|
<?php |
|
2
|
|
|
use \SilverStripe\Elastica\ElasticSearcher; |
|
3
|
|
|
|
|
4
|
|
|
class ElasticaAutoCompleteController extends Controller { |
|
5
|
|
|
private static $url_handlers = array( |
|
|
|
|
|
|
6
|
|
|
'search' => 'search' |
|
7
|
|
|
); |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
private static $allowed_actions = array('search'); |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
public function search() { |
|
14
|
|
|
$es = new ElasticSearcher(); |
|
15
|
|
|
$query = $this->request->getVar('query'); |
|
16
|
|
|
$query = trim($query); |
|
17
|
|
|
$classes = $this->request->getVar('classes'); |
|
18
|
|
|
$filter = $this->request->getVar('filter'); |
|
19
|
|
|
|
|
20
|
|
|
// Makes most sense to only provide one field here, e.g. Title, Name |
|
21
|
|
|
$field = $this->request->getVar('field'); |
|
22
|
|
|
|
|
23
|
|
|
error_log('QUERY:'.$query); |
|
24
|
|
|
|
|
25
|
|
|
// start, and page length, i.e. pagination |
|
26
|
|
|
$es->setPageLength(10); |
|
27
|
|
|
if ($classes) { |
|
28
|
|
|
$es->setClasses($classes); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
if ($filter) { |
|
32
|
|
|
$es->addFilter('IsInSiteTree', true); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$resultList = $es->autocomplete_search($query,$field); |
|
36
|
|
|
$result = array(); |
|
37
|
|
|
$result['Query'] = $query; |
|
38
|
|
|
$suggestions = array(); |
|
39
|
|
|
|
|
40
|
|
|
foreach ($resultList->getResults() as $singleResult) { |
|
41
|
|
|
$suggestion = array('value' => $singleResult->Title); |
|
42
|
|
|
$suggestion['data'] = array( |
|
43
|
|
|
'ID' => $singleResult->getParam('_id'), |
|
44
|
|
|
'Class' => $singleResult->getParam('_type'), |
|
45
|
|
|
'Link' => $singleResult->Link |
|
46
|
|
|
); |
|
47
|
|
|
array_push($suggestions, $suggestion); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$result['suggestions'] = $suggestions; |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
$json = json_encode($result); |
|
54
|
|
|
|
|
55
|
|
|
$this->response->addHeader('Content-Type', 'application/json'); |
|
56
|
|
|
//$this->response->setBody($json); |
|
|
|
|
|
|
57
|
|
|
return $json; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.