What Is an Index in Pinecone?
In Pinecone, an index is a critical component of the functionality of the database. It provides a highly optimized data structure for organizing and optimizing the storage and querying of a high-dimensional vector data.
In this tutorial, we will learn how to use the Node.js Pinecone client to connect to a Pinecone client and create a new index.
Requirements:
To follow along with this post, ensure that you have the following:
- Installed Node.js 17 and above
- A configured Pinecone cluster
Installing the Pinecone Client
The first step is ensuring that the Pinecone client for Node.js is installed on the machine. We can do this by running the following command:
Once installed, we can proceed and learn how to configure Pinecone with Node.js.
Initializing a Client
Before interacting with the Pinecone database, we must create a client with the server configuration using the API key and the environment properties.
The following code shows how to use the “PineconeClient” and the init method:
const pinecone = new PineconeClient();
await pinecone.init({
environment: "us-west1-gcp-free",
apiKey: "0f57b6af-ea59-4fd3-a0ce-3c7f0c1d419f"
});
In this case, we initialize a new Pinecone client using the provided environment and API Key.
Create an Index in Pinecone Using Node.js
Once connected to the server, we can proceed and create an index to store the target data. The Node.js client provides us with the CreateIndex() method which enables us to quickly configure a new index as shown in the following example code:
createRequest: {
name: "sample-index",
dimension: 8,
metric: "cosine"
}
});
In the given example, we use the createIndex() method to create an index called “sample-index” with a dimension of 8 and a cosine distance metric.
List the Indexes
Once we create an index, we can use the listIndexes() method to list all the available indexes in that server as follows:
console.log(indexesList)
This should get a list of all available indexes from the server. An example output is as follows:
Pinecone Index Information
You can also use the “DescribeIndex” method to log all the information about the created index as demonstrated in the following:
indexName: "sample-index",
});
console.log(indexDescription)
An example output is as follows:
database: {
name: 'sample-index',
dimensions: undefined,
indexType: undefined,
metric: 'cosine',
pods: 1,
replicas: 1,
shards: 1,
podType: undefined,
indexConfig: undefined,
metadataConfig: undefined
},
status: { ready: true, state: 'Ready' }
}
Delete an Index in Pinecone
Once you are done with the index, you can remove the index using the “DeleteIndex” method. An example is as follows:
indexName: "sample-index",
});
This should remove the specified index from the server.
Conclusion
You learned how to work with the Pinecone client for Node.js to connect to the Pinecone server, create a new Pinecone index, list the available indexes, gather an index information, and remove the index from the server.