Manage Data

Overview

In addition to inserting and querying data, you can also fetch an item ((id, vector) pair) by id, update an item’s vector by id, delete an item by id, or get information about the index.

Setup

Create an index and insert data into the index.

import pinecone
import pandas as pd
import numpy as np

pinecone.init(">>>YOUR_API_KEY<<<")

df = pd.DataFrame(data={
    "id": [f"id-{ii}" for ii in range(10000)],
    "vector": [ii + np.zeros(2) for ii in range(10000)]
})

pinecone.create_index("pinecone-index", metric="euclidean")
index = pinecone.Index("pinecone-index")

index.upsert(items=zip(df.id, df.vector))

Get Index Information

index.info()

# InfoResult(index_size=10000)

Fetch

Fetch items by their ids.

index.fetch(ids=["id-1", "id-2"])

# [FetchResult(id='id-1', vector=array([0.99999994, 0.99999994], dtype=float32)),
#  FetchResult(id='id-2', vector=array([1.9999999, 1.9999999], dtype=float32))]

Fetch one item at a time.

index.unary_fetch(id="id-3")

# FetchResult(id='id-3', vector=array([3., 3.], dtype=float32))

Update

Updating an item by id is done the same way as inserting items. (Write operations in Pinecone are idempotent.)

Let’s update the value of the item ("id-3", [3., 3.]):

index.unary_upsert(item=("id-3", 3.3 + np.zeros(2)))

Now fetch the item again and we should get ("id-3", [3.3, 3.3]):

index.unary_fetch(id="id-3")

# FetchResult(id='id-3', vector=array([3.3, 3.3], dtype=float32))

Delete

Delete items by their ids.

DeleteResult(id='id-1') is an acknowledgement that the item with id='id-1' has been deleted from the index.

acks = index.delete(ids=["id-1", "id-2"])
print(acks)

# [DeleteResult(id='id-1'), DeleteResult(id='id-2')]

Delete one item at a time.

index.unary_delete(id="id-3")

# DeleteResult(id='id-3')

Delete the Index

Free up computing resources and delete all of the data.

pinecone.delete_index("pinecone-index")