diff --git a/dist/fuse.basic.common.js b/dist/fuse.basic.common.js index fa99177b4..7ab92e856 100644 --- a/dist/fuse.basic.common.js +++ b/dist/fuse.basic.common.js @@ -363,7 +363,9 @@ 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); @@ -371,7 +373,8 @@ var SPACE = /[^ ]+/g; // Field-length norm: the shorter the field, the higher th // 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 { @@ -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); @@ -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(); @@ -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); @@ -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); @@ -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; @@ -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 }); } }, { @@ -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; diff --git a/dist/fuse.basic.esm.js b/dist/fuse.basic.esm.js index 1160ac34c..da01ef117 100644 --- a/dist/fuse.basic.esm.js +++ b/dist/fuse.basic.esm.js @@ -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 = { @@ -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); @@ -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); @@ -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; @@ -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 @@ -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 }); } diff --git a/dist/fuse.basic.js b/dist/fuse.basic.js index 6df784de9..c0ba7f30f 100644 --- a/dist/fuse.basic.js +++ b/dist/fuse.basic.js @@ -367,7 +367,9 @@ // 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); @@ -375,7 +377,8 @@ // 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 { @@ -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); @@ -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(); @@ -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); @@ -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); @@ -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; @@ -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 }); } }, { @@ -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; })); diff --git a/dist/fuse.common.js b/dist/fuse.common.js index a05df3d39..27afc8143 100644 --- a/dist/fuse.common.js +++ b/dist/fuse.common.js @@ -446,7 +446,9 @@ 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); @@ -454,7 +456,8 @@ var SPACE = /[^ ]+/g; // Field-length norm: the shorter the field, the higher th // 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 { @@ -463,9 +466,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); @@ -481,11 +485,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(); @@ -664,10 +670,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); @@ -677,12 +686,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); @@ -1875,7 +1887,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; @@ -1902,7 +1914,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 }); } }, { @@ -2227,17 +2240,19 @@ 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; } { register(ExtendedSearch); } +var Fuse = Fuse$1; + module.exports = Fuse; diff --git a/dist/fuse.esm.js b/dist/fuse.esm.js index bbd3b241a..c2dbeff48 100644 --- a/dist/fuse.esm.js +++ b/dist/fuse.esm.js @@ -264,7 +264,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 = { @@ -278,7 +280,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); @@ -290,7 +292,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); @@ -306,8 +309,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; @@ -446,17 +449,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 @@ -1515,7 +1518,8 @@ class Fuse { this._myIndex = index || createIndex(this.options.keys, this._docs, { - getFn: this.options.getFn + getFn: this.options.getFn, + fieldNormWeight: this.options.fieldNormWeight }); } diff --git a/dist/fuse.js b/dist/fuse.js index 9460af0ee..5a08bca87 100644 --- a/dist/fuse.js +++ b/dist/fuse.js @@ -450,7 +450,9 @@ // 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); @@ -458,7 +460,8 @@ // 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 { @@ -467,9 +470,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); @@ -485,11 +489,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(); @@ -668,10 +674,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); @@ -681,12 +690,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); @@ -1879,7 +1891,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; @@ -1906,7 +1918,8 @@ } this._myIndex = index || createIndex(this.options.keys, this._docs, { - getFn: this.options.getFn + getFn: this.options.getFn, + fieldNormWeight: this.options.fieldNormWeight }); } }, { @@ -2231,19 +2244,21 @@ 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; } { register(ExtendedSearch); } + var Fuse = Fuse$1; + return Fuse; })); diff --git a/src/core/config.js b/src/core/config.js index 495d626ee..13d019f9e 100644 --- a/src/core/config.js +++ b/src/core/config.js @@ -54,7 +54,9 @@ export 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 } export default { diff --git a/src/core/index.js b/src/core/index.js index a95459317..17325ffd3 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -34,7 +34,8 @@ export default class Fuse { this._myIndex = index || createIndex(this.options.keys, this._docs, { - getFn: this.options.getFn + getFn: this.options.getFn, + fieldNormWeight: this.options.fieldNormWeight }) } @@ -286,4 +287,4 @@ export default class Fuse { return matches } -} \ No newline at end of file +} diff --git a/src/tools/FuseIndex.js b/src/tools/FuseIndex.js index e02357cb1..4d3213c30 100644 --- a/src/tools/FuseIndex.js +++ b/src/tools/FuseIndex.js @@ -4,8 +4,8 @@ import normGenerator from './norm' import { createKey } from './KeyStore' export default class FuseIndex { - constructor({ getFn = Config.getFn } = {}) { - this.norm = normGenerator(3) + constructor({ getFn = Config.getFn, fieldNormWeight = Config.fieldNormWeight } = {}) { + this.norm = normGenerator(fieldNormWeight, 3) this.getFn = getFn this.isCreated = false @@ -147,17 +147,17 @@ export default class FuseIndex { } } -export function createIndex(keys, docs, { getFn = Config.getFn } = {}) { - const myIndex = new FuseIndex({ getFn }) +export 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 } -export function parseIndex(data, { getFn = Config.getFn } = {}) { +export 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 diff --git a/src/tools/norm.js b/src/tools/norm.js index 23abf9cc5..9ed8575a4 100644 --- a/src/tools/norm.js +++ b/src/tools/norm.js @@ -2,7 +2,7 @@ const SPACE = /[^ ]+/g // Field-length norm: the shorter the field, the higher the weight. // Set to 3 decimals to reduce index size. -export default function norm(mantissa = 3) { +export default function norm(weight = 1, mantissa = 3) { const cache = new Map() const m = Math.pow(10, mantissa) @@ -14,7 +14,8 @@ export default 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) diff --git a/test/scoring.test.js b/test/scoring.test.js new file mode 100644 index 000000000..9a8f99751 --- /dev/null +++ b/test/scoring.test.js @@ -0,0 +1,65 @@ +const Fuse = require('../dist/fuse') + +const defaultList = ['Stove', 'My good friend Steve from college'] +const defaultOptions = {} + +const setup = (itemList, overwriteOptions) => { + const list = itemList || defaultList + const options = { ...defaultOptions, ...overwriteOptions } + + return new Fuse(list, options) +} + +describe('Flat list of strings: ["Stove", "My good friend Steve from college"]', () => { + + describe('When performing a fuzzy search for the term "Steve" with ignoreFieldNorm off', () => { + let result, fuse + beforeEach(() => { + fuse = setup() + result = fuse.search('Steve') + }) + + test('we get a list of containing 2 items', () => { + expect(result).toHaveLength(2) + }) + + test('whose values represent the indices of ["Stove", "My good friend Steve from college"]', () => { + expect(result[0].refIndex).toBe(0) + expect(result[1].refIndex).toBe(1) + }) + }) + + describe('When performing a fuzzy search for the term "Steve" with ignoreFieldNorm on', () => { + let result, fuse + beforeEach(() => { + fuse = setup(null, { ignoreFieldNorm: true }) + result = fuse.search('Steve') + }) + + test('we get a list of containing 2 items', () => { + expect(result).toHaveLength(2) + }) + + test('whose values represent the indices of ["My good friend Steve from college", "Stove"]', () => { + expect(result[0].refIndex).toBe(1) + expect(result[1].refIndex).toBe(0) + }) + }) + + describe('When performing a fuzzy search for the term "Steve" with ignoreFieldNorm off and fieldNormWeight decreased', () => { + let result, fuse + beforeEach(() => { + fuse = setup(null, { fieldNormWeight: 0.15 }) + result = fuse.search('Steve') + }) + + test('we get a list of containing 2 items', () => { + expect(result).toHaveLength(2) + }) + + test('whose values represent the indices of ["My good friend Steve from college", "Stove"]', () => { + expect(result[0].refIndex).toBe(1) + expect(result[1].refIndex).toBe(0) + }) + }) +})