From 4fb4613676edc815c135eebb601336893f5c9caf Mon Sep 17 00:00:00 2001 From: Clemens Peters <13015002+clemenspeters@users.noreply.github.com> Date: Sat, 3 Aug 2024 02:58:13 +0200 Subject: [PATCH] Add namespace to allow indexing multiple tables within the same schema (#6341) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ✨ Add namespace to allow indexing multiple tables within the same schema * 🚨 Auto style fixes with `yarn format` --- libs/langchain-community/src/vectorstores/pgvector.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libs/langchain-community/src/vectorstores/pgvector.ts b/libs/langchain-community/src/vectorstores/pgvector.ts index 6a58c4ba5cd0..8f4e564f2caf 100644 --- a/libs/langchain-community/src/vectorstores/pgvector.ts +++ b/libs/langchain-community/src/vectorstores/pgvector.ts @@ -685,6 +685,7 @@ export class PGVectorStore extends VectorStore { * @param m - The max number of connections per layer (16 by default). Index build time improves with smaller values, while higher values can speed up search queries. * @param efConstruction - The size of the dynamic candidate list for constructing the graph (64 by default). A higher value can potentially improve the index quality at the cost of index build time. * @param distanceFunction - The distance function name you want to use, is automatically selected based on the distanceStrategy. + * @param namespace - The namespace is used to create the index with a specific name. This is useful when you want to create multiple indexes on the same database schema (within the same schema in PostgreSQL, the index name must be unique across all tables). * @returns Promise that resolves with the query response of creating the index. */ async createHnswIndex(config: { @@ -692,8 +693,10 @@ export class PGVectorStore extends VectorStore { m?: number; efConstruction?: number; distanceFunction?: string; + namespace?: string; }): Promise { let idxDistanceFunction = config?.distanceFunction || "vector_cosine_ops"; + const prefix = config?.namespace ? `${config.namespace}_` : ""; switch (this.distanceStrategy) { case "cosine": @@ -709,7 +712,7 @@ export class PGVectorStore extends VectorStore { throw new Error(`Unknown distance strategy: ${this.distanceStrategy}`); } - const createIndexQuery = `CREATE INDEX IF NOT EXISTS ${ + const createIndexQuery = `CREATE INDEX IF NOT EXISTS ${prefix}${ this.vectorColumnName }_embedding_hnsw_idx ON ${this.computedTableName} USING hnsw ((${