performTermsSearch : function()

in versions/1.2.1/_static/searchtools_custom.js [660:760]


  performTermsSearch : function(searchterms, excluded, terms, titleterms) {
    var filenames = this._index.filenames;
    var titles = this._index.titles;

    var i, j, file;
    var fileMap = {};
    var scoreMap = {};
    var results = [];
    var scoreDict = {};

    // perform the search on the required terms
    for (i = 0; i < searchterms.length; i++) {
      var word = searchterms[i];
      var files = [];
      var _o = [
        {files: terms[word], score: Scorer.term},
        {files: titleterms[word], score: Scorer.title}
      ];

      // no match but word was a required one
      if ($u.every(_o, function(o){return o.files === undefined;})) {
        break;
      }
      // found search word in contents
      $u.each(_o, function(o) {
        var _files = o.files;
        if (_files === undefined)
          return

        if (_files.length === undefined)
          _files = [_files];
        files = files.concat(_files);

        // set score for the word in each file to Scorer.term
        for (j = 0; j < _files.length; j++) {
          file = _files[j];
          if (!(file in scoreMap))
            scoreMap[file] = {}
          scoreMap[file][word] = o.score;
        }
      });

      // create the mapping
      for (j = 0; j < files.length; j++) {
        file = files[j];
        if (file in fileMap)
          fileMap[file].push(word);
        else
          fileMap[file] = [word];
      }
    }

    // now check if the files don't contain excluded terms
    for (file in fileMap) {
      var valid = true;

      // check if all requirements are matched
      if (fileMap[file].length != searchterms.length)
          continue;

      // ensure that none of the excluded terms is in the search result
      for (i = 0; i < excluded.length; i++) {
        if (terms[excluded[i]] == file ||
          titleterms[excluded[i]] == file ||
          $u.contains(terms[excluded[i]] || [], file) ||
          $u.contains(titleterms[excluded[i]] || [], file)) {
            valid = false;
            break;
        }
      }

      // if we have still a valid result we can add it to the result list
      if (valid) {
        // select one (max) score for the file.
        // for better ranking, we should calculate ranking by using words statistics like basic tf-idf...
        var score = $u.max($u.map(fileMap[file], function(w){return scoreMap[file][w]}));
        for(i = 0; i < searchterms.length; i++) {
          var word = searchterms[i];
          if(word == 'ndarrai') word = 'ndarray';
          if(titles[file].toLowerCase().indexOf(word.toLowerCase()) != -1) {
            score += Scorer.title;
          }
        }

        if(!(filenames[file] in scoreDict)) {
          scoreDict[filenames[file]] = {};
        }
        if(!(titles[file] in scoreDict[filenames[file]])) {
          scoreDict[filenames[file]][titles[file]] = 0;
        }
        scoreDict[filenames[file]][titles[file]] += score;
      }
    }

    for(var file in scoreDict) {
      for(var title in scoreDict[file]) {
        results.push([file, title, '', null, scoreDict[file][title]]);
      }
    }
    return results;
  },