Skip to content

Commit

Permalink
feat(search): adds human-readable elapsed time config (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleriva authored Jan 27, 2023
1 parent 8465eae commit 21af3a6
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/methods/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export async function create<S extends PropertiesSchema>(properties: Configurati
avgFieldLength: {},
fieldLengths: {},
components: {
elapsed: properties.components?.elapsed ?? {},
tokenizer,
algorithms: {
intersectTokenScores: properties.components?.algorithms?.intersectTokenScores ?? intersectTokenScores,
Expand Down
12 changes: 9 additions & 3 deletions src/methods/search.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Lyra, PropertiesSchema, ResolveSchema, SearchProperties, TokenMap, TokenScore, BM25Params, BM25OptionalParams, PropertiesBoost, FacetsSearch } from "../types.js";
import { defaultTokenizerConfig, Language } from "../tokenizer/index.js";
import { find as radixFind } from "../radix-tree/radix.js";
import { getNanosecondsTime, sortTokenScorePredicate } from "../utils.js";
import { formatNanoseconds, getNanosecondsTime, sortTokenScorePredicate } from "../utils.js";
import { getIndices } from "./common.js";
import { prioritizeTokenScores, BM25 } from "../algorithms.js";
import { FacetReturningValue, getFacets } from "../facets.js";
Expand Down Expand Up @@ -118,7 +118,7 @@ export type SearchResult<S extends PropertiesSchema> = {
/**
* The time taken to search.
*/
elapsed: bigint;
elapsed: bigint | string;
/**
* The facets results.
*/
Expand Down Expand Up @@ -297,8 +297,14 @@ export async function search<S extends PropertiesSchema>(
}
}

let elapsed: bigint | string = getNanosecondsTime() - timeStart;

if (lyra.components.elapsed?.format === "human") {
elapsed = formatNanoseconds(elapsed);
}

const searchResult: SearchResult<S> = {
elapsed: getNanosecondsTime() - timeStart,
elapsed,
hits: results.filter(Boolean),
count: Object.keys(uniqueDocsIDs).length,
};
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export type PropertiesBoost<S extends PropertiesSchema> = {
[P in keyof S]?: number;
};

export type ElaspedConfig = {
format?: "human" | "raw",
}

export type Configuration<S extends PropertiesSchema> = {
/**
* The structure of the document to be inserted into the database.
Expand All @@ -85,6 +89,7 @@ export type Data<S extends PropertiesSchema> = {
};

export type Components = {
elapsed?: ElaspedConfig;
tokenizer?: TokenizerConfig;
algorithms?: AlgorithmsConfig;
};
Expand Down
58 changes: 58 additions & 0 deletions tests/elapsed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import t from "tap";
import { create, insert, search } from "../src/index.js";

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

t.test("should correctly set elapsed time to a human-readable form", async t => {
t.plan(1);
const db = await create({
schema: {
title: "string",
body: "string",
},
components: {
elapsed: {
format: "human"
}
}
});

await insert(db, {
title: "Hello world",
body: "This is a test",
});

const results = await search(db, {
term: "test"
});

t.same(typeof results.elapsed, "string");
});

t.test("should correctly set elapsed time to a raw, bigInt", async t => {
t.plan(1);
const db = await create({
schema: {
title: "string",
body: "string",
},
components: {
elapsed: {
format: "raw"
}
}
});

await insert(db, {
title: "Hello world",
body: "This is a test",
});

const results = await search(db, {
term: "test"
});

t.same(typeof results.elapsed, "bigint");
});
});

0 comments on commit 21af3a6

Please sign in to comment.