Skip to content

Commit

Permalink
fix(full-text-search): fix fuzzy extended to withdraw wrong results (#51
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Viatorus authored Nov 26, 2017
1 parent 4bbb397 commit 4d493ac
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions packages/full-text-search/spec/generic/search/fuzzy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ describe("fuzzy query", () => {
});
}
let query = new QB().fuzzy("body", "web").prefixLength(1).fuzziness(1).extended(true).build();
assertMatches(fts, query, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
assertMatches(fts, query, [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);

query = new QB().match("body", "web").prefixLength(1).fuzziness(1).extended(true).build();
assertMatches(fts, query, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
assertMatches(fts, query, [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
});
});
20 changes: 11 additions & 9 deletions packages/full-text-search/src/index_searcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,17 @@ function fuzzySearch(query: ANY, root: InvertedIndex.Index) {
let index = stack.pop();
let treeTerms = treeStack.pop();

// Check if fuzzy should be extended.
if (extended) {
if (treeTerms.length === fuzzy.length) {
extend_fuzzy = levenshteinDistance(fuzzy, treeTerms);
} else {
extend_fuzzy = extend_fuzzy <= fuzziness && treeTerms.length >= fuzzy.length
? extend_fuzzy
: 1e10;
}
}

// Compare tokens if they are in near distance.
if (index.df !== undefined) {
let matched = false;
Expand All @@ -378,15 +389,6 @@ function fuzzySearch(query: ANY, root: InvertedIndex.Index) {
}
}

// Check if fuzzy should be extended.
if (extended && treeTerms.length === fuzzy.length) {
extend_fuzzy = levenshteinDistance(fuzzy, treeTerms);
} else {
extend_fuzzy = extended && extend_fuzzy <= fuzziness && treeTerms.length >= fuzzy.length
? extend_fuzzy
: 1e10;
}

// Iterate over all subtrees.
// If token from tree is not longer than maximal distance.
if ((treeTerms.length - fuzzy.length <= fuzziness) || extend_fuzzy <= fuzziness) {
Expand Down

0 comments on commit 4d493ac

Please sign in to comment.