Source of file ElasticaAutoCompleteController.php
Size: 1,679 Bytes - Last Modified: 2021-12-23T10:58:34+00:00
/var/www/docs.ssmods.com/process/src/code/ElasticaAutoCompleteController.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 | <?php use \SilverStripe\Elastica\ElasticaSearcher; class ElasticaAutoCompleteController extends Controller { private static $url_handlers = array( 'search' => 'search', ); private static $allowed_actions = array('search'); public function search() { $es = new ElasticaSearcher(); $query = $this->request->getVar('query'); $query = trim($query); $classes = $this->request->getVar('classes'); $filter = $this->request->getVar('filter'); // Makes most sense to only provide one field here, e.g. Title, Name $field = $this->request->getVar('field'); // start, and page length, i.e. pagination $es->setPageLength(10); if ($classes) { $es->setClasses($classes); } if ($filter) { $es->addFilter('IsInSiteTree', true); } $resultList = $es->autocomplete_search($query, $field); $result = array(); $result['Query'] = $query; $suggestions = array(); foreach ($resultList->getResults() as $singleResult) { $suggestion = array('value' => $singleResult->Title); $suggestion['data'] = array( 'ID' => $singleResult->getParam('_id'), 'Class' => $singleResult->getParam('_type'), 'Link' => $singleResult->Link, ); array_push($suggestions, $suggestion); } $result['suggestions'] = $suggestions; $json = json_encode($result); $this->response->addHeader('Content-Type', 'application/json'); //$this->response->setBody($json); return $json; } } |