import type { AggregateOptions, AggregateResult, CountRetriever, FlatIndexSchema, IndexDescription, Language, NestedIndexSchema, QueryOptions, QueryResult, RootQueryFilter, } from "./types"; import type { Requester } from "../../http"; import { setOwnProperty } from "../../util"; import { ExecCommand } from "../exec"; import { buildAggregateCommand, buildCreateIndexCommand, buildQueryCommand, } from "./command-builder"; import { deserializeAggregateResponse, deserializeDescribeResponse, deserializeQueryResponse, parseCountResponse, } from "./utils"; export type CreateIndexParameters = { name: string; language?: Language; skipInitialScan?: boolean; existsOk?: boolean; } & ( | { dataType: "string"; prefix: string | string[]; schema: TSchema extends NestedIndexSchema ? TSchema : never; } | { dataType: "json"; prefix: string | string[]; schema: TSchema extends NestedIndexSchema ? TSchema : never; } | { dataType: "hash"; prefix: string | string[]; schema: TSchema extends FlatIndexSchema ? TSchema : never; } | { /** * Index the entries of a single Redis stream. Each entry becomes a document whose key is * the entry ID and whose fields are the entry's fields. * * @see https://upstash.com/docs/redis/search/streams */ dataType: "stream"; /** * Exact key of the stream to index. The stream does not need to exist yet. */ stream: string; prefix?: never; schema: TSchema extends FlatIndexSchema ? TSchema : never; } ); export type InitIndexParameters = { name: string; schema?: TSchema; }; export type SearchIndexParameters = { name: string; client: Requester; schema?: TSchema; }; export class SearchIndex { readonly name: SearchIndexParameters["name"]; readonly schema?: TSchema; private client: Requester; constructor({ name, schema, client }: SearchIndexParameters) { this.name = name; this.schema = schema; this.client = client; } async waitIndexing(): Promise<0 | 1> { const command = ["SEARCH.WAITINDEXING", this.name]; return await new ExecCommand<0 | 1>(command as [string, ...string[]]).exec(this.client); } async describe(): Promise | null> { const command = ["SEARCH.DESCRIBE", this.name]; const rawResult = await new ExecCommand(command as [string, ...string[]]).exec( this.client ); if (!rawResult) return null; return deserializeDescribeResponse(rawResult); } async query>( options?: TOpts ): Promise[]> { const command = buildQueryCommand("SEARCH.QUERY", this.name, options); const rawResult = await new ExecCommand(command as [string, ...string[]]).exec( this.client ); if (!rawResult) return rawResult; return deserializeQueryResponse(rawResult); } async aggregate>( options: TOpts ): Promise> { const command = buildAggregateCommand(this.name, options); const rawResult = await new ExecCommand<(string | number)[]>( command as [string, ...string[]] ).exec(this.client); return deserializeAggregateResponse(rawResult); } async count({ filter, }: { filter: RootQueryFilter | CountRetriever; }): Promise<{ count: number }> { const command = buildQueryCommand("SEARCH.COUNT", this.name, { filter, } as QueryOptions); const rawResult = await new ExecCommand(command as [string, ...string[]]).exec( this.client ); return { count: parseCountResponse(rawResult) }; } async drop(): Promise<1 | 0> { const command = ["SEARCH.DROP", this.name]; const result = await new ExecCommand<1 | 0>(command as [string, ...string[]]).exec(this.client); return result; } async addAlias({ alias }: { alias: string }): Promise<1> { const command = ["SEARCH.ALIASADD", alias, this.name]; const result = await new ExecCommand<1>(command as [string, ...string[]]).exec(this.client); return result; } } export async function createIndex( client: Requester, params: CreateIndexParameters ): Promise> { const { name, schema } = params; const createIndexCommand = buildCreateIndexCommand(params); await new ExecCommand(createIndexCommand as [string, ...string[]]).exec(client); return initIndex(client, { name, schema: schema as TSchema }); } export function initIndex( client: Requester, params: Pick, "name"> ): SearchIndex; export function initIndex( client: Requester, params: InitIndexParameters ): SearchIndex; export function initIndex( client: Requester, params: InitIndexParameters ): SearchIndex { const { name, schema } = params; return new SearchIndex({ name, schema, client }); } export type InferFilterFromSchema = NonNullable["query"]>[0]>["filter"]>; export async function listAliases(client: Requester): Promise> { const command = ["SEARCH.LISTALIASES"]; const rawResult = await new ExecCommand(command as [string, ...string[]]).exec(client); // Handle empty case - might be 0 or an empty array if (rawResult === 0 || (Array.isArray(rawResult) && rawResult.length === 0)) { return {}; } // Parse the response: [[alias1, index1], [alias2, index2], ...] if (!Array.isArray(rawResult)) { return {}; } const aliases: Record = {}; for (const pair of rawResult) { if (Array.isArray(pair) && pair.length === 2) { const [alias, index] = pair; setOwnProperty(aliases, alias, index); } } return aliases; } export async function addAlias( client: Requester, { indexName, alias }: { indexName: string; alias: string } ): Promise<1 | 2> { const command = ["SEARCH.ALIASADD", alias, indexName]; const result = await new ExecCommand<1 | 2>(command as [string, ...string[]]).exec(client); return result; } export async function delAlias(client: Requester, { alias }: { alias: string }): Promise<0 | 1> { const command = ["SEARCH.ALIASDEL", alias]; const result = await new ExecCommand<0 | 1>(command as [string, ...string[]]).exec(client); return result; }