Skip to content

Commit

Permalink
fix(logical): scoring for logical OR
Browse files Browse the repository at this point in the history
Fixes #593
  • Loading branch information
krisk committed Dec 23, 2021
1 parent 36bada4 commit 6f6af51
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 134 deletions.
16 changes: 13 additions & 3 deletions dist/fuse.basic.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,10 @@ function norm(weight = 1, mantissa = 3) {
}

class FuseIndex {
constructor({ getFn = Config.getFn, fieldNormWeight = Config.fieldNormWeight } = {}) {
constructor({
getFn = Config.getFn,
fieldNormWeight = Config.fieldNormWeight
} = {}) {
this.norm = norm(fieldNormWeight, 3);
this.getFn = getFn;
this.isCreated = false;
Expand Down Expand Up @@ -451,15 +454,22 @@ class FuseIndex {
}
}

function createIndex(keys, docs, { getFn = Config.getFn, fieldNormWeight = Config.fieldNormWeight } = {}) {
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, fieldNormWeight = Config.fieldNormWeight } = {}) {
function parseIndex(
data,
{ getFn = Config.getFn, fieldNormWeight = Config.fieldNormWeight } = {}
) {
const { keys, records } = data;
const myIndex = new FuseIndex({ getFn, fieldNormWeight });
myIndex.setKeys(keys);
Expand Down
48 changes: 11 additions & 37 deletions dist/fuse.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -2059,47 +2059,21 @@ var Fuse$1 = /*#__PURE__*/function () {

return [];
}
/*eslint indent: [2, 2, {"SwitchCase": 1}]*/

var res = [];

switch (node.operator) {
case LogicalOperator.AND:
{
var res = [];
for (var i = 0, len = node.children.length; i < len; i += 1) {
var child = node.children[i];
var result = evaluate(child, item, idx);

for (var i = 0, len = node.children.length; i < len; i += 1) {
var child = node.children[i];
var result = evaluate(child, item, idx);

if (result.length) {
res.push.apply(res, _toConsumableArray(result));
} else {
return [];
}
}

return res;
}

case LogicalOperator.OR:
{
var _res = [];

for (var _i = 0, _len = node.children.length; _i < _len; _i += 1) {
var _child = node.children[_i];

var _result = evaluate(_child, item, idx);

if (_result.length) {
_res.push.apply(_res, _toConsumableArray(_result));

break;
}
}

return _res;
}
if (result.length) {
res.push.apply(res, _toConsumableArray(result));
} else if (node.operator === LogicalOperator.AND) {
return [];
}
}

return res;
};

var records = this._myIndex.records;
Expand Down
51 changes: 22 additions & 29 deletions dist/fuse.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,10 @@ function norm(weight = 1, mantissa = 3) {
}

class FuseIndex {
constructor({ getFn = Config.getFn, fieldNormWeight = Config.fieldNormWeight } = {}) {
constructor({
getFn = Config.getFn,
fieldNormWeight = Config.fieldNormWeight
} = {}) {
this.norm = norm(fieldNormWeight, 3);
this.getFn = getFn;
this.isCreated = false;
Expand Down Expand Up @@ -449,15 +452,22 @@ class FuseIndex {
}
}

function createIndex(keys, docs, { getFn = Config.getFn, fieldNormWeight = Config.fieldNormWeight } = {}) {
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, fieldNormWeight = Config.fieldNormWeight } = {}) {
function parseIndex(
data,
{ getFn = Config.getFn, fieldNormWeight = Config.fieldNormWeight } = {}
) {
const { keys, records } = data;
const myIndex = new FuseIndex({ getFn, fieldNormWeight });
myIndex.setKeys(keys);
Expand Down Expand Up @@ -1641,34 +1651,17 @@ class Fuse {
return []
}

/*eslint indent: [2, 2, {"SwitchCase": 1}]*/
switch (node.operator) {
case LogicalOperator.AND: {
const res = [];
for (let i = 0, len = node.children.length; i < len; i += 1) {
const child = node.children[i];
const result = evaluate(child, item, idx);
if (result.length) {
res.push(...result);
} else {
return []
}
}
return res
}
case LogicalOperator.OR: {
const res = [];
for (let i = 0, len = node.children.length; i < len; i += 1) {
const child = node.children[i];
const result = evaluate(child, item, idx);
if (result.length) {
res.push(...result);
break
}
}
return res
const res = [];
for (let i = 0, len = node.children.length; i < len; i += 1) {
const child = node.children[i];
const result = evaluate(child, item, idx);
if (result.length) {
res.push(...result);
} else if (node.operator === LogicalOperator.AND) {
return []
}
}
return res
};

const records = this._myIndex.records;
Expand Down

0 comments on commit 6f6af51

Please sign in to comment.