[1e781ba] | 1 | /*
|
---|
| 2 | * searchtools.js
|
---|
| 3 | * ~~~~~~~~~~~~~~~~
|
---|
| 4 | *
|
---|
| 5 | * Sphinx JavaScript utilities for the full-text search.
|
---|
| 6 | *
|
---|
| 7 | * :copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
|
---|
| 8 | * :license: BSD, see LICENSE for details.
|
---|
| 9 | *
|
---|
| 10 | */
|
---|
| 11 |
|
---|
| 12 | if (!Scorer) {
|
---|
| 13 | /**
|
---|
| 14 | * Simple result scoring code.
|
---|
| 15 | */
|
---|
| 16 | var Scorer = {
|
---|
| 17 | // Implement the following function to further tweak the score for each result
|
---|
| 18 | // The function takes a result array [filename, title, anchor, descr, score]
|
---|
| 19 | // and returns the new score.
|
---|
| 20 | /*
|
---|
| 21 | score: function(result) {
|
---|
| 22 | return result[4];
|
---|
| 23 | },
|
---|
| 24 | */
|
---|
| 25 |
|
---|
| 26 | // query matches the full name of an object
|
---|
| 27 | objNameMatch: 11,
|
---|
| 28 | // or matches in the last dotted part of the object name
|
---|
| 29 | objPartialMatch: 6,
|
---|
| 30 | // Additive scores depending on the priority of the object
|
---|
| 31 | objPrio: {0: 15, // used to be importantResults
|
---|
| 32 | 1: 5, // used to be objectResults
|
---|
| 33 | 2: -5}, // used to be unimportantResults
|
---|
| 34 | // Used when the priority is not in the mapping.
|
---|
| 35 | objPrioDefault: 0,
|
---|
| 36 |
|
---|
| 37 | // query found in title
|
---|
| 38 | title: 15,
|
---|
| 39 | // query found in terms
|
---|
| 40 | term: 5
|
---|
| 41 | };
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | if (!splitQuery) {
|
---|
| 45 | function splitQuery(query) {
|
---|
| 46 | return query.split(/\s+/);
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
| 51 | * Search Module
|
---|
| 52 | */
|
---|
| 53 | var Search = {
|
---|
| 54 |
|
---|
| 55 | _index : null,
|
---|
| 56 | _queued_query : null,
|
---|
| 57 | _pulse_status : -1,
|
---|
| 58 |
|
---|
| 59 | init : function() {
|
---|
| 60 | var params = $.getQueryParameters();
|
---|
| 61 | if (params.q) {
|
---|
| 62 | var query = params.q[0];
|
---|
| 63 | $('input[name="q"]')[0].value = query;
|
---|
| 64 | this.performSearch(query);
|
---|
| 65 | }
|
---|
| 66 | },
|
---|
| 67 |
|
---|
| 68 | loadIndex : function(url) {
|
---|
| 69 | $.ajax({type: "GET", url: url, data: null,
|
---|
| 70 | dataType: "script", cache: true,
|
---|
| 71 | complete: function(jqxhr, textstatus) {
|
---|
| 72 | if (textstatus != "success") {
|
---|
| 73 | document.getElementById("searchindexloader").src = url;
|
---|
| 74 | }
|
---|
| 75 | }});
|
---|
| 76 | },
|
---|
| 77 |
|
---|
| 78 | setIndex : function(index) {
|
---|
| 79 | var q;
|
---|
| 80 | this._index = index;
|
---|
| 81 | if ((q = this._queued_query) !== null) {
|
---|
| 82 | this._queued_query = null;
|
---|
| 83 | Search.query(q);
|
---|
| 84 | }
|
---|
| 85 | },
|
---|
| 86 |
|
---|
| 87 | hasIndex : function() {
|
---|
| 88 | return this._index !== null;
|
---|
| 89 | },
|
---|
| 90 |
|
---|
| 91 | deferQuery : function(query) {
|
---|
| 92 | this._queued_query = query;
|
---|
| 93 | },
|
---|
| 94 |
|
---|
| 95 | stopPulse : function() {
|
---|
| 96 | this._pulse_status = 0;
|
---|
| 97 | },
|
---|
| 98 |
|
---|
| 99 | startPulse : function() {
|
---|
| 100 | if (this._pulse_status >= 0)
|
---|
| 101 | return;
|
---|
| 102 | function pulse() {
|
---|
| 103 | var i;
|
---|
| 104 | Search._pulse_status = (Search._pulse_status + 1) % 4;
|
---|
| 105 | var dotString = '';
|
---|
| 106 | for (i = 0; i < Search._pulse_status; i++)
|
---|
| 107 | dotString += '.';
|
---|
| 108 | Search.dots.text(dotString);
|
---|
| 109 | if (Search._pulse_status > -1)
|
---|
| 110 | window.setTimeout(pulse, 500);
|
---|
| 111 | }
|
---|
| 112 | pulse();
|
---|
| 113 | },
|
---|
| 114 |
|
---|
| 115 | /**
|
---|
| 116 | * perform a search for something (or wait until index is loaded)
|
---|
| 117 | */
|
---|
| 118 | performSearch : function(query) {
|
---|
| 119 | // create the required interface elements
|
---|
| 120 | this.out = $('#search-results');
|
---|
| 121 | this.title = $('<h2>' + _('Searching') + '</h2>').appendTo(this.out);
|
---|
| 122 | this.dots = $('<span></span>').appendTo(this.title);
|
---|
| 123 | this.status = $('<p style="display: none"></p>').appendTo(this.out);
|
---|
| 124 | this.output = $('<ul class="search"/>').appendTo(this.out);
|
---|
| 125 |
|
---|
| 126 | $('#search-progress').text(_('Preparing search...'));
|
---|
| 127 | this.startPulse();
|
---|
| 128 |
|
---|
| 129 | // index already loaded, the browser was quick!
|
---|
| 130 | if (this.hasIndex())
|
---|
| 131 | this.query(query);
|
---|
| 132 | else
|
---|
| 133 | this.deferQuery(query);
|
---|
| 134 | },
|
---|
| 135 |
|
---|
| 136 | /**
|
---|
| 137 | * execute search (requires search index to be loaded)
|
---|
| 138 | */
|
---|
| 139 | query : function(query) {
|
---|
| 140 | var i;
|
---|
| 141 |
|
---|
| 142 | // stem the searchterms and add them to the correct list
|
---|
| 143 | var stemmer = new Stemmer();
|
---|
| 144 | var searchterms = [];
|
---|
| 145 | var excluded = [];
|
---|
| 146 | var hlterms = [];
|
---|
| 147 | var tmp = splitQuery(query);
|
---|
| 148 | var objectterms = [];
|
---|
| 149 | for (i = 0; i < tmp.length; i++) {
|
---|
| 150 | if (tmp[i] !== "") {
|
---|
| 151 | objectterms.push(tmp[i].toLowerCase());
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | if ($u.indexOf(stopwords, tmp[i].toLowerCase()) != -1 || tmp[i].match(/^\d+$/) ||
|
---|
| 155 | tmp[i] === "") {
|
---|
| 156 | // skip this "word"
|
---|
| 157 | continue;
|
---|
| 158 | }
|
---|
| 159 | // stem the word
|
---|
| 160 | var word = stemmer.stemWord(tmp[i].toLowerCase());
|
---|
| 161 | // prevent stemmer from cutting word smaller than two chars
|
---|
| 162 | if(word.length < 3 && tmp[i].length >= 3) {
|
---|
| 163 | word = tmp[i];
|
---|
| 164 | }
|
---|
| 165 | var toAppend;
|
---|
| 166 | // select the correct list
|
---|
| 167 | if (word[0] == '-') {
|
---|
| 168 | toAppend = excluded;
|
---|
| 169 | word = word.substr(1);
|
---|
| 170 | }
|
---|
| 171 | else {
|
---|
| 172 | toAppend = searchterms;
|
---|
| 173 | hlterms.push(tmp[i].toLowerCase());
|
---|
| 174 | }
|
---|
| 175 | // only add if not already in the list
|
---|
| 176 | if (!$u.contains(toAppend, word))
|
---|
| 177 | toAppend.push(word);
|
---|
| 178 | }
|
---|
| 179 | var highlightstring = '?highlight=' + $.urlencode(hlterms.join(" "));
|
---|
| 180 |
|
---|
| 181 | // console.debug('SEARCH: searching for:');
|
---|
| 182 | // console.info('required: ', searchterms);
|
---|
| 183 | // console.info('excluded: ', excluded);
|
---|
| 184 |
|
---|
| 185 | // prepare search
|
---|
| 186 | var terms = this._index.terms;
|
---|
| 187 | var titleterms = this._index.titleterms;
|
---|
| 188 |
|
---|
| 189 | // array of [filename, title, anchor, descr, score]
|
---|
| 190 | var results = [];
|
---|
| 191 | $('#search-progress').empty();
|
---|
| 192 |
|
---|
| 193 | // lookup as object
|
---|
| 194 | for (i = 0; i < objectterms.length; i++) {
|
---|
| 195 | var others = [].concat(objectterms.slice(0, i),
|
---|
| 196 | objectterms.slice(i+1, objectterms.length));
|
---|
| 197 | results = results.concat(this.performObjectSearch(objectterms[i], others));
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | // lookup as search terms in fulltext
|
---|
| 201 | results = results.concat(this.performTermsSearch(searchterms, excluded, terms, titleterms));
|
---|
| 202 |
|
---|
| 203 | // let the scorer override scores with a custom scoring function
|
---|
| 204 | if (Scorer.score) {
|
---|
| 205 | for (i = 0; i < results.length; i++)
|
---|
| 206 | results[i][4] = Scorer.score(results[i]);
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | // now sort the results by score (in opposite order of appearance, since the
|
---|
| 210 | // display function below uses pop() to retrieve items) and then
|
---|
| 211 | // alphabetically
|
---|
| 212 | results.sort(function(a, b) {
|
---|
| 213 | var left = a[4];
|
---|
| 214 | var right = b[4];
|
---|
| 215 | if (left > right) {
|
---|
| 216 | return 1;
|
---|
| 217 | } else if (left < right) {
|
---|
| 218 | return -1;
|
---|
| 219 | } else {
|
---|
| 220 | // same score: sort alphabetically
|
---|
| 221 | left = a[1].toLowerCase();
|
---|
| 222 | right = b[1].toLowerCase();
|
---|
| 223 | return (left > right) ? -1 : ((left < right) ? 1 : 0);
|
---|
| 224 | }
|
---|
| 225 | });
|
---|
| 226 |
|
---|
| 227 | // for debugging
|
---|
| 228 | //Search.lastresults = results.slice(); // a copy
|
---|
| 229 | //console.info('search results:', Search.lastresults);
|
---|
| 230 |
|
---|
| 231 | // print the results
|
---|
| 232 | var resultCount = results.length;
|
---|
| 233 | function displayNextItem() {
|
---|
| 234 | // results left, load the summary and display it
|
---|
| 235 | if (results.length) {
|
---|
| 236 | var item = results.pop();
|
---|
| 237 | var listItem = $('<li style="display:none"></li>');
|
---|
| 238 | if (DOCUMENTATION_OPTIONS.FILE_SUFFIX === '') {
|
---|
| 239 | // dirhtml builder
|
---|
| 240 | var dirname = item[0] + '/';
|
---|
| 241 | if (dirname.match(/\/index\/$/)) {
|
---|
| 242 | dirname = dirname.substring(0, dirname.length-6);
|
---|
| 243 | } else if (dirname == 'index/') {
|
---|
| 244 | dirname = '';
|
---|
| 245 | }
|
---|
| 246 | listItem.append($('<a/>').attr('href',
|
---|
| 247 | DOCUMENTATION_OPTIONS.URL_ROOT + dirname +
|
---|
| 248 | highlightstring + item[2]).html(item[1]));
|
---|
| 249 | } else {
|
---|
| 250 | // normal html builders
|
---|
| 251 | listItem.append($('<a/>').attr('href',
|
---|
| 252 | item[0] + DOCUMENTATION_OPTIONS.FILE_SUFFIX +
|
---|
| 253 | highlightstring + item[2]).html(item[1]));
|
---|
| 254 | }
|
---|
| 255 | if (item[3]) {
|
---|
| 256 | listItem.append($('<span> (' + item[3] + ')</span>'));
|
---|
| 257 | Search.output.append(listItem);
|
---|
| 258 | listItem.slideDown(5, function() {
|
---|
| 259 | displayNextItem();
|
---|
| 260 | });
|
---|
| 261 | } else if (DOCUMENTATION_OPTIONS.HAS_SOURCE) {
|
---|
| 262 | var suffix = DOCUMENTATION_OPTIONS.SOURCELINK_SUFFIX;
|
---|
| 263 | if (suffix === undefined) {
|
---|
| 264 | suffix = '.txt';
|
---|
| 265 | }
|
---|
| 266 | $.ajax({url: DOCUMENTATION_OPTIONS.URL_ROOT + '_sources/' + item[5] + (item[5].slice(-suffix.length) === suffix ? '' : suffix),
|
---|
| 267 | dataType: "text",
|
---|
| 268 | complete: function(jqxhr, textstatus) {
|
---|
| 269 | var data = jqxhr.responseText;
|
---|
| 270 | if (data !== '' && data !== undefined) {
|
---|
| 271 | listItem.append(Search.makeSearchSummary(data, searchterms, hlterms));
|
---|
| 272 | }
|
---|
| 273 | Search.output.append(listItem);
|
---|
| 274 | listItem.slideDown(5, function() {
|
---|
| 275 | displayNextItem();
|
---|
| 276 | });
|
---|
| 277 | }});
|
---|
| 278 | } else {
|
---|
| 279 | // no source available, just display title
|
---|
| 280 | Search.output.append(listItem);
|
---|
| 281 | listItem.slideDown(5, function() {
|
---|
| 282 | displayNextItem();
|
---|
| 283 | });
|
---|
| 284 | }
|
---|
| 285 | }
|
---|
| 286 | // search finished, update title and status message
|
---|
| 287 | else {
|
---|
| 288 | Search.stopPulse();
|
---|
| 289 | Search.title.text(_('Search Results'));
|
---|
| 290 | if (!resultCount)
|
---|
| 291 | 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.'));
|
---|
| 292 | else
|
---|
| 293 | Search.status.text(_('Search finished, found %s page(s) matching the search query.').replace('%s', resultCount));
|
---|
| 294 | Search.status.fadeIn(500);
|
---|
| 295 | }
|
---|
| 296 | }
|
---|
| 297 | displayNextItem();
|
---|
| 298 | },
|
---|
| 299 |
|
---|
| 300 | /**
|
---|
| 301 | * search for object names
|
---|
| 302 | */
|
---|
| 303 | performObjectSearch : function(object, otherterms) {
|
---|
| 304 | var filenames = this._index.filenames;
|
---|
| 305 | var docnames = this._index.docnames;
|
---|
| 306 | var objects = this._index.objects;
|
---|
| 307 | var objnames = this._index.objnames;
|
---|
| 308 | var titles = this._index.titles;
|
---|
| 309 |
|
---|
| 310 | var i;
|
---|
| 311 | var results = [];
|
---|
| 312 |
|
---|
| 313 | for (var prefix in objects) {
|
---|
| 314 | for (var name in objects[prefix]) {
|
---|
| 315 | var fullname = (prefix ? prefix + '.' : '') + name;
|
---|
| 316 | if (fullname.toLowerCase().indexOf(object) > -1) {
|
---|
| 317 | var score = 0;
|
---|
| 318 | var parts = fullname.split('.');
|
---|
| 319 | // check for different match types: exact matches of full name or
|
---|
| 320 | // "last name" (i.e. last dotted part)
|
---|
| 321 | if (fullname == object || parts[parts.length - 1] == object) {
|
---|
| 322 | score += Scorer.objNameMatch;
|
---|
| 323 | // matches in last name
|
---|
| 324 | } else if (parts[parts.length - 1].indexOf(object) > -1) {
|
---|
| 325 | score += Scorer.objPartialMatch;
|
---|
| 326 | }
|
---|
| 327 | var match = objects[prefix][name];
|
---|
| 328 | var objname = objnames[match[1]][2];
|
---|
| 329 | var title = titles[match[0]];
|
---|
| 330 | // If more than one term searched for, we require other words to be
|
---|
| 331 | // found in the name/title/description
|
---|
| 332 | if (otherterms.length > 0) {
|
---|
| 333 | var haystack = (prefix + ' ' + name + ' ' +
|
---|
| 334 | objname + ' ' + title).toLowerCase();
|
---|
| 335 | var allfound = true;
|
---|
| 336 | for (i = 0; i < otherterms.length; i++) {
|
---|
| 337 | if (haystack.indexOf(otherterms[i]) == -1) {
|
---|
| 338 | allfound = false;
|
---|
| 339 | break;
|
---|
| 340 | }
|
---|
| 341 | }
|
---|
| 342 | if (!allfound) {
|
---|
| 343 | continue;
|
---|
| 344 | }
|
---|
| 345 | }
|
---|
| 346 | var descr = objname + _(', in ') + title;
|
---|
| 347 |
|
---|
| 348 | var anchor = match[3];
|
---|
| 349 | if (anchor === '')
|
---|
| 350 | anchor = fullname;
|
---|
| 351 | else if (anchor == '-')
|
---|
| 352 | anchor = objnames[match[1]][1] + '-' + fullname;
|
---|
| 353 | // add custom score for some objects according to scorer
|
---|
| 354 | if (Scorer.objPrio.hasOwnProperty(match[2])) {
|
---|
| 355 | score += Scorer.objPrio[match[2]];
|
---|
| 356 | } else {
|
---|
| 357 | score += Scorer.objPrioDefault;
|
---|
| 358 | }
|
---|
| 359 | results.push([docnames[match[0]], fullname, '#'+anchor, descr, score, filenames[match[0]]]);
|
---|
| 360 | }
|
---|
| 361 | }
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | return results;
|
---|
| 365 | },
|
---|
| 366 |
|
---|
| 367 | /**
|
---|
| 368 | * search for full-text terms in the index
|
---|
| 369 | */
|
---|
| 370 | performTermsSearch : function(searchterms, excluded, terms, titleterms) {
|
---|
| 371 | var docnames = this._index.docnames;
|
---|
| 372 | var filenames = this._index.filenames;
|
---|
| 373 | var titles = this._index.titles;
|
---|
| 374 |
|
---|
| 375 | var i, j, file;
|
---|
| 376 | var fileMap = {};
|
---|
| 377 | var scoreMap = {};
|
---|
| 378 | var results = [];
|
---|
| 379 |
|
---|
| 380 | // perform the search on the required terms
|
---|
| 381 | for (i = 0; i < searchterms.length; i++) {
|
---|
| 382 | var word = searchterms[i];
|
---|
| 383 | var files = [];
|
---|
| 384 | var _o = [
|
---|
| 385 | {files: terms[word], score: Scorer.term},
|
---|
| 386 | {files: titleterms[word], score: Scorer.title}
|
---|
| 387 | ];
|
---|
| 388 |
|
---|
| 389 | // no match but word was a required one
|
---|
| 390 | if ($u.every(_o, function(o){return o.files === undefined;})) {
|
---|
| 391 | break;
|
---|
| 392 | }
|
---|
| 393 | // found search word in contents
|
---|
| 394 | $u.each(_o, function(o) {
|
---|
| 395 | var _files = o.files;
|
---|
| 396 | if (_files === undefined)
|
---|
| 397 | return
|
---|
| 398 |
|
---|
| 399 | if (_files.length === undefined)
|
---|
| 400 | _files = [_files];
|
---|
| 401 | files = files.concat(_files);
|
---|
| 402 |
|
---|
| 403 | // set score for the word in each file to Scorer.term
|
---|
| 404 | for (j = 0; j < _files.length; j++) {
|
---|
| 405 | file = _files[j];
|
---|
| 406 | if (!(file in scoreMap))
|
---|
| 407 | scoreMap[file] = {}
|
---|
| 408 | scoreMap[file][word] = o.score;
|
---|
| 409 | }
|
---|
| 410 | });
|
---|
| 411 |
|
---|
| 412 | // create the mapping
|
---|
| 413 | for (j = 0; j < files.length; j++) {
|
---|
| 414 | file = files[j];
|
---|
| 415 | if (file in fileMap)
|
---|
| 416 | fileMap[file].push(word);
|
---|
| 417 | else
|
---|
| 418 | fileMap[file] = [word];
|
---|
| 419 | }
|
---|
| 420 | }
|
---|
| 421 |
|
---|
| 422 | // now check if the files don't contain excluded terms
|
---|
| 423 | for (file in fileMap) {
|
---|
| 424 | var valid = true;
|
---|
| 425 |
|
---|
| 426 | // check if all requirements are matched
|
---|
| 427 | if (fileMap[file].length != searchterms.length)
|
---|
| 428 | continue;
|
---|
| 429 |
|
---|
| 430 | // ensure that none of the excluded terms is in the search result
|
---|
| 431 | for (i = 0; i < excluded.length; i++) {
|
---|
| 432 | if (terms[excluded[i]] == file ||
|
---|
| 433 | titleterms[excluded[i]] == file ||
|
---|
| 434 | $u.contains(terms[excluded[i]] || [], file) ||
|
---|
| 435 | $u.contains(titleterms[excluded[i]] || [], file)) {
|
---|
| 436 | valid = false;
|
---|
| 437 | break;
|
---|
| 438 | }
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 | // if we have still a valid result we can add it to the result list
|
---|
| 442 | if (valid) {
|
---|
| 443 | // select one (max) score for the file.
|
---|
| 444 | // for better ranking, we should calculate ranking by using words statistics like basic tf-idf...
|
---|
| 445 | var score = $u.max($u.map(fileMap[file], function(w){return scoreMap[file][w]}));
|
---|
| 446 | results.push([docnames[file], titles[file], '', null, score, filenames[file]]);
|
---|
| 447 | }
|
---|
| 448 | }
|
---|
| 449 | return results;
|
---|
| 450 | },
|
---|
| 451 |
|
---|
| 452 | /**
|
---|
| 453 | * helper function to return a node containing the
|
---|
| 454 | * search summary for a given text. keywords is a list
|
---|
| 455 | * of stemmed words, hlwords is the list of normal, unstemmed
|
---|
| 456 | * words. the first one is used to find the occurrence, the
|
---|
| 457 | * latter for highlighting it.
|
---|
| 458 | */
|
---|
| 459 | makeSearchSummary : function(text, keywords, hlwords) {
|
---|
| 460 | var textLower = text.toLowerCase();
|
---|
| 461 | var start = 0;
|
---|
| 462 | $.each(keywords, function() {
|
---|
| 463 | var i = textLower.indexOf(this.toLowerCase());
|
---|
| 464 | if (i > -1)
|
---|
| 465 | start = i;
|
---|
| 466 | });
|
---|
| 467 | start = Math.max(start - 120, 0);
|
---|
| 468 | var excerpt = ((start > 0) ? '...' : '') +
|
---|
| 469 | $.trim(text.substr(start, 240)) +
|
---|
| 470 | ((start + 240 - text.length) ? '...' : '');
|
---|
| 471 | var rv = $('<div class="context"></div>').text(excerpt);
|
---|
| 472 | $.each(hlwords, function() {
|
---|
| 473 | rv = rv.highlightText(this, 'highlighted');
|
---|
| 474 | });
|
---|
| 475 | return rv;
|
---|
| 476 | }
|
---|
| 477 | };
|
---|
| 478 |
|
---|
| 479 | $(document).ready(function() {
|
---|
| 480 | Search.init();
|
---|
| 481 | });
|
---|