searchtools.js 19 KB

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