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, you will learn about the create_index, delete_index, and the list_indexes() methods that are provided by the Pinecone client for Python.
NOTE: This tutorial demonstrates how to work with the Pinecone database using the Pinecone client for the Python programming language. Hence, ensure that you have an installed Python 3.10 and above on your macine.
We also assume that you have a basic Pinecone project setup for demonstration purposes. You can create one by checking the Pinecone cloud console.
Install the Pinecone Client
The first step is to ensure that you have the Pinecone client for Python that is installed on your system. You can do this by running the “pip” command that is provided in the following:
This should download the latest stable version of the Pinecone-client package and install it on your machine.
Create an Index in Pinecone
As you can guess, the Pinecone-client package provides the create_index() method that allows us to create a new index in the Pinecone cluster.
The method syntax is as follows:
The function parameters are described as follows:
name – This specifies the name of the index that you wish to create. The maximum length of the index is set to 45 characters.
dimension – This specifies the dimension of the vectors to be inserted in the index.
metric – This is an optional parameter that specifies the distance metric that is used in the vector similarity search. Accepted distances include “euclidean”, “cosine”, and “dotproduct”.
pods – This is an optional integer parameter that defines the number of pods that the index will use. This value includes the number of replicas.
replicas – This sets the number of replicas that are used by the index.
pod_type – This parameter sets the type of pods for the index. The supported pod types include “s1”, “p1”, and “p2”.
metadata_config – This specifies the configuration for Pinecone’s internal metadata index behavior.
source_collection – This sets the collection name from which to create the index.
Example Usage
The following example demonstrates how to use the create_index() function to create a basic index:
# init pinecone configuration
pinecone.init(api_key="YOUR_API_KEY", environment="YOUR_ENVIRONMENT"
# create basic index
pinecone.create_index("sample", dimension=8)
The previous code creates an index called “sample” with a dimension of 8.
Delete an Index in Pinecone
As you can guess, we also have access to the delete_index() method that allows us to remove an existing index from the server.
The method syntax is as follows:
The method accepts the name of the index that you wish to remove as the parameter.
For example, to remove the sample-index that we created earlier, we can run the code as follows:
pinecone.init(api_key='YOUR_API_KEY', environment='YOUR_ENVIRONMENT')
pinecone.delete_index("sample-index")
Similarly, remember to replace the API_KEY and ENVIRONMENT parameters to match your project.
List the Indexes in Pinecone
Once done, you can use the list_indexes() method to list all the indexes in the server as demonstrated in the following:
pinecone.init(api_key='YOUR_API_KEY', environment='YOUR_ENVIRONMENT')
active_indexes = Pinecone.list_indexes()
This should return the available indexes in your cluster.
Conclusion
You learned how to use the Pinecone client to create an index, delete an index, and list the indexes in the Pinecone server.