Skip to content

Commit

Permalink
Merge pull request #21 from tigrisdata/main
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
ovaistariq authored May 8, 2023
2 parents 410ded8 + 82310e4 commit 962d800
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { VectorDocumentStore } from "@tigrisdata/vector";

const vectorDocStore = new VectorDocumentStore({
connection: {
serverUrl: "api-preview.tigrisdata.io",
serverUrl: "api.preview.tigrisdata.cloud",
projectName: "vectordemo",
clientId: "clientId_here",
clientSecret: "clientSecret_here",
Expand Down
41 changes: 41 additions & 0 deletions src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ const mockGetSearch = jest.fn().mockImplementation(() => {
createOrUpdateIndex: jest.fn().mockImplementation(() => {
return {
createOrReplaceMany: jest.fn(),
deleteMany: jest.fn(),
deleteByQuery: jest.fn(),
getMany: jest.fn().mockImplementation(() => {
return [
{
document: {
content: "testContent",
metadata: { test: "test" },
},
meta: {},
},
];
}),
search: jest.fn().mockImplementation(() => {
return {
hits: [
Expand All @@ -22,6 +35,7 @@ const mockGetSearch = jest.fn().mockImplementation(() => {
}),
};
}),
deleteIndex: jest.fn(),
};
});

Expand Down Expand Up @@ -96,6 +110,15 @@ it("should create an index with the correct schema", async () => {
);
});

it("should delete an index", async () => {
const vectorStore = new VectorDocumentStore(config);
const searchClient = vectorStore.searchClient;

await vectorStore.deleteIndex();

expect(searchClient.deleteIndex).toHaveBeenCalledWith(config.indexName);
});

it("should create documents with the correct schema", async () => {
const vectorStore = new VectorDocumentStore(config);

Expand Down Expand Up @@ -123,6 +146,24 @@ it("should create documents with the correct schema", async () => {
expect(index.createOrReplaceMany).toHaveBeenCalledWith(documents);
});

it("should delete documents with the correct schema", async () => {
const vectorStore = new VectorDocumentStore(config);

await vectorStore.deleteDocuments(["testId"]);
const index = vectorStore.index;

expect(index.deleteMany).toHaveBeenCalledWith(["testId"]);
});

it("should get documents with the correct schema", async () => {
const vectorStore = new VectorDocumentStore(config);

await vectorStore.getDocuments(["testId"]);
const index = vectorStore.index;

expect(index.getMany).toHaveBeenCalledWith(["testId"]);
});

it("should search for documents with the correct schema", async () => {
const vectorStore = new VectorDocumentStore(config);

Expand Down
51 changes: 51 additions & 0 deletions src/vectorstore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
DeleteIndexResponse,
DocStatus,
Filter,
Search,
Expand Down Expand Up @@ -69,6 +70,14 @@ export class VectorDocumentStore {
);
}

/**
* Deletes the index.
* @returns A promise that resolves when the index has been deleted
*/
public async deleteIndex(): Promise<DeleteIndexResponse> {
return this._searchClient.deleteIndex(this._indexName);
}

/**
* Add documents to the index.
* @param ids The IDs of the documents to add
Expand Down Expand Up @@ -101,6 +110,48 @@ export class VectorDocumentStore {
return this._index.createOrReplaceMany(documentsToAdd);
}

/**
* Delete documents from the index.
* @param ids The IDs of the documents to delete
* @returns A promise that resolves when the documents have been deleted
* successfully
*/
public async deleteDocuments(ids: string[]): Promise<DocStatus[]> {
await this.enusreIndex();

return this._index.deleteMany(ids);
}

/**
* Delete documents from the index by filter.
* @param filter The filter to apply to the documents to delete
* @returns A promise that resolves when the documents have been deleted
* successfully
*/
public async deleteDocumentsByFilter(filter: object): Promise<number> {
await this.enusreIndex();

return this._index.deleteByQuery(filter);
}

/**
* Get documents from the index by ID.
* @param ids The IDs of the documents to get
* @returns A promise that resolves to an array of documents fetched
* successfully
*/
public async getDocuments(ids: string[]): Promise<Document[]> {
await this.enusreIndex();

const response = await this._index.getMany(ids);
return response.map((document) => {
return {
content: document.document.content,
metadata: document.document.metadata,
};
});
}

/**
* Search for the most similar documents to a query,
* when you already have the embedding of the query.
Expand Down

0 comments on commit 962d800

Please sign in to comment.