One of the major building blocks in Pinecone is an index. A pinecone index refers to a structure that is used by Pinecone to index and organize the vectors that are stored within that database. An index employs the advanced techniques such as approximate nearest neighbor algorithms, dimensionality reduction for search efficiency, and more.
Using an index in Pinecone facilitates rapid similarity searches by quickly identifying the vectors that are closest to a given query vector which facilitates an efficient information retrieval.
In this tutorial, we will walk you through the basics of working with indexes in a Pinecone. We will start by creating a basic index, adding a sample data, and gathering information about the index.
NOTE: This tutorial demonstrates how to work with the Pinecone database using the API endpoints using cURL or any other API client of your choice.
We also assume that you have a basic Pinecone project setup for demonstration purposes. You can create one by checking the Pinecone cloud console.
Create an Index in Pinecone
Let us start at the basics and create a simple index for our Pinecone client. For this tutorial, we will use the HTTP requests to the Pinecone APIs.
The following command shows how to use cURL to create an index called “sample-index” in Pinecone:
-H 'Api-Key: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"name": "sample-index",
"dimension": 128
}'
NOTE: Ensure to replace the previous URL with the URL to your Pinecone project. Similarly, provide the API key for your project which you can get from the Pinecone console dashboard.
Once you run the previous command, it creates an index with a single pod that allows you to perform an approximate nearest neighbor search using the cosine similarity.
Insert a Sample Data in Pinecone
Once we create the target index, we can proceed and insert the sample data into the index as shown in the following request:
-H 'Api-Key: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"vectors": [
{
"id": "A",
"values": [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
},
{
"id": "B",
"values": [0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2]
},
{
"id": "C",
"values": [0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3]
},
{
"id": "D",
"values": [0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4]
},
{
"id": "E",
"values": [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]
}
]
}'
The previous request should perform an upsert operation to the sample index that we created with the specified data. Replace the correct URL and API KEYs to match your target project.
Describe an Index in Pinecone
Once you define your index and added sample data, we can gather the information about the index by making a GET request to the target index.
An example request is shown in the following:
-H 'Api-Key: YOUR_API_KEY'
Once you execute the previous command, you should get a JSON response that contains the details of the specified index. An example response is as follows:
"database": {
"name": "sample-index",
"metric": "cosine",
"dimension": 128,
"replicas": 1,
"shards": 1,
"pods": 1
},
"status": {
"waiting": [],
"crashed": [],
"host": "sample-index-9daedfd.svc.us-west1-gcp-free.pinecone.io",
"port": 433,
"state": "Ready",
"ready": true
}
}
There you have it!
Conclusion
You learned how to use the Pinecone API endpoints to perform the index operations. You learned how to create an index, perform an upsert operation, and gather information about an existing index.