mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Fix performTermsSearch function for multiple words search.
This commit is contained in:
parent
f21a3039a0
commit
e3b3cda0cb
@ -195,8 +195,9 @@ 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, 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,6 +361,7 @@ var Search = {
|
||||
/**
|
||||
* search for full-text terms in the index
|
||||
*/
|
||||
/*
|
||||
performTermsSearch : function(searchterms, excluded, terms, score) {
|
||||
var filenames = this._index.filenames;
|
||||
var titles = this._index.titles;
|
||||
@ -411,6 +413,104 @@ var Search = {
|
||||
}
|
||||
return results;
|
||||
},
|
||||
*/
|
||||
|
||||
/**
|
||||
* search for full-text terms in the index
|
||||
*/
|
||||
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 (_files1 === undefined && _files2 === undefined) {
|
||||
break;
|
||||
}
|
||||
// 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];
|
||||
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...
|
||||
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]);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
},
|
||||
|
||||
/**
|
||||
* helper function to return a node containing the
|
||||
|
Loading…
Reference in New Issue
Block a user