Skip to content

Commit

Permalink
feat(scoring): field length norm weight
Browse files Browse the repository at this point in the history
  • Loading branch information
krisk committed Dec 22, 2021
1 parent 721e4f4 commit a9e0080
Show file tree
Hide file tree
Showing 11 changed files with 236 additions and 99 deletions.
49 changes: 32 additions & 17 deletions dist/fuse.basic.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,15 +363,18 @@ var AdvancedOptions = {
// When `true`, the calculation for the relevance score (used for sorting) will
// ignore the field-length norm.
// More info: https://fusejs.io/concepts/scoring-theory.html#field-length-norm
ignoreFieldNorm: false
ignoreFieldNorm: false,
// The weight to determine how much field length norm effects scoring.
fieldNormWeight: 1
};
var Config = _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({}, BasicOptions), MatchOptions), FuzzyOptions), AdvancedOptions);

var SPACE = /[^ ]+/g; // Field-length norm: the shorter the field, the higher the weight.
// Set to 3 decimals to reduce index size.

function norm() {
var mantissa = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 3;
var weight = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
var mantissa = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 3;
var cache = new Map();
var m = Math.pow(10, mantissa);
return {
Expand All @@ -380,9 +383,10 @@ function norm() {

if (cache.has(numTokens)) {
return cache.get(numTokens);
}
} // Default function is 1/sqrt(x), weight makes that variable


var norm = 1 / Math.sqrt(numTokens); // In place of `toFixed(mantissa)`, for faster computation
var norm = 1 / Math.pow(numTokens, 0.5 * weight); // In place of `toFixed(mantissa)`, for faster computation

var n = parseFloat(Math.round(norm * m) / m);
cache.set(numTokens, n);
Expand All @@ -398,11 +402,13 @@ var FuseIndex = /*#__PURE__*/function () {
function FuseIndex() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref$getFn = _ref.getFn,
getFn = _ref$getFn === void 0 ? Config.getFn : _ref$getFn;
getFn = _ref$getFn === void 0 ? Config.getFn : _ref$getFn,
_ref$fieldNormWeight = _ref.fieldNormWeight,
fieldNormWeight = _ref$fieldNormWeight === void 0 ? Config.fieldNormWeight : _ref$fieldNormWeight;

_classCallCheck(this, FuseIndex);

this.norm = norm(3);
this.norm = norm(fieldNormWeight, 3);
this.getFn = getFn;
this.isCreated = false;
this.setIndexRecords();
Expand Down Expand Up @@ -581,10 +587,13 @@ var FuseIndex = /*#__PURE__*/function () {
function createIndex(keys, docs) {
var _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
_ref2$getFn = _ref2.getFn,
getFn = _ref2$getFn === void 0 ? Config.getFn : _ref2$getFn;
getFn = _ref2$getFn === void 0 ? Config.getFn : _ref2$getFn,
_ref2$fieldNormWeight = _ref2.fieldNormWeight,
fieldNormWeight = _ref2$fieldNormWeight === void 0 ? Config.fieldNormWeight : _ref2$fieldNormWeight;

var myIndex = new FuseIndex({
getFn: getFn
getFn: getFn,
fieldNormWeight: fieldNormWeight
});
myIndex.setKeys(keys.map(createKey));
myIndex.setSources(docs);
Expand All @@ -594,12 +603,15 @@ function createIndex(keys, docs) {
function parseIndex(data) {
var _ref3 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
_ref3$getFn = _ref3.getFn,
getFn = _ref3$getFn === void 0 ? Config.getFn : _ref3$getFn;
getFn = _ref3$getFn === void 0 ? Config.getFn : _ref3$getFn,
_ref3$fieldNormWeight = _ref3.fieldNormWeight,
fieldNormWeight = _ref3$fieldNormWeight === void 0 ? Config.fieldNormWeight : _ref3$fieldNormWeight;

var keys = data.keys,
records = data.records;
var myIndex = new FuseIndex({
getFn: getFn
getFn: getFn,
fieldNormWeight: fieldNormWeight
});
myIndex.setKeys(keys);
myIndex.setIndexRecords(records);
Expand Down Expand Up @@ -1182,7 +1194,7 @@ function format(results, docs) {
});
}

var Fuse = /*#__PURE__*/function () {
var Fuse$1 = /*#__PURE__*/function () {
function Fuse(docs) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var index = arguments.length > 2 ? arguments[2] : undefined;
Expand All @@ -1209,7 +1221,8 @@ var Fuse = /*#__PURE__*/function () {
}

this._myIndex = index || createIndex(this.options.keys, this._docs, {
getFn: this.options.getFn
getFn: this.options.getFn,
fieldNormWeight: this.options.fieldNormWeight
});
}
}, {
Expand Down Expand Up @@ -1438,13 +1451,15 @@ var Fuse = /*#__PURE__*/function () {
return Fuse;
}();

Fuse.version = '6.4.6';
Fuse.createIndex = createIndex;
Fuse.parseIndex = parseIndex;
Fuse.config = Config;
Fuse$1.version = '6.4.6';
Fuse$1.createIndex = createIndex;
Fuse$1.parseIndex = parseIndex;
Fuse$1.config = Config;

{
Fuse.parseQuery = parse;
Fuse$1.parseQuery = parse;
}

var Fuse = Fuse$1;

module.exports = Fuse;
24 changes: 14 additions & 10 deletions dist/fuse.basic.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,9 @@ const AdvancedOptions = {
// When `true`, the calculation for the relevance score (used for sorting) will
// ignore the field-length norm.
// More info: https://fusejs.io/concepts/scoring-theory.html#field-length-norm
ignoreFieldNorm: false
ignoreFieldNorm: false,
// The weight to determine how much field length norm effects scoring.
fieldNormWeight: 1
};

var Config = {
Expand All @@ -280,7 +282,7 @@ const SPACE = /[^ ]+/g;

// Field-length norm: the shorter the field, the higher the weight.
// Set to 3 decimals to reduce index size.
function norm(mantissa = 3) {
function norm(weight = 1, mantissa = 3) {
const cache = new Map();
const m = Math.pow(10, mantissa);

Expand All @@ -292,7 +294,8 @@ function norm(mantissa = 3) {
return cache.get(numTokens)
}

const norm = 1 / Math.sqrt(numTokens);
// Default function is 1/sqrt(x), weight makes that variable
const norm = 1 / Math.pow(numTokens, 0.5 * weight);

// In place of `toFixed(mantissa)`, for faster computation
const n = parseFloat(Math.round(norm * m) / m);
Expand All @@ -308,8 +311,8 @@ function norm(mantissa = 3) {
}

class FuseIndex {
constructor({ getFn = Config.getFn } = {}) {
this.norm = norm(3);
constructor({ getFn = Config.getFn, fieldNormWeight = Config.fieldNormWeight } = {}) {
this.norm = norm(fieldNormWeight, 3);
this.getFn = getFn;
this.isCreated = false;

Expand Down Expand Up @@ -448,17 +451,17 @@ class FuseIndex {
}
}

function createIndex(keys, docs, { getFn = Config.getFn } = {}) {
const myIndex = new FuseIndex({ getFn });
function createIndex(keys, docs, { getFn = Config.getFn, fieldNormWeight = Config.fieldNormWeight } = {}) {
const myIndex = new FuseIndex({ getFn, fieldNormWeight });
myIndex.setKeys(keys.map(createKey));
myIndex.setSources(docs);
myIndex.create();
return myIndex
}

function parseIndex(data, { getFn = Config.getFn } = {}) {
function parseIndex(data, { getFn = Config.getFn, fieldNormWeight = Config.fieldNormWeight } = {}) {
const { keys, records } = data;
const myIndex = new FuseIndex({ getFn });
const myIndex = new FuseIndex({ getFn, fieldNormWeight });
myIndex.setKeys(keys);
myIndex.setIndexRecords(records);
return myIndex
Expand Down Expand Up @@ -1058,7 +1061,8 @@ class Fuse {
this._myIndex =
index ||
createIndex(this.options.keys, this._docs, {
getFn: this.options.getFn
getFn: this.options.getFn,
fieldNormWeight: this.options.fieldNormWeight
});
}

Expand Down
49 changes: 32 additions & 17 deletions dist/fuse.basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,15 +367,18 @@
// When `true`, the calculation for the relevance score (used for sorting) will
// ignore the field-length norm.
// More info: https://fusejs.io/concepts/scoring-theory.html#field-length-norm
ignoreFieldNorm: false
ignoreFieldNorm: false,
// The weight to determine how much field length norm effects scoring.
fieldNormWeight: 1
};
var Config = _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({}, BasicOptions), MatchOptions), FuzzyOptions), AdvancedOptions);

var SPACE = /[^ ]+/g; // Field-length norm: the shorter the field, the higher the weight.
// Set to 3 decimals to reduce index size.

function norm() {
var mantissa = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 3;
var weight = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
var mantissa = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 3;
var cache = new Map();
var m = Math.pow(10, mantissa);
return {
Expand All @@ -384,9 +387,10 @@

if (cache.has(numTokens)) {
return cache.get(numTokens);
}
} // Default function is 1/sqrt(x), weight makes that variable


var norm = 1 / Math.sqrt(numTokens); // In place of `toFixed(mantissa)`, for faster computation
var norm = 1 / Math.pow(numTokens, 0.5 * weight); // In place of `toFixed(mantissa)`, for faster computation

var n = parseFloat(Math.round(norm * m) / m);
cache.set(numTokens, n);
Expand All @@ -402,11 +406,13 @@
function FuseIndex() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref$getFn = _ref.getFn,
getFn = _ref$getFn === void 0 ? Config.getFn : _ref$getFn;
getFn = _ref$getFn === void 0 ? Config.getFn : _ref$getFn,
_ref$fieldNormWeight = _ref.fieldNormWeight,
fieldNormWeight = _ref$fieldNormWeight === void 0 ? Config.fieldNormWeight : _ref$fieldNormWeight;

_classCallCheck(this, FuseIndex);

this.norm = norm(3);
this.norm = norm(fieldNormWeight, 3);
this.getFn = getFn;
this.isCreated = false;
this.setIndexRecords();
Expand Down Expand Up @@ -585,10 +591,13 @@
function createIndex(keys, docs) {
var _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
_ref2$getFn = _ref2.getFn,
getFn = _ref2$getFn === void 0 ? Config.getFn : _ref2$getFn;
getFn = _ref2$getFn === void 0 ? Config.getFn : _ref2$getFn,
_ref2$fieldNormWeight = _ref2.fieldNormWeight,
fieldNormWeight = _ref2$fieldNormWeight === void 0 ? Config.fieldNormWeight : _ref2$fieldNormWeight;

var myIndex = new FuseIndex({
getFn: getFn
getFn: getFn,
fieldNormWeight: fieldNormWeight
});
myIndex.setKeys(keys.map(createKey));
myIndex.setSources(docs);
Expand All @@ -598,12 +607,15 @@
function parseIndex(data) {
var _ref3 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
_ref3$getFn = _ref3.getFn,
getFn = _ref3$getFn === void 0 ? Config.getFn : _ref3$getFn;
getFn = _ref3$getFn === void 0 ? Config.getFn : _ref3$getFn,
_ref3$fieldNormWeight = _ref3.fieldNormWeight,
fieldNormWeight = _ref3$fieldNormWeight === void 0 ? Config.fieldNormWeight : _ref3$fieldNormWeight;

var keys = data.keys,
records = data.records;
var myIndex = new FuseIndex({
getFn: getFn
getFn: getFn,
fieldNormWeight: fieldNormWeight
});
myIndex.setKeys(keys);
myIndex.setIndexRecords(records);
Expand Down Expand Up @@ -1186,7 +1198,7 @@
});
}

var Fuse = /*#__PURE__*/function () {
var Fuse$1 = /*#__PURE__*/function () {
function Fuse(docs) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var index = arguments.length > 2 ? arguments[2] : undefined;
Expand All @@ -1213,7 +1225,8 @@
}

this._myIndex = index || createIndex(this.options.keys, this._docs, {
getFn: this.options.getFn
getFn: this.options.getFn,
fieldNormWeight: this.options.fieldNormWeight
});
}
}, {
Expand Down Expand Up @@ -1442,15 +1455,17 @@
return Fuse;
}();

Fuse.version = '6.4.6';
Fuse.createIndex = createIndex;
Fuse.parseIndex = parseIndex;
Fuse.config = Config;
Fuse$1.version = '6.4.6';
Fuse$1.createIndex = createIndex;
Fuse$1.parseIndex = parseIndex;
Fuse$1.config = Config;

{
Fuse.parseQuery = parse;
Fuse$1.parseQuery = parse;
}

var Fuse = Fuse$1;

return Fuse;

}));
Loading

0 comments on commit a9e0080

Please sign in to comment.