searchtools.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. var stopwords = DOCUMENTATION_OPTIONS.SEARCH_LANGUAGE_STOP_WORDS;
  125. // stem the searchterms and add them to the correct list
  126. var stemmer = new Stemmer();
  127. var searchterms = [];
  128. var excluded = [];
  129. var hlterms = [];
  130. var tmp = splitQuery(query);
  131. var objectterms = [];
  132. for (i = 0; i < tmp.length; i++) {
  133. if (tmp[i] !== "") {
  134. objectterms.push(tmp[i].toLowerCase());
  135. }
  136. if ($u.indexOf(stopwords, tmp[i].toLowerCase()) != -1 || tmp[i].match(/^\d+$/) ||
  137. tmp[i] === "") {
  138. // skip this "word"
  139. continue;
  140. }
  141. // stem the word
  142. var word = stemmer.stemWord(tmp[i].toLowerCase());
  143. // prevent stemmer from cutting word smaller than two chars
  144. if(word.length < 3 && tmp[i].length >= 3) {
  145. word = tmp[i];
  146. }
  147. var toAppend;
  148. // select the correct list
  149. if (word[0] == '-') {
  150. toAppend = excluded;
  151. word = word.substr(1);
  152. }
  153. else {
  154. toAppend = searchterms;
  155. hlterms.push(tmp[i].toLowerCase());
  156. }
  157. // only add if not already in the list
  158. if (!$u.contains(toAppend, word))
  159. toAppend.push(word);
  160. }
  161. var highlightstring = '?highlight=' + $.urlencode(hlterms.join(" "));
  162. // console.debug('SEARCH: searching for:');
  163. // console.info('required: ', searchterms);
  164. // console.info('excluded: ', excluded);
  165. // prepare search
  166. var terms = this._index.terms;
  167. var titleterms = this._index.titleterms;
  168. // array of [filename, title, anchor, descr, score]
  169. var results = [];
  170. $('#search-progress').empty();
  171. // lookup as object
  172. for (i = 0; i < objectterms.length; i++) {
  173. var others = [].concat(objectterms.slice(0, i),
  174. objectterms.slice(i+1, objectterms.length));
  175. results = results.concat(this.performObjectSearch(objectterms[i], others));
  176. }
  177. // lookup as search terms in fulltext
  178. results = results.concat(this.performTermsSearch(searchterms, excluded, terms, titleterms));
  179. // let the scorer override scores with a custom scoring function
  180. if (Scorer.score) {
  181. for (i = 0; i < results.length; i++)
  182. results[i][4] = Scorer.score(results[i]);
  183. }
  184. // now sort the results by score (in opposite order of appearance, since the
  185. // display function below uses pop() to retrieve items) and then
  186. // alphabetically
  187. results.sort(function(a, b) {
  188. var left = a[4];
  189. var right = b[4];
  190. if (left > right) {
  191. return 1;
  192. } else if (left < right) {
  193. return -1;
  194. } else {
  195. // same score: sort alphabetically
  196. left = a[1].toLowerCase();
  197. right = b[1].toLowerCase();
  198. return (left > right) ? -1 : ((left < right) ? 1 : 0);
  199. }
  200. });
  201. // for debugging
  202. //Search.lastresults = results.slice(); // a copy
  203. //console.info('search results:', Search.lastresults);
  204. // print the results
  205. var resultCount = results.length;
  206. function displayNextItem() {
  207. // results left, load the summary and display it
  208. if (results.length) {
  209. var item = results.pop();
  210. var listItem = $('<li style="display:none"></li>');
  211. if (DOCUMENTATION_OPTIONS.FILE_SUFFIX === '') {
  212. // dirhtml builder
  213. var dirname = item[0] + '/';
  214. if (dirname.match(/\/index\/$/)) {
  215. dirname = dirname.substring(0, dirname.length-6);
  216. } else if (dirname == 'index/') {
  217. dirname = '';
  218. }
  219. listItem.append($('<a/>').attr('href',
  220. DOCUMENTATION_OPTIONS.URL_ROOT + dirname +
  221. highlightstring + item[2]).html(item[1]));
  222. } else {
  223. // normal html builders
  224. listItem.append($('<a/>').attr('href',
  225. item[0] + DOCUMENTATION_OPTIONS.FILE_SUFFIX +
  226. highlightstring + item[2]).html(item[1]));
  227. }
  228. if (item[3]) {
  229. listItem.append($('<span> (' + item[3] + ')</span>'));
  230. Search.output.append(listItem);
  231. listItem.slideDown(5, function() {
  232. displayNextItem();
  233. });
  234. } else if (DOCUMENTATION_OPTIONS.HAS_SOURCE) {
  235. var suffix = DOCUMENTATION_OPTIONS.SOURCELINK_SUFFIX;
  236. if (suffix === undefined) {
  237. suffix = '.txt';
  238. }
  239. $.ajax({url: DOCUMENTATION_OPTIONS.URL_ROOT + '_sources/' + item[5] + (item[5].slice(-suffix.length) === suffix ? '' : suffix),
  240. dataType: "text",
  241. complete: function(jqxhr, textstatus) {
  242. var data = jqxhr.responseText;
  243. if (data !== '' && data !== undefined) {
  244. listItem.append(Search.makeSearchSummary(data, searchterms, hlterms));
  245. }
  246. Search.output.append(listItem);
  247. listItem.slideDown(5, function() {
  248. displayNextItem();
  249. });
  250. }});
  251. } else {
  252. // no source available, just display title
  253. Search.output.append(listItem);
  254. listItem.slideDown(5, function() {
  255. displayNextItem();
  256. });
  257. }
  258. }
  259. // search finished, update title and status message
  260. else {
  261. Search.stopPulse();
  262. Search.title.text(_('Search Results'));
  263. if (!resultCount)
  264. 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.'));
  265. else
  266. Search.status.text(_('Search finished, found %s page(s) matching the search query.').replace('%s', resultCount));
  267. Search.status.fadeIn(500);
  268. }
  269. }
  270. displayNextItem();
  271. },
  272. /**
  273. * search for object names
  274. */
  275. performObjectSearch : function(object, otherterms) {
  276. var filenames = this._index.filenames;
  277. var docnames = this._index.docnames;
  278. var objects = this._index.objects;
  279. var objnames = this._index.objnames;
  280. var titles = this._index.titles;
  281. var i;
  282. var results = [];
  283. for (var prefix in objects) {
  284. for (var name in objects[prefix]) {
  285. var fullname = (prefix ? prefix + '.' : '') + name;
  286. if (fullname.toLowerCase().indexOf(object) > -1) {
  287. var score = 0;
  288. var parts = fullname.split('.');
  289. // check for different match types: exact matches of full name or
  290. // "last name" (i.e. last dotted part)
  291. if (fullname == object || parts[parts.length - 1] == object) {
  292. score += Scorer.objNameMatch;
  293. // matches in last name
  294. } else if (parts[parts.length - 1].indexOf(object) > -1) {
  295. score += Scorer.objPartialMatch;
  296. }
  297. var match = objects[prefix][name];
  298. var objname = objnames[match[1]][2];
  299. var title = titles[match[0]];
  300. // If more than one term searched for, we require other words to be
  301. // found in the name/title/description
  302. if (otherterms.length > 0) {
  303. var haystack = (prefix + ' ' + name + ' ' +
  304. objname + ' ' + title).toLowerCase();
  305. var allfound = true;
  306. for (i = 0; i < otherterms.length; i++) {
  307. if (haystack.indexOf(otherterms[i]) == -1) {
  308. allfound = false;
  309. break;
  310. }
  311. }
  312. if (!allfound) {
  313. continue;
  314. }
  315. }
  316. var descr = objname + _(', in ') + title;
  317. var anchor = match[3];
  318. if (anchor === '')
  319. anchor = fullname;
  320. else if (anchor == '-')
  321. anchor = objnames[match[1]][1] + '-' + fullname;
  322. // add custom score for some objects according to scorer
  323. if (Scorer.objPrio.hasOwnProperty(match[2])) {
  324. score += Scorer.objPrio[match[2]];
  325. } else {
  326. score += Scorer.objPrioDefault;
  327. }
  328. results.push([docnames[match[0]], fullname, '#'+anchor, descr, score, filenames[match[0]]]);
  329. }
  330. }
  331. }
  332. return results;
  333. },
  334. /**
  335. * search for full-text terms in the index
  336. */
  337. performTermsSearch : function(searchterms, excluded, terms, titleterms) {
  338. var docnames = this._index.docnames;
  339. var filenames = this._index.filenames;
  340. var titles = this._index.titles;
  341. var i, j, file;
  342. var fileMap = {};
  343. var scoreMap = {};
  344. var results = [];
  345. // perform the search on the required terms
  346. for (i = 0; i < searchterms.length; i++) {
  347. var word = searchterms[i];
  348. var files = [];
  349. var _o = [
  350. {files: terms[word], score: Scorer.term},
  351. {files: titleterms[word], score: Scorer.title}
  352. ];
  353. // no match but word was a required one
  354. if ($u.every(_o, function(o){return o.files === undefined;})) {
  355. break;
  356. }
  357. // found search word in contents
  358. $u.each(_o, function(o) {
  359. var _files = o.files;
  360. if (_files === undefined)
  361. return
  362. if (_files.length === undefined)
  363. _files = [_files];
  364. files = files.concat(_files);
  365. // set score for the word in each file to Scorer.term
  366. for (j = 0; j < _files.length; j++) {
  367. file = _files[j];
  368. if (!(file in scoreMap))
  369. scoreMap[file] = {}
  370. scoreMap[file][word] = o.score;
  371. }
  372. });
  373. // create the mapping
  374. for (j = 0; j < files.length; j++) {
  375. file = files[j];
  376. if (file in fileMap)
  377. fileMap[file].push(word);
  378. else
  379. fileMap[file] = [word];
  380. }
  381. }
  382. // now check if the files don't contain excluded terms
  383. for (file in fileMap) {
  384. var valid = true;
  385. // check if all requirements are matched
  386. if (fileMap[file].length != searchterms.length)
  387. continue;
  388. // ensure that none of the excluded terms is in the search result
  389. for (i = 0; i < excluded.length; i++) {
  390. if (terms[excluded[i]] == file ||
  391. titleterms[excluded[i]] == file ||
  392. $u.contains(terms[excluded[i]] || [], file) ||
  393. $u.contains(titleterms[excluded[i]] || [], file)) {
  394. valid = false;
  395. break;
  396. }
  397. }
  398. // if we have still a valid result we can add it to the result list
  399. if (valid) {
  400. // select one (max) score for the file.
  401. // for better ranking, we should calculate ranking by using words statistics like basic tf-idf...
  402. var score = $u.max($u.map(fileMap[file], function(w){return scoreMap[file][w]}));
  403. results.push([docnames[file], titles[file], '', null, score, filenames[file]]);
  404. }
  405. }
  406. return results;
  407. },
  408. /**
  409. * helper function to return a node containing the
  410. * search summary for a given text. keywords is a list
  411. * of stemmed words, hlwords is the list of normal, unstemmed
  412. * words. the first one is used to find the occurrence, the
  413. * latter for highlighting it.
  414. */
  415. makeSearchSummary : function(text, keywords, hlwords) {
  416. var textLower = text.toLowerCase();
  417. var start = 0;
  418. $.each(keywords, function() {
  419. var i = textLower.indexOf(this.toLowerCase());
  420. if (i > -1)
  421. start = i;
  422. });
  423. start = Math.max(start - 120, 0);
  424. var excerpt = ((start > 0) ? '...' : '') +
  425. $.trim(text.substr(start, 240)) +
  426. ((start + 240 - text.length) ? '...' : '');
  427. var rv = $('<div class="context"></div>').text(excerpt);
  428. $.each(hlwords, function() {
  429. rv = rv.highlightText(this, 'highlighted');
  430. });
  431. return rv;
  432. }
  433. };
  434. $(document).ready(function() {
  435. Search.init();
  436. });