Every database needs data, and your vector database is no exception.

Our SDK makes it easy to insert or update (upsert) vector data in your indexes and/or namespaces.

Upsert

With Upstash Vector, you can upsert one or more vectors. For now, let’s focus on upserting a single vector.

We’ll use the upsert() method to instruct the SDK to upsert vectors into an index, as shown below:

use Upstash\Vector\Index;
use Upstash\Vector\VectorUpsert;

use function Upstash\Vector\createRandomVector;

$index = new Index(
  url: "<UPSTASH_VECTOR_REST_URL>",
  token: "<UPSTASH_VECTOR_REST_TOKEN>",
);

$index->upsert(new VectorUpsert(
  id: '1',
  vector: createRandomVector(dimensions: 1536)
));

You can also enhance your index by adding metadata, enabling more efficient filtering in the future.

You can read more about Metadata, Data and Metadata Filtering on our docs.

Upsert Many

Building on the previous section, Upstash Vector also supports upserting multiple vectors at once.

To do this, we’ll use the upsertMany() method, which allows you to efficiently insert or update multiple vectors into an index, as shown below:

use Upstash\Vector\Index;
use Upstash\Vector\VectorUpsert;

use function Upstash\Vector\createRandomVector;

$index = new Index(
  url: "<UPSTASH_VECTOR_REST_URL>",
  token: "<UPSTASH_VECTOR_REST_TOKEN>",
);

$index->upsertMany([
  new VectorUpsert(
    id: '1',
    vector: createRandomVector(dimensions: 1536)
  ),
  new VectorUpsert(
    id: '2',
    vector: createRandomVector(dimensions: 1536)
  ),
]);

Upserting multiple records simultaneously improves performance by allowing you to batch your upserts efficiently.

For optimal results, we recommend limiting each batch to no more than 1,000 records at a time.

Update

When you upsert data you are basicly overriding the data that is already in the index. If you want to update the data you can use the update method.

The update method is similar to the upsert method, but it will only update the data that is already in the index.

use Upstash\Vector\Index;
use Upstash\Vector\VectorUpdate;
use Upstash\Vector\Enums\UpdateMode;

$index = new Index(
  url: "<UPSTASH_VECTOR_REST_URL>",
  token: "<UPSTASH_VECTOR_REST_TOKEN>",
);

$index->update(new VectorUpdate(
  id: '1',
  metadata: ['foo' => 'baz'],
  metadataUpdateMode: UpdateMode::OVERWRITE,
));

Sparse Indexes

If you are using a sparse index, you’ll need to modify your upsert call accordingly.

Sparse indexes require a set of indices and their corresponding values, which can be upserted as demonstrated below:

use Upstash\Vector\Index;
use Upstash\Vector\VectorUpsert;
use Upstash\Vector\SparseVector;

$index = new Index(
  url: "<UPSTASH_VECTOR_REST_URL>",
  token: "<UPSTASH_VECTOR_REST_TOKEN>",
);

$index->upsert(new VectorUpsert(
  id: '1',
  sparseVector: new SparseVector(
    indices: [0, 1],
    values: [1.0, 2.0],
  ),
));

You can read more about Sparse Indexes on our docs.

Hybrid Indexes

If you are using a hybrid index, you need to provide both sparse vectors and dense vectors.

use Upstash\Vector\Index;
use Upstash\Vector\VectorUpsert;
use Upstash\Vector\SparseVector;

use function Upstash\Vector\createRandomVector;

$index = new Index(
  url: "<UPSTASH_VECTOR_REST_URL>",
  token: "<UPSTASH_VECTOR_REST_TOKEN>",
);

$index->upsert(new VectorUpsert(
  id: '1',
  vector: createRandomVector(dimensions: 1536),
  sparseVector: new SparseVector(
    indices: [0, 1],
    values: [1.0, 2.0],
  ),
));

You can read more about Hybrid Indexes on our docs.