5 * Sphinx JavaScript utilties for the full-text search.
7 * :copyright: Copyright 2007-2014 by the Sphinx team, see AUTHORS.
8 * :license: BSD, see LICENSE for details.
16 var Stemmer = function() {
52 var c = "[^aeiou]"; // consonant
53 var v = "[aeiouy]"; // vowel
54 var C = c + "[^aeiouy]*"; // consonant sequence
55 var V = v + "[aeiou]*"; // vowel sequence
57 var mgr0 = "^(" + C + ")?" + V + C; // [C]VC... is m>0
58 var meq1 = "^(" + C + ")?" + V + C + "(" + V + ")?$"; // [C]VC[V] is m=1
59 var mgr1 = "^(" + C + ")?" + V + C + V + C; // [C]VCVC... is m>1
60 var s_v = "^(" + C + ")?" + v; // vowel in stem
62 this.stemWord = function (w) {
76 firstch = w.substr(0,1);
78 w = firstch.toUpperCase() + w.substr(1);
81 re = /^(.+?)(ss|i)es$/;
82 re2 = /^(.+?)([^s])s$/;
85 w = w.replace(re,"$1$2");
87 w = w.replace(re2,"$1$2");
91 re2 = /^(.+?)(ed|ing)$/;
94 re = new RegExp(mgr0);
100 else if (re2.test(w)) {
101 var fp = re2.exec(w);
103 re2 = new RegExp(s_v);
104 if (re2.test(stem)) {
107 re3 = new RegExp("([^aeiouylsz])\\1$");
108 re4 = new RegExp("^" + C + v + "[^aeiouwxy]$");
111 else if (re3.test(w)) {
113 w = w.replace(re,"");
115 else if (re4.test(w))
125 re = new RegExp(s_v);
131 re = /^(.+?)(ational|tional|enci|anci|izer|bli|alli|entli|eli|ousli|ization|ation|ator|alism|iveness|fulness|ousness|aliti|iviti|biliti|logi)$/;
136 re = new RegExp(mgr0);
138 w = stem + step2list[suffix];
142 re = /^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$/;
147 re = new RegExp(mgr0);
149 w = stem + step3list[suffix];
153 re = /^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize)$/;
154 re2 = /^(.+?)(s|t)(ion)$/;
158 re = new RegExp(mgr1);
162 else if (re2.test(w)) {
163 var fp = re2.exec(w);
164 stem = fp[1] + fp[2];
165 re2 = new RegExp(mgr1);
175 re = new RegExp(mgr1);
176 re2 = new RegExp(meq1);
177 re3 = new RegExp("^" + C + v + "[^aeiouwxy]$");
178 if (re.test(stem) || (re2.test(stem) && !(re3.test(stem))))
182 re2 = new RegExp(mgr1);
183 if (re.test(w) && re2.test(w)) {
185 w = w.replace(re,"");
188 // and turn initial Y back to y
190 w = firstch.toLowerCase() + w.substr(1);
198 * Simple result scoring code.
201 // Implement the following function to further tweak the score for each result
202 // The function takes a result array [filename, title, anchor, descr, score]
203 // and returns the new score.
205 score: function(result) {
210 // query matches the full name of an object
212 // or matches in the last dotted part of the object name
214 // Additive scores depending on the priority of the object
215 objPrio: {0: 15, // used to be importantResults
216 1: 5, // used to be objectResults
217 2: -5}, // used to be unimportantResults
218 // Used when the priority is not in the mapping.
221 // query found in title
223 // query found in terms
234 _queued_query : null,
238 var params = $.getQueryParameters();
240 var query = params.q[0];
241 $('input[name="q"]')[0].value = query;
242 this.performSearch(query);
246 loadIndex : function(url) {
247 $.ajax({type: "GET", url: url, data: null,
248 dataType: "script", cache: true,
249 complete: function(jqxhr, textstatus) {
250 if (textstatus != "success") {
251 document.getElementById("searchindexloader").src = url;
256 setIndex : function(index) {
259 if ((q = this._queued_query) !== null) {
260 this._queued_query = null;
265 hasIndex : function() {
266 return this._index !== null;
269 deferQuery : function(query) {
270 this._queued_query = query;
273 stopPulse : function() {
274 this._pulse_status = 0;
277 startPulse : function() {
278 if (this._pulse_status >= 0)
282 Search._pulse_status = (Search._pulse_status + 1) % 4;
284 for (i = 0; i < Search._pulse_status; i++)
286 Search.dots.text(dotString);
287 if (Search._pulse_status > -1)
288 window.setTimeout(pulse, 500);
294 * perform a search for something (or wait until index is loaded)
296 performSearch : function(query) {
297 // create the required interface elements
298 this.out = $('#search-results');
299 this.title = $('<h2>' + _('Searching') + '</h2>').appendTo(this.out);
300 this.dots = $('<span></span>').appendTo(this.title);
301 this.status = $('<p style="display: none"></p>').appendTo(this.out);
302 this.output = $('<ul class="search"/>').appendTo(this.out);
304 $('#search-progress').text(_('Preparing search...'));
307 // index already loaded, the browser was quick!
311 this.deferQuery(query);
315 * execute search (requires search index to be loaded)
317 query : function(query) {
319 var stopwords = ["a","and","are","as","at","be","but","by","for","if","in","into","is","it","near","no","not","of","on","or","such","that","the","their","then","there","these","they","this","to","was","will","with"];
321 // stem the searchterms and add them to the correct list
322 var stemmer = new Stemmer();
323 var searchterms = [];
326 var tmp = query.split(/\s+/);
327 var objectterms = [];
328 for (i = 0; i < tmp.length; i++) {
330 objectterms.push(tmp[i].toLowerCase());
333 if ($u.indexOf(stopwords, tmp[i].toLowerCase()) != -1 || tmp[i].match(/^\d+$/) ||
339 var word = stemmer.stemWord(tmp[i].toLowerCase());
341 // select the correct list
342 if (word[0] == '-') {
344 word = word.substr(1);
347 toAppend = searchterms;
348 hlterms.push(tmp[i].toLowerCase());
350 // only add if not already in the list
351 if (!$u.contains(toAppend, word))
354 var highlightstring = '?highlight=' + $.urlencode(hlterms.join(" "));
356 // console.debug('SEARCH: searching for:');
357 // console.info('required: ', searchterms);
358 // console.info('excluded: ', excluded);
361 var terms = this._index.terms;
362 var titleterms = this._index.titleterms;
364 // array of [filename, title, anchor, descr, score]
366 $('#search-progress').empty();
369 for (i = 0; i < objectterms.length; i++) {
370 var others = [].concat(objectterms.slice(0, i),
371 objectterms.slice(i+1, objectterms.length));
372 results = results.concat(this.performObjectSearch(objectterms[i], others));
375 // lookup as search terms in fulltext
376 results = results.concat(this.performTermsSearch(searchterms, excluded, terms, Scorer.term))
377 .concat(this.performTermsSearch(searchterms, excluded, titleterms, Scorer.title));
379 // let the scorer override scores with a custom scoring function
381 for (i = 0; i < results.length; i++)
382 results[i][4] = Scorer.score(results[i]);
385 // now sort the results by score (in opposite order of appearance, since the
386 // display function below uses pop() to retrieve items) and then
388 results.sort(function(a, b) {
393 } else if (left < right) {
396 // same score: sort alphabetically
397 left = a[1].toLowerCase();
398 right = b[1].toLowerCase();
399 return (left > right) ? -1 : ((left < right) ? 1 : 0);
404 //Search.lastresults = results.slice(); // a copy
405 //console.info('search results:', Search.lastresults);
408 var resultCount = results.length;
409 function displayNextItem() {
410 // results left, load the summary and display it
411 if (results.length) {
412 var item = results.pop();
413 var listItem = $('<li style="display:none"></li>');
414 if (DOCUMENTATION_OPTIONS.FILE_SUFFIX === '') {
416 var dirname = item[0] + '/';
417 if (dirname.match(/\/index\/$/)) {
418 dirname = dirname.substring(0, dirname.length-6);
419 } else if (dirname == 'index/') {
422 listItem.append($('<a/>').attr('href',
423 DOCUMENTATION_OPTIONS.URL_ROOT + dirname +
424 highlightstring + item[2]).html(item[1]));
426 // normal html builders
427 listItem.append($('<a/>').attr('href',
428 item[0] + DOCUMENTATION_OPTIONS.FILE_SUFFIX +
429 highlightstring + item[2]).html(item[1]));
432 listItem.append($('<span> (' + item[3] + ')</span>'));
433 Search.output.append(listItem);
434 listItem.slideDown(5, function() {
437 } else if (DOCUMENTATION_OPTIONS.HAS_SOURCE) {
438 $.ajax({url: DOCUMENTATION_OPTIONS.URL_ROOT + '_sources/' + item[0] + '.txt',
440 complete: function(jqxhr, textstatus) {
441 var data = jqxhr.responseText;
443 listItem.append(Search.makeSearchSummary(data, searchterms, hlterms));
445 Search.output.append(listItem);
446 listItem.slideDown(5, function() {
451 // no source available, just display title
452 Search.output.append(listItem);
453 listItem.slideDown(5, function() {
458 // search finished, update title and status message
461 Search.title.text(_('Search Results'));
463 Search.status.text(_('Your search did not match any documents. Please make sure that all words are spelled correctly and that you\'ve selected enough categories.'));
465 Search.status.text(_('Search finished, found %s page(s) matching the search query.').replace('%s', resultCount));
466 Search.status.fadeIn(500);
473 * search for object names
475 performObjectSearch : function(object, otherterms) {
476 var filenames = this._index.filenames;
477 var objects = this._index.objects;
478 var objnames = this._index.objnames;
479 var titles = this._index.titles;
484 for (var prefix in objects) {
485 for (var name in objects[prefix]) {
486 var fullname = (prefix ? prefix + '.' : '') + name;
487 if (fullname.toLowerCase().indexOf(object) > -1) {
489 var parts = fullname.split('.');
490 // check for different match types: exact matches of full name or
491 // "last name" (i.e. last dotted part)
492 if (fullname == object || parts[parts.length - 1] == object) {
493 score += Scorer.objNameMatch;
494 // matches in last name
495 } else if (parts[parts.length - 1].indexOf(object) > -1) {
496 score += Scorer.objPartialMatch;
498 var match = objects[prefix][name];
499 var objname = objnames[match[1]][2];
500 var title = titles[match[0]];
501 // If more than one term searched for, we require other words to be
502 // found in the name/title/description
503 if (otherterms.length > 0) {
504 var haystack = (prefix + ' ' + name + ' ' +
505 objname + ' ' + title).toLowerCase();
507 for (i = 0; i < otherterms.length; i++) {
508 if (haystack.indexOf(otherterms[i]) == -1) {
517 var descr = objname + _(', in ') + title;
519 var anchor = match[3];
522 else if (anchor == '-')
523 anchor = objnames[match[1]][1] + '-' + fullname;
524 // add custom score for some objects according to scorer
525 if (Scorer.objPrio.hasOwnProperty(match[2])) {
526 score += Scorer.objPrio[match[2]];
528 score += Scorer.objPrioDefault;
530 results.push([filenames[match[0]], fullname, '#'+anchor, descr, score]);
539 * search for full-text terms in the index
541 performTermsSearch : function(searchterms, excluded, terms, score) {
542 var filenames = this._index.filenames;
543 var titles = this._index.titles;
545 var i, j, file, files;
549 // perform the search on the required terms
550 for (i = 0; i < searchterms.length; i++) {
551 var word = searchterms[i];
552 // no match but word was a required one
553 if ((files = terms[word]) === undefined)
555 if (files.length === undefined) {
558 // create the mapping
559 for (j = 0; j < files.length; j++) {
562 fileMap[file].push(word);
564 fileMap[file] = [word];
568 // now check if the files don't contain excluded terms
569 for (file in fileMap) {
572 // check if all requirements are matched
573 if (fileMap[file].length != searchterms.length)
576 // ensure that none of the excluded terms is in the search result
577 for (i = 0; i < excluded.length; i++) {
578 if (terms[excluded[i]] == file ||
579 $u.contains(terms[excluded[i]] || [], file)) {
585 // if we have still a valid result we can add it to the result list
587 results.push([filenames[file], titles[file], '', null, score]);
594 * helper function to return a node containing the
595 * search summary for a given text. keywords is a list
596 * of stemmed words, hlwords is the list of normal, unstemmed
597 * words. the first one is used to find the occurance, the
598 * latter for highlighting it.
600 makeSearchSummary : function(text, keywords, hlwords) {
601 var textLower = text.toLowerCase();
603 $.each(keywords, function() {
604 var i = textLower.indexOf(this.toLowerCase());
608 start = Math.max(start - 120, 0);
609 var excerpt = ((start > 0) ? '...' : '') +
610 $.trim(text.substr(start, 240)) +
611 ((start + 240 - text.length) ? '...' : '');
612 var rv = $('<div class="context"></div>').text(excerpt);
613 $.each(hlwords, function() {
614 rv = rv.highlightText(this, 'highlighted');
620 $(document).ready(function() {