Skip to content
Merged
Changes from all 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
20 changes: 18 additions & 2 deletions src/libsql/search-index/libsql-search-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ import type { LibsqlClientBaseOptions } from "@/libsql/libsql-client-base-option
import type { LibsqlSearchQueryBuilder } from "./libsql-search-query-builder.ts";
import { rebuildLibsqlSearchIndexFromQuads } from "./rebuild-libsql-search-index-from-quads.ts";

/**
* SearchRequestWithProfile extends SearchRequest with memory profile overrides for topK and minScore.
* These fields will be added to the upstream SearchRequest interface in a future release.
*/
interface SearchRequestWithProfile extends SearchRequest {
topK?: number;
minScore?: number;
}

/**
* LibsqlSearchIndexOptions defines the structured configuration and dependency parameters needed to construct the LibSQL search engine.
*/
Expand All @@ -37,6 +46,7 @@ export class LibsqlSearchIndex implements SearchIndexInterface {
* search executes a keyword and vector hybrid query against the current index.
*/
public async search(request: SearchRequest): Promise<SearchResponse> {
const profileRequest = request as SearchRequestWithProfile;
let vectorJson: string | undefined;

if (this.options.embeddingService) {
Expand All @@ -63,19 +73,25 @@ export class LibsqlSearchIndex implements SearchIndexInterface {
}
}

const searchLimit = profileRequest.topK ?? this.options.limit ?? 100;

const { sql, args } = this.options.searchQueryBuilder.buildSearchQuery(
request,
{
vectorJson,
limit: this.options.limit ?? 100,
limit: searchLimit,
},
);

const resultSet = await this.options.client.execute({ sql, args });

const minScore = profileRequest.minScore ?? 0;
const results: SearchResult[] = [];

for (const row of resultSet.rows) {
const score = Number(row["combined_rank"]);
if (score < minScore) continue;

const searchResultBase = {
subject: String(row["subject"]),
predicate: String(row["predicate"]),
Expand All @@ -85,7 +101,7 @@ export class LibsqlSearchIndex implements SearchIndexInterface {
results.push({
id: await buildSearchResultId(searchResultBase),
...searchResultBase,
score: Number(row["combined_rank"]),
score,
});
}

Expand Down