> ## Documentation Index
> Fetch the complete documentation index at: https://upstash-vector.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Api Reference

## Query

### `constructor(options: QueryOptions)`

Create a new query instance.

```ts
const q = new Query({
    redis: new Redis(...),
    encoderDecoder: // optional, defaults to JSON.stringify/JSON.parse
});
```

### `.createCollection(name: string)`

Create a new collection by giving it a name and document type.

```ts
const users = q.createCollection<User>("users");
```

## Collection

### `.set(id: string, data: T): Promise<void>`

Insert a new document to the collection.
This will throw an error if the document already exists.

```ts
await users.set(userId, user)
```

### `.get(id: string): Promise<Document<T> | null>`

Get a document by id.

```ts
const document = await users.get(userId)
// {
//     id: userId,
//     ts: 000, // the timestamp when created or last updated
//     data: {} // the data you have stored
// }
```

### `.delete(id: string): Promise<void>`

Delete a document by id.

```ts
await users.delete(userId)
```

### `.update(id: string, data: T): Promise<void>`

Update a document by id.
This will throw an error if the document does not exist.

```ts
user.name = "New Name"
await users.update(userId, user)
```

### `.createIndex(options: CreateIndexOptions<T>): Index<T>`

Create a new index on the collection.
The terms field will be strongly typed depending on the type you have passed in when creating the collection.

```ts
const usersByOrganization = users.createIndex({
    name: "users_by_organization",
    terms: ["organization"],
});
```

## Index

### `.match(query): Promise<Document<T>[]>`

Search for matches in the index.
The `query` argument is strongly typed depending on the terms you have passed in when creating the index.

```ts
const upstashEmployees = await usersByOrganization.match({ 
    organization: "Upstash" 
  });

  /**
   * [
   *     {
   *         id: "documentId",
   *         ts: 000, // the timestamp when created or last updated
   *         data: {
   *             id: "chronark",
   *             name: "Andreas Thomas",
   *             organization: "Upstash",
   *             email: "andreas@upstash.com"
   *         }
   *     }
   * ]
   */
```

### `.reIndex(): Promise<void>`

Rebuild the index. This might be necessary if you are adding an index after documents are already in the collection.
`reIndex` will go through the entire collection and rebuild the index, depending on the size of the collection this might take a while.

### `.delete(): Promise<void>`

Delete the index and all its data. The items in the collection will not be affected.
