CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
create.ts57 linesDownload Raw Back to search_indexes
1import { type Document } from '../../bson';2import { type Connection } from '../../cmap/connection';3import { MongoDBResponse } from '../../cmap/wire_protocol/responses';4import type { Collection } from '../../collection';5import type { ServerCommandOptions } from '../../sdam/server';6import type { ClientSession } from '../../sessions';7import { type TimeoutContext } from '../../timeout';8import { AbstractOperation } from '../operation';9 10/**11 * @public12 */13export interface SearchIndexDescription extends Document {14  /** The name of the index. */15  name?: string;16 17  /** The index definition. */18  definition: Document;19 20  /** The type of the index.  Currently `search` or `vectorSearch` are supported. */21  type?: string;22}23 24/** @internal */25export class CreateSearchIndexesOperation extends AbstractOperation<Document> {26  override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;27  private readonly collection: Collection;28  private readonly descriptions: ReadonlyArray<SearchIndexDescription>;29 30  constructor(collection: Collection, descriptions: ReadonlyArray<SearchIndexDescription>) {31    super();32    this.collection = collection;33    this.descriptions = descriptions;34    this.ns = collection.fullNamespace;35  }36 37  override get commandName() {38    return 'createSearchIndexes' as const;39  }40 41  override buildCommand(_connection: Connection, _session?: ClientSession): Document {42    const namespace = this.collection.fullNamespace;43    return {44      createSearchIndexes: namespace.collection,45      indexes: this.descriptions46    };47  }48 49  override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): string[] {50    return super.handleOk(response).indexesCreated.map((val: { name: string }) => val.name);51  }52 53  override buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions {54    return { session: this.session, timeoutContext };55  }56}57