searchtools.js 16 KB

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