Skip to content

Commit

Permalink
Merge pull request #27 from upstash/dx-883-vector-sdk-namespace-support
Browse files Browse the repository at this point in the history
add: namespace to readme
  • Loading branch information
fahreddinozcan authored May 14, 2024
2 parents f1c7ac6 + 0fd0472 commit 7567b73
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 26 deletions.
124 changes: 100 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,43 +61,58 @@ await index.upsert([{
}])

//Query Data
const results = await index.query<Metadata>({
vector: [
... // query embedding
],
includeVectors: true,
includeMetadata: true
topK: 1,
filter: "genre = 'fantasy' and title = 'Lord of the Rings'"
})
const results = await index.query<Metadata>(
{
vector: [
... // query embedding
],
includeVectors: true,
includeMetadata: true
topK: 1,
filter: "genre = 'fantasy' and title = 'Lord of the Rings'"
},
{
namespace: "example-namespace"
}
)

// If you wanna learn more about filtering check: [Metadata Filtering](https://upstash.com/docs/vector/features/filtering)

//Update Data
await index.upsert({
id: "upstash-rocks",
metadata: {
title: 'Star Wars',
genre: 'sci-fi',
category: 'classic'
await index.upsert(
{
id: "upstash-rocks",
metadata: {
title: 'Star Wars',
genre: 'sci-fi',
category: 'classic'
}
},
{
namespace: "namespace"
}
});
);

//Delete record
await index.delete("upstash-rocks");
await index.delete("upstash-rocks", {namespace: "example-namespace"});

//Delete many by id
await index.delete(["id-1", "id-2", "id-3"]);

//Fetch records by their IDs
await index.fetch(["id-1", "id-2"]);
await index.fetch(["id-1", "id-2"], {namespace: "example-namespace"});

//Fetch records with range
await index.range({
cursor: 0,
limit: 5,
includeVectors: true,
});
await index.range(
{
cursor: 0,
limit: 5,
includeVectors: true,
},
{
namespace: "example-namespace"
}
);

//Reset index
await index.reset();
Expand All @@ -106,7 +121,68 @@ await index.reset();
await index.info();

//Random vector based on stored vectors
await index.random();
await index.random({namespace: "example-namespace"});

//List existing namesapces
await index.listNamespaces();

//Delete a namespace
await index.deleteNamespace("namespace-to-be-deleted");
```

## Namespaces

Upstash Vector allows you to partition a single index into multiple isolated namespaces. Each namespace functions as a self-contained subset of the index, in which read and write requests are only limited to one namespace. To learn more about it, see [Namespaces](https://upstash.com/docs/vector/features/namespaces)

# Example

```ts
import { Index } from "@upstash/vector";

type Metadata = {
title: string;
genre: "sci-fi" | "fantasy" | "horror" | "action";
category: "classic" | "modern";
};

const index = new Index<Metadata>({
url: "<UPSTASH_VECTOR_REST_URL>",
token: "<UPSTASH_VECTOR_REST_TOKEN>",
});

const namespace = index.namespace("example-namespace");

//Upsert Data
await namespace.upsert([{
id: 'upstash-rocks',
vector: [
.... // embedding values
],
metadata: {
title: 'Lord of The Rings',
genre: 'fantasy',
category: 'classic'
}
}])

//Query Vector
const results = await namespace.query<Metadata>(
{
vector: [
... // query embedding
],
includeVectors: true,
includeMetadata: true
topK: 1,
filter: "genre = 'fantasy' and title = 'Lord of the Rings'"
},
)

//Delete Record
await namespace.delete("upstash-rocks");

//Fetch records by their IDs
await namespace.fetch(["id-1", "id-2"]);
```

## Metadata Filtering
Expand Down
4 changes: 2 additions & 2 deletions src/commands/client/namespace/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe("NAMESPACE", () => {
metadata: { namespace: "test-namespace-reset" },
});

sleep(1000);
sleep(5000);

const res = await namespace.query({
vector: range(0, 384),
Expand All @@ -66,7 +66,7 @@ describe("NAMESPACE", () => {

await namespace.reset();

sleep(1000);
sleep(5000);

const res2 = await namespace.query({
vector: range(0, 384),
Expand Down
2 changes: 2 additions & 0 deletions src/commands/management/namespaces/delete/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ describe("NAMESPACES->DELETE", () => {

await new DeleteNamespaceCommand("test-namespace-delete").exec(client);

sleep(2000);

const namespacesAfterDelete = await new ListNamespacesCommand().exec(client);

expect(namespacesAfterDelete).not.toContain("test-namespace-delete");
Expand Down

0 comments on commit 7567b73

Please sign in to comment.