Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed intersectMany function #160

Merged
merged 5 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function intersectMany<T>(arrays: T[][]): T[] {
let found = 0;
for (const elem of arrays[i]) {
const count = set.get(elem);
if (count === 1) {
if (count === i) {
set.set(elem, count + 1);
found++;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/datasets/event-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export type EventJson = {
matteoscaramuzza marked this conversation as resolved.
Show resolved Hide resolved
result: {
events: {
date: string;
description: string;
granularity: string;
category1: string;
category2: string;
}[];
};
};
32 changes: 28 additions & 4 deletions tests/lyra.dataset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import t from "tap";
import { create, insertBatch, remove, search } from "../src/lyra";
import type { PropertiesSchema, SearchResult } from "../src/lyra";
import dataset from "./datasets/events.json";
import { EventJson } from "./datasets/event-json";

function removeVariadicData<T extends PropertiesSchema>(res: SearchResult<T>): SearchResult<T> {
const hits = res.hits.map(h => {
Expand Down Expand Up @@ -29,11 +30,10 @@ const db = create({
});

t.test("lyra.dataset", async t => {
t.plan(3);
t.plan(4);

t.before(async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const events = (dataset as any).result.events.map((ev: any) => ({
const events = (dataset as EventJson).result.events.map(ev => ({
date: ev.date,
description: ev.description,
granularity: ev.granularity,
Expand Down Expand Up @@ -73,12 +73,36 @@ t.test("lyra.dataset", async t => {
offset: 0,
});

t.equal(Object.keys(db.docs).length, (dataset as any).result.events.length);
t.equal(Object.keys(db.docs).length, (dataset as EventJson).result.events.length);
t.equal(s1.count, 1117);
t.equal(s2.count, 1842);
t.equal(s3.count, 1842);
});

// Tests for https://github.com/LyraSearch/lyra/issues/159
t.test("should correctly search long strings", t => {
t.plan(3);

const s1 = search(db, {
term: "e into the",
properties: ["description"],
});

const s2 = search(db, {
term: "The Roman armies",
properties: ["description"],
});

const s3 = search(db, {
term: "the King of Epirus, is taken",
properties: ["description"],
});

t.equal(s1.count, 500);
t.equal(s2.count, 183);
t.equal(s3.count, 1);
});

t.test("should perform paginate search", t => {
t.plan(5);

Expand Down
63 changes: 35 additions & 28 deletions tests/lyra.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ t.test("lyra", t => {
t.plan(17);

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

const db = create({
schema: {
Expand Down Expand Up @@ -148,6 +148,13 @@ t.test("lyra", t => {

t.equal(result5.count, 2);
t.equal(result6.count, 4);

// Long string search (Tests for https://github.com/LyraSearch/lyra/issues/159 )
const result7 = search(db, { term: "They are the best" });
const result8 = search(db, { term: "Foxes are nice animals" });

t.equal(result7.count, 2);
t.equal(result8.count, 1);
});

t.test("should correctly insert and retrieve data", t => {
Expand Down Expand Up @@ -608,67 +615,67 @@ t.test("lyra", t => {
quote: "string",
author: {
name: "string",
surname: "string"
surname: "string",
},
tag: {
name: "string",
description: "string"
}
}
})
description: "string",
},
},
});

insert(db, {
quote: "Be yourself; everyone else is already taken.",
author: {
name: "Oscar",
surname: "Wild"
surname: "Wild",
},
tag: {
name: "inspirational",
description: "Inspirational quotes"
}
})
description: "Inspirational quotes",
},
});

insert(db, {
quote: "So many books, so little time.",
author: {
name: "Frank",
surname: "Zappa"
surname: "Zappa",
},
tag: {
name: "books",
description: "Quotes about books"
}
})
description: "Quotes about books",
},
});

insert(db, {
quote: "A room without books is like a body without a soul.",
author: {
name: "Marcus",
surname: "Tullius Cicero"
surname: "Tullius Cicero",
},
tag: {
name: "books",
description: "Quotes about books"
}
})
description: "Quotes about books",
},
});

const resultAuthor = search(db, {
term: "Oscar"
})
term: "Oscar",
});

const resultTag = search(db, {
term: "books"
})
term: "books",
});

const resultQuotes = search(db, {
term: "quotes"
})
term: "quotes",
});

t.equal(resultAuthor.count, 1)
t.equal(resultTag.count, 2)
t.equal(resultQuotes.count, 3)
})
t.equal(resultAuthor.count, 1);
t.equal(resultTag.count, 2);
t.equal(resultQuotes.count, 3);
});

t.test("should suport batch insert of documents", async t => {
t.plan(2);
Expand Down
17 changes: 15 additions & 2 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import t from "tap";
import { formatBytes, formatNanoseconds } from "../src/utils";
import { intersectMany, formatBytes, formatNanoseconds } from "../src/utils";

t.test("utils", t => {
t.plan(2);
t.plan(3);

t.test("should correctly intersect 2 or more arrays", t => {
t.plan(4);

const arr1 = [1, 2, 3, 4, 5, 8];
const arr2 = [2, 3, 8];
const arr3 = [4, 6, 5, 8];

t.equal(intersectMany([arr1, arr2]).length, 3);
matteoscaramuzza marked this conversation as resolved.
Show resolved Hide resolved
t.equal(intersectMany([arr1, arr3]).length, 3);
t.equal(intersectMany([arr2, arr3]).length, 1);
t.equal(intersectMany([arr1, arr2, arr3]).length, 1);
});

t.test("should correctly format bytes", t => {
t.plan(9);
Expand Down