Skip to content

Commit a9e0080

Browse files
committed
feat(scoring): field length norm weight
1 parent 721e4f4 commit a9e0080

11 files changed

Lines changed: 236 additions & 99 deletions

File tree

dist/fuse.basic.common.js

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -363,15 +363,18 @@ var AdvancedOptions = {
363363
// When `true`, the calculation for the relevance score (used for sorting) will
364364
// ignore the field-length norm.
365365
// More info: https://fusejs.io/concepts/scoring-theory.html#field-length-norm
366-
ignoreFieldNorm: false
366+
ignoreFieldNorm: false,
367+
// The weight to determine how much field length norm effects scoring.
368+
fieldNormWeight: 1
367369
};
368370
var Config = _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({}, BasicOptions), MatchOptions), FuzzyOptions), AdvancedOptions);
369371

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

373375
function norm() {
374-
var mantissa = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 3;
376+
var weight = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
377+
var mantissa = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 3;
375378
var cache = new Map();
376379
var m = Math.pow(10, mantissa);
377380
return {
@@ -380,9 +383,10 @@ function norm() {
380383

381384
if (cache.has(numTokens)) {
382385
return cache.get(numTokens);
383-
}
386+
} // Default function is 1/sqrt(x), weight makes that variable
387+
384388

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

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

403409
_classCallCheck(this, FuseIndex);
404410

405-
this.norm = norm(3);
411+
this.norm = norm(fieldNormWeight, 3);
406412
this.getFn = getFn;
407413
this.isCreated = false;
408414
this.setIndexRecords();
@@ -581,10 +587,13 @@ var FuseIndex = /*#__PURE__*/function () {
581587
function createIndex(keys, docs) {
582588
var _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
583589
_ref2$getFn = _ref2.getFn,
584-
getFn = _ref2$getFn === void 0 ? Config.getFn : _ref2$getFn;
590+
getFn = _ref2$getFn === void 0 ? Config.getFn : _ref2$getFn,
591+
_ref2$fieldNormWeight = _ref2.fieldNormWeight,
592+
fieldNormWeight = _ref2$fieldNormWeight === void 0 ? Config.fieldNormWeight : _ref2$fieldNormWeight;
585593

586594
var myIndex = new FuseIndex({
587-
getFn: getFn
595+
getFn: getFn,
596+
fieldNormWeight: fieldNormWeight
588597
});
589598
myIndex.setKeys(keys.map(createKey));
590599
myIndex.setSources(docs);
@@ -594,12 +603,15 @@ function createIndex(keys, docs) {
594603
function parseIndex(data) {
595604
var _ref3 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
596605
_ref3$getFn = _ref3.getFn,
597-
getFn = _ref3$getFn === void 0 ? Config.getFn : _ref3$getFn;
606+
getFn = _ref3$getFn === void 0 ? Config.getFn : _ref3$getFn,
607+
_ref3$fieldNormWeight = _ref3.fieldNormWeight,
608+
fieldNormWeight = _ref3$fieldNormWeight === void 0 ? Config.fieldNormWeight : _ref3$fieldNormWeight;
598609

599610
var keys = data.keys,
600611
records = data.records;
601612
var myIndex = new FuseIndex({
602-
getFn: getFn
613+
getFn: getFn,
614+
fieldNormWeight: fieldNormWeight
603615
});
604616
myIndex.setKeys(keys);
605617
myIndex.setIndexRecords(records);
@@ -1182,7 +1194,7 @@ function format(results, docs) {
11821194
});
11831195
}
11841196

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

12111223
this._myIndex = index || createIndex(this.options.keys, this._docs, {
1212-
getFn: this.options.getFn
1224+
getFn: this.options.getFn,
1225+
fieldNormWeight: this.options.fieldNormWeight
12131226
});
12141227
}
12151228
}, {
@@ -1438,13 +1451,15 @@ var Fuse = /*#__PURE__*/function () {
14381451
return Fuse;
14391452
}();
14401453

1441-
Fuse.version = '6.4.6';
1442-
Fuse.createIndex = createIndex;
1443-
Fuse.parseIndex = parseIndex;
1444-
Fuse.config = Config;
1454+
Fuse$1.version = '6.4.6';
1455+
Fuse$1.createIndex = createIndex;
1456+
Fuse$1.parseIndex = parseIndex;
1457+
Fuse$1.config = Config;
14451458

14461459
{
1447-
Fuse.parseQuery = parse;
1460+
Fuse$1.parseQuery = parse;
14481461
}
14491462

1463+
var Fuse = Fuse$1;
1464+
14501465
module.exports = Fuse;

dist/fuse.basic.esm.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,9 @@ const AdvancedOptions = {
266266
// When `true`, the calculation for the relevance score (used for sorting) will
267267
// ignore the field-length norm.
268268
// More info: https://fusejs.io/concepts/scoring-theory.html#field-length-norm
269-
ignoreFieldNorm: false
269+
ignoreFieldNorm: false,
270+
// The weight to determine how much field length norm effects scoring.
271+
fieldNormWeight: 1
270272
};
271273

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

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

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

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

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

310313
class FuseIndex {
311-
constructor({ getFn = Config.getFn } = {}) {
312-
this.norm = norm(3);
314+
constructor({ getFn = Config.getFn, fieldNormWeight = Config.fieldNormWeight } = {}) {
315+
this.norm = norm(fieldNormWeight, 3);
313316
this.getFn = getFn;
314317
this.isCreated = false;
315318

@@ -448,17 +451,17 @@ class FuseIndex {
448451
}
449452
}
450453

451-
function createIndex(keys, docs, { getFn = Config.getFn } = {}) {
452-
const myIndex = new FuseIndex({ getFn });
454+
function createIndex(keys, docs, { getFn = Config.getFn, fieldNormWeight = Config.fieldNormWeight } = {}) {
455+
const myIndex = new FuseIndex({ getFn, fieldNormWeight });
453456
myIndex.setKeys(keys.map(createKey));
454457
myIndex.setSources(docs);
455458
myIndex.create();
456459
return myIndex
457460
}
458461

459-
function parseIndex(data, { getFn = Config.getFn } = {}) {
462+
function parseIndex(data, { getFn = Config.getFn, fieldNormWeight = Config.fieldNormWeight } = {}) {
460463
const { keys, records } = data;
461-
const myIndex = new FuseIndex({ getFn });
464+
const myIndex = new FuseIndex({ getFn, fieldNormWeight });
462465
myIndex.setKeys(keys);
463466
myIndex.setIndexRecords(records);
464467
return myIndex
@@ -1058,7 +1061,8 @@ class Fuse {
10581061
this._myIndex =
10591062
index ||
10601063
createIndex(this.options.keys, this._docs, {
1061-
getFn: this.options.getFn
1064+
getFn: this.options.getFn,
1065+
fieldNormWeight: this.options.fieldNormWeight
10621066
});
10631067
}
10641068

dist/fuse.basic.js

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -367,15 +367,18 @@
367367
// When `true`, the calculation for the relevance score (used for sorting) will
368368
// ignore the field-length norm.
369369
// More info: https://fusejs.io/concepts/scoring-theory.html#field-length-norm
370-
ignoreFieldNorm: false
370+
ignoreFieldNorm: false,
371+
// The weight to determine how much field length norm effects scoring.
372+
fieldNormWeight: 1
371373
};
372374
var Config = _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({}, BasicOptions), MatchOptions), FuzzyOptions), AdvancedOptions);
373375

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

377379
function norm() {
378-
var mantissa = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 3;
380+
var weight = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
381+
var mantissa = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 3;
379382
var cache = new Map();
380383
var m = Math.pow(10, mantissa);
381384
return {
@@ -384,9 +387,10 @@
384387

385388
if (cache.has(numTokens)) {
386389
return cache.get(numTokens);
387-
}
390+
} // Default function is 1/sqrt(x), weight makes that variable
391+
388392

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

391395
var n = parseFloat(Math.round(norm * m) / m);
392396
cache.set(numTokens, n);
@@ -402,11 +406,13 @@
402406
function FuseIndex() {
403407
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
404408
_ref$getFn = _ref.getFn,
405-
getFn = _ref$getFn === void 0 ? Config.getFn : _ref$getFn;
409+
getFn = _ref$getFn === void 0 ? Config.getFn : _ref$getFn,
410+
_ref$fieldNormWeight = _ref.fieldNormWeight,
411+
fieldNormWeight = _ref$fieldNormWeight === void 0 ? Config.fieldNormWeight : _ref$fieldNormWeight;
406412

407413
_classCallCheck(this, FuseIndex);
408414

409-
this.norm = norm(3);
415+
this.norm = norm(fieldNormWeight, 3);
410416
this.getFn = getFn;
411417
this.isCreated = false;
412418
this.setIndexRecords();
@@ -585,10 +591,13 @@
585591
function createIndex(keys, docs) {
586592
var _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
587593
_ref2$getFn = _ref2.getFn,
588-
getFn = _ref2$getFn === void 0 ? Config.getFn : _ref2$getFn;
594+
getFn = _ref2$getFn === void 0 ? Config.getFn : _ref2$getFn,
595+
_ref2$fieldNormWeight = _ref2.fieldNormWeight,
596+
fieldNormWeight = _ref2$fieldNormWeight === void 0 ? Config.fieldNormWeight : _ref2$fieldNormWeight;
589597

590598
var myIndex = new FuseIndex({
591-
getFn: getFn
599+
getFn: getFn,
600+
fieldNormWeight: fieldNormWeight
592601
});
593602
myIndex.setKeys(keys.map(createKey));
594603
myIndex.setSources(docs);
@@ -598,12 +607,15 @@
598607
function parseIndex(data) {
599608
var _ref3 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
600609
_ref3$getFn = _ref3.getFn,
601-
getFn = _ref3$getFn === void 0 ? Config.getFn : _ref3$getFn;
610+
getFn = _ref3$getFn === void 0 ? Config.getFn : _ref3$getFn,
611+
_ref3$fieldNormWeight = _ref3.fieldNormWeight,
612+
fieldNormWeight = _ref3$fieldNormWeight === void 0 ? Config.fieldNormWeight : _ref3$fieldNormWeight;
602613

603614
var keys = data.keys,
604615
records = data.records;
605616
var myIndex = new FuseIndex({
606-
getFn: getFn
617+
getFn: getFn,
618+
fieldNormWeight: fieldNormWeight
607619
});
608620
myIndex.setKeys(keys);
609621
myIndex.setIndexRecords(records);
@@ -1186,7 +1198,7 @@
11861198
});
11871199
}
11881200

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

12151227
this._myIndex = index || createIndex(this.options.keys, this._docs, {
1216-
getFn: this.options.getFn
1228+
getFn: this.options.getFn,
1229+
fieldNormWeight: this.options.fieldNormWeight
12171230
});
12181231
}
12191232
}, {
@@ -1442,15 +1455,17 @@
14421455
return Fuse;
14431456
}();
14441457

1445-
Fuse.version = '6.4.6';
1446-
Fuse.createIndex = createIndex;
1447-
Fuse.parseIndex = parseIndex;
1448-
Fuse.config = Config;
1458+
Fuse$1.version = '6.4.6';
1459+
Fuse$1.createIndex = createIndex;
1460+
Fuse$1.parseIndex = parseIndex;
1461+
Fuse$1.config = Config;
14491462

14501463
{
1451-
Fuse.parseQuery = parse;
1464+
Fuse$1.parseQuery = parse;
14521465
}
14531466

1467+
var Fuse = Fuse$1;
1468+
14541469
return Fuse;
14551470

14561471
}));

0 commit comments

Comments
 (0)