searchtools.js 15 KB

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