Skip to content
This repository has been archived by the owner on Jan 10, 2021. It is now read-only.

Commit

Permalink
[IMPROVEMENT] Implementing client side search suggestion filtering.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Glasl committed Feb 27, 2018
1 parent 4b6f0a9 commit 9e499a5
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 52 deletions.
47 changes: 37 additions & 10 deletions client/javascript/extensible-search-suggestions.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
;(function($) {
$(function() {

// Bind autocomplete to the search form.
// Bind autocomplete to the primary search form.

var search = $('input.extensible-search');
var search = $('input.extensible-search:first');
if(search.length) {

// Retrieve the search suggestions that have been approved.

var suggestions = [];
$.get('extensible-search-api/getPageSuggestions', {
page: search.data('extensible-search-page')
})
.done(function(data) {

suggestions = data;
});

// Initialise the autocomplete.

search.autocomplete({

// Determine whether to disable search suggestions, based on configuration.
Expand All @@ -15,20 +29,33 @@

minLength: 3,

// Retrieve the most relevant search suggestions that have been approved.
// Determine the most relevant search suggestions that have been approved.

source: function(request, response) {

$.get('extensible-search-api/getSuggestions', {
term: request.term,
page: search.data('extensible-search-page')
})
.done(function(data) {
// Perform client side filtering, which provides a massive performance increase!

response(data);
var term = search.val();
var options = [];
$.each(suggestions, function() {

if(term === this.substr(0, term.length)) {
options.push({
'label': term + '<strong>' + this.substr(term.length) + '</strong>',
'value': this
});
}
});
response(options);
}
});
})

// This needs to render HTML.

.data('ui-autocomplete')._renderItem = function(ul, item) {

return $('<li>').append($('<div>').html(item.label)).appendTo(ul);
}
}

});
Expand Down
18 changes: 9 additions & 9 deletions src/controllers/ExtensibleSearchAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class ExtensibleSearchAPI extends Controller {

private static $allowed_actions = array(
'toggleSuggestionApproved',
'getSuggestions',
'getPageSuggestions'
'getPageSuggestions',
'getSuggestions'
);

/**
Expand Down Expand Up @@ -59,17 +59,16 @@ public function toggleSuggestionApproved($request) {
}

/**
* Retrieve the most relevant search suggestions that have been approved.
* Retrieve the search suggestions that have been approved (great for client side filtering).
*
* @URLparameter term <{SEARCH_TERM}> string
* @URLparameter page <{EXTENSIBLE_SEARCH_PAGE_ID}> integer
* @return JSON
*/

public function getSuggestions($request) {
public function getPageSuggestions($request) {

if(Config::inst()->get(ExtensibleSearchSuggestion::class, 'enable_suggestions')) {
$suggestions = $this->service->getSuggestions($request->getVar('term'), $request->getVar('page'));
$suggestions = $this->service->getPageSuggestions($request->getVar('page'));

// Return the search suggestions as JSON.

Expand All @@ -85,16 +84,17 @@ public function getSuggestions($request) {
}

/**
* Retrieve the search suggestions for a page that have been approved (great for client side filtering).
* Retrieve the most relevant search suggestions that have been approved.
*
* @URLparameter term <{SEARCH_TERM}> string
* @URLparameter page <{EXTENSIBLE_SEARCH_PAGE_ID}> integer
* @return JSON
*/

public function getPageSuggestions($request) {
public function getSuggestions($request) {

if(Config::inst()->get(ExtensibleSearchSuggestion::class, 'enable_suggestions')) {
$suggestions = $this->service->getPageSuggestions($request->getVar('page'));
$suggestions = $this->service->getSuggestions($request->getVar('term'), $request->getVar('page'));

// Return the search suggestions as JSON.

Expand Down
66 changes: 33 additions & 33 deletions src/services/ExtensibleSearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,39 @@ public function toggleSuggestionApproved($ID) {
}
}

/**
* Retrieve the search suggestions for a page.
*
* @parameter <{EXTENSIBLE_SEARCH_PAGE_ID}> integer
* @parameter <{LIMIT}> integer
* @parameter <{APPROVED_ONLY}> boolean
* @return array
*/

public function getPageSuggestions($pageID, $limit = 0, $approved = true) {

// Make sure the current user has appropriate permission.

$pageID = (int)$pageID;
if(($page = ExtensibleSearchPage::get_by_id(ExtensibleSearchPage::class, $pageID)) && $page->canView()) {

// Retrieve the search suggestions.

$suggestions = ExtensibleSearchSuggestion::get()->filter(array(
'Approved' => (int)$approved,
'ExtensibleSearchPageID' => $pageID
))->sort('Frequency', 'DESC');
if($limit) {
$suggestions = $suggestions->limit($limit);
}

// Make sure the search suggestions are unique.

return array_unique($suggestions->column('Term'));
}
return array();
}

/**
* Retrieve the most relevant search suggestions.
*
Expand Down Expand Up @@ -182,37 +215,4 @@ public function getSuggestions($term, $pageID, $limit = 5, $approved = true) {
return array();
}

/**
* Retrieve the search suggestions for a page.
*
* @parameter <{EXTENSIBLE_SEARCH_PAGE_ID}> integer
* @parameter <{LIMIT}> integer
* @parameter <{APPROVED_ONLY}> boolean
* @return array
*/

public function getPageSuggestions($pageID, $limit = 0, $approved = true) {

// Make sure the current user has appropriate permission.

$pageID = (int)$pageID;
if(($page = ExtensibleSearchPage::get_by_id(ExtensibleSearchPage::class, $pageID)) && $page->canView()) {

// Retrieve the search suggestions.

$suggestions = ExtensibleSearchSuggestion::get()->filter(array(
'Approved' => (int)$approved,
'ExtensibleSearchPageID' => $pageID
))->sort('Frequency', 'DESC');
if($limit) {
$suggestions = $suggestions->limit($limit);
}

// Make sure the search suggestions are unique.

return array_unique($suggestions->column('Term'));
}
return array();
}

}

0 comments on commit 9e499a5

Please sign in to comment.