Skip to content

Commit

Permalink
fix(lyra): fixes duplicated results when search term appears on multi…
Browse files Browse the repository at this point in the history
…ple properties
  • Loading branch information
micheleriva committed Aug 1, 2022
1 parent 137a97f commit f813865
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
16 changes: 10 additions & 6 deletions packages/lyra/src/lyra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,11 @@ export function search<T extends PropertiesSchema>(

const tokens = tokenize(params.term, language);
const indices = getIndices(lyra, params.properties);
const uniqueDocIds = new Set<string>();
const { limit = 10, offset = 0, exact = false } = params;
const results: RetrievedDoc<T>[] = Array.from({
length: limit,
});
let count = 0;

const timeStart = getNanosecondsTime();

Expand All @@ -297,13 +297,15 @@ export function search<T extends PropertiesSchema>(
for (const index of indices) {
const documentIDs = getDocumentIDsFromSearch(lyra, { ...params, index, term, exact });

count += documentIDs.length;
for (const id of documentIDs) {
uniqueDocIds.add(id);
}

if (i >= limit) {
break;
}

for (const id of documentIDs) {
for (const id of uniqueDocIds) {
if (j < offset) {
j++;
continue;
Expand All @@ -313,8 +315,10 @@ export function search<T extends PropertiesSchema>(
break;
}

const fullDoc = lyra.docs[id]!;
results[i] = { id, ...fullDoc };
if (results.findIndex(x => x?.id === id) === -1) {
const fullDoc = lyra.docs[id]!;
results[i] = { id, ...fullDoc };
}
i++;
}
}
Expand All @@ -325,7 +329,7 @@ export function search<T extends PropertiesSchema>(
return {
elapsed: getNanosecondsTime() - timeStart,
hits,
count,
count: uniqueDocIds.size,
};
}

Expand Down
39 changes: 38 additions & 1 deletion packages/lyra/tests/lyra.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,44 @@ t.test("checkInsertDocSchema", t => {
});

t.test("lyra", t => {
t.plan(13);
t.plan(14);

t.test("should correctly search for data", t => {
t.plan(6);

const db = create({
schema: {
quote: "string",
author: "string",
},
});

insert(db, { quote: "the quick, brown fox jumps over the lazy dog. What a fox!", author: "John Doe" });
insert(db, { quote: "Foxes are nice animals. But I prefer having a dog.", author: "John Doe" });
insert(db, { quote: "I like dogs. They are the best.", author: "Jane Doe" });
insert(db, { quote: "I like cats. They are the best.", author: "Jane Doe" });

// Exact search
const result1 = search(db, { term: "fox", exact: true });
const result2 = search(db, { term: "dog", exact: true });

t.equal(result1.count, 1);
t.equal(result2.count, 2);

// Prefix search
const result3 = search(db, { term: "fox", exact: false });
const result4 = search(db, { term: "dog", exact: false });

t.equal(result3.count, 2);
t.equal(result4.count, 3);

// Typo-tolerant search
const result5 = search(db, { term: "fx", tolerance: 1 });
const result6 = search(db, { term: "dg", tolerance: 2 });

t.equal(result5.count, 1);
t.equal(result6.count, 4);
});

t.test("should correctly insert and retrieve data", t => {
t.plan(4);
Expand Down

0 comments on commit f813865

Please sign in to comment.