Skip to content

Commit

Permalink
community[patch]: Index name column of collection pgvectorstore (#5333)
Browse files Browse the repository at this point in the history
* community[patch]: Index name column of collection pgvectorstore

* chore: lint files

* chore: lint files
  • Loading branch information
bracesproul authored May 10, 2024
1 parent 64fe671 commit 4151d74
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
6 changes: 5 additions & 1 deletion libs/langchain-community/src/vectorstores/pgvector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,8 @@ export class PGVectorStore extends VectorStore {
cmetadata jsonb
);
CREATE INDEX IF NOT EXISTS idx_${this.collectionTableName}_name ON ${this.computedCollectionTableName}(name);
ALTER TABLE ${this.computedTableName}
ADD COLUMN collection_id uuid;
Expand All @@ -597,7 +599,9 @@ export class PGVectorStore extends VectorStore {
} catch (e) {
if (!(e as Error).message.includes("already exists")) {
console.error(e);
throw new Error(`Error adding column: ${(e as Error).message}`);
throw new Error(
`Error adding column or creating index: ${(e as Error).message}`
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe("PGVectorStore", () => {
postgresConnectionOptions: {
type: "postgres",
host: "127.0.0.1",
port: 5433,
port: 5432,
user: "myuser",
password: "ChangeMe",
database: "api",
Expand Down Expand Up @@ -252,7 +252,7 @@ describe("PGVectorStore", () => {
});
});

describe.skip("PGVectorStore with collection", () => {
describe("PGVectorStore with collection", () => {
let pgvectorVectorStore: PGVectorStore;
const tableName = "testlangchain_collection";
const collectionTableName = "langchain_pg_collection";
Expand All @@ -262,7 +262,7 @@ describe.skip("PGVectorStore with collection", () => {
postgresConnectionOptions: {
type: "postgres",
host: "127.0.0.1",
port: 5433,
port: 5432,
user: "myuser",
password: "ChangeMe",
database: "api",
Expand Down Expand Up @@ -298,6 +298,18 @@ describe.skip("PGVectorStore with collection", () => {
await pgvectorVectorStore.end();
});

test("'name' column is indexed", async () => {
const result = await pgvectorVectorStore.pool.query(
`SELECT * FROM pg_indexes WHERE tablename = '${pgvectorVectorStore.computedCollectionTableName}'`
);
const expectedIndexName = `idx_${pgvectorVectorStore.computedCollectionTableName}_name`;

const index = result.rows.find(
(row) => row.indexname === expectedIndexName
);
expect(index).toBeDefined();
});

test("Test embeddings creation", async () => {
const documents = [
{
Expand Down Expand Up @@ -475,7 +487,7 @@ describe.skip("PGVectorStore with collection", () => {
});
});

describe.skip("PGVectorStore with schema", () => {
describe("PGVectorStore with schema", () => {
let pgvectorVectorStore: PGVectorStore;
const tableName = "testlangchain_schema";
const schema = "test_schema";
Expand All @@ -487,7 +499,7 @@ describe.skip("PGVectorStore with schema", () => {
beforeAll(async () => {
pool = new pg.Pool({
host: "127.0.0.1",
port: 5433,
port: 5432,
user: "myuser",
password: "ChangeMe",
database: "api",
Expand Down

0 comments on commit 4151d74

Please sign in to comment.