Merge pull request #2121 from mocobeta/better_and_search

Fix performTermsSearch function for multiple words search.
This commit is contained in:
Takayuki SHIMIZUKAWA 2015-11-21 16:52:27 +09:00
commit f1d4eabfe7

View File

@ -195,8 +195,7 @@ var Search = {
}
// lookup as search terms in fulltext
results = results.concat(this.performTermsSearch(searchterms, excluded, terms, Scorer.term))
.concat(this.performTermsSearch(searchterms, excluded, titleterms, Scorer.title));
results = results.concat(this.performTermsSearch(searchterms, excluded, terms, titleterms));
// let the scorer override scores with a custom scoring function
if (Scorer.score) {
@ -360,23 +359,55 @@ var Search = {
/**
* search for full-text terms in the index
*/
performTermsSearch : function(searchterms, excluded, terms, score) {
performTermsSearch : function(searchterms, excluded, terms, titleterms) {
var filenames = this._index.filenames;
var titles = this._index.titles;
var i, j, file, files;
var fileMap = {};
var scoreMap = {};
var results = [];
// perform the search on the required terms
for (i = 0; i < searchterms.length; i++) {
var word = searchterms[i];
files = [];
_files1 = terms[word];
_files2 = titleterms[word];
// no match but word was a required one
if ((files = terms[word]) === undefined)
if (_files1 === undefined && _files2 === undefined) {
break;
if (files.length === undefined) {
files = [files];
}
// found search word in contents
if (_files1 !== undefined) {
if (_files1.length === undefined)
_files1 = [_files1];
files = files.concat(_files1);
// set score for the word in each file to Scorer.term
for (j = 0; j < _files1.length; j++) {
file = _files1[j];
if (!(file in scoreMap))
scoreMap[file] = {}
scoreMap[file][word] = Scorer.term;
}
}
if (_files2 !== undefined) {
// found the word in document title
if (_files2.length === undefined)
_files2 = [_files2];
files = files.concat(_files2);
// set score for the word in each file to Scorer.title
for (j = 0; j < _files2.length; j++) {
file = _files2[j];
if (!(file in scoreMap))
scoreMap[file] = {}
scoreMap[file][word] = Scorer.title;
}
}
// create the mapping
for (j = 0; j < files.length; j++) {
file = files[j];
@ -398,7 +429,9 @@ var Search = {
// ensure that none of the excluded terms is in the search result
for (i = 0; i < excluded.length; i++) {
if (terms[excluded[i]] == file ||
$u.contains(terms[excluded[i]] || [], file)) {
titleterms[excluded[i]] == file ||
$u.contains(terms[excluded[i]] || [], file) ||
$u.contains(titleterms[excluded[i]] || [], file)) {
valid = false;
break;
}
@ -406,6 +439,14 @@ var Search = {
// 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...
score = 0;
for (i = 0; i < fileMap[file].length; i++) {
w = fileMap[file][i];
if (score < scoreMap[file][w])
score = scoreMap[file][w]
}
results.push([filenames[file], titles[file], '', null, score]);
}
}