Skip to content

Commit

Permalink
remove queryNorm computation
Browse files Browse the repository at this point in the history
  • Loading branch information
weixsong committed Feb 27, 2016
1 parent 9c3de45 commit 34a30ce
Showing 1 changed file with 4 additions and 28 deletions.
32 changes: 4 additions & 28 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,15 +334,13 @@ elasticlunr.Index.prototype.search = function (query, userConfig) {
var queryTokens = this.pipeline.run(elasticlunr.tokenizer(query));

var queryResults = {};
var squaredWeight = this.computeSquaredWeight(queryTokens, config);

for (var field in config) {
var fieldSearchResults = this.fieldSearch(queryTokens, field, config);
var fieldBoost = config[field].boost;
var queryNorm = 1 / Math.sqrt(1 / (fieldBoost * fieldBoost) * squaredWeight);

for (var docRef in fieldSearchResults) {
fieldSearchResults[docRef] = fieldSearchResults[docRef] * queryNorm;
fieldSearchResults[docRef] = fieldSearchResults[docRef] * fieldBoost;
}

for (var docRef in fieldSearchResults) {
Expand Down Expand Up @@ -389,9 +387,9 @@ elasticlunr.Index.prototype.fieldSearch = function (queryTokens, fieldName, conf
for (var docRef in docs) {
var tf = this.index[fieldName].getTermFrequency(key, docRef);
var fieldLength = this.documentStore.getFieldLength(docRef, fieldName);
var norm = 1;
var fieldLengthNorm = 1;
if (fieldLength != 0) {
norm = 1 / Math.sqrt(fieldLength);
fieldLengthNorm = 1 / Math.sqrt(fieldLength);
}

var penality = 1;
Expand All @@ -409,7 +407,7 @@ elasticlunr.Index.prototype.fieldSearch = function (queryTokens, fieldName, conf
this.fieldSearchStats(docTokens, key, docs);
}

var score = tf * idf * norm * penality;
var score = tf * idf * fieldLengthNorm * penality;

if (docRef in scores) {
scores[docRef] += score;
Expand Down Expand Up @@ -448,28 +446,6 @@ elasticlunr.Index.prototype.fieldSearchStats = function (docTokens, token, docs)
}
};

/**
* compute squared weight of query tokens.
*
* @param {Array} queryTokens query tokens.
* @param {elasticlunr.Configuration} config The user query config, JSON format.
* @return {Float}
*/
elasticlunr.Index.prototype.computeSquaredWeight = function (queryTokens, config) {
var weight = 0.0;
queryTokens.forEach(function (token) {
var fieldWeight = 0.0;
for (var field in config) {
var fieldBoost = config[field].boost;
var idf = this.idf(token, field);
fieldWeight += idf * idf * fieldBoost * fieldBoost;
}
weight += fieldWeight;
}, this);

return weight;
};

/**
* find documents contain all the query tokens.
* only for inner use.
Expand Down

0 comments on commit 34a30ce

Please sign in to comment.