Index Lifecycle: Creating and Managing Pinecone Indexes
An Index is the primary database structure in Pinecone. It stores vector embeddings, organizes metadata, and runs search queries. Let us review the index lifecycle.
1. Installation and Client Initialization
Install the official Node.js client package dependency:
# Install Pinecone SDK
npm install @pinecone-database/pineconeInstantiate the client in your code:
// src/lib/pinecone.ts
import { Pinecone } from "@pinecone-database/pinecone";
const apiKey = process.env.PINECONE_API_KEY;
if (!apiKey) {
throw new Error("Missing PINECONE_API_KEY environment variable");
}
// Export singleton instance
export const pc = new Pinecone({ apiKey });2. Creating an Index (Serverless Mode)
To store vector embeddings, create an index and define its dimensions and distance metric (such as cosine, euclidean, or dotproduct):
import { pc } from "./pinecone";
async function setupSearchIndex() {
console.log("Creating a new Pinecone index...");
await pc.createIndex({
name: "knowledge-base",
dimension: 1536, // Must match the embedding model (e.g. OpenAI text-embedding-3-small)
metric: "cosine", // Metric options: cosine, euclidean, dotproduct
spec: {
serverless: {
cloud: "aws", // Cloud provider: aws, gcp
region: "us-east-1", // Cloud region
},
},
});
console.log("Index created successfully!");
}3. Index Status and Deletion
You can retrieve index status metadata or delete an index when it is no longer needed:
// A. Describe index configurations
const description = await pc.describeIndex("knowledge-base");
console.log("Dimensions:", description.dimension);
console.log("Status:", description.status.ready);
// B. List all index names
const indexList = await pc.listIndexes();
console.log("Active indexes:", indexList.indexes);
// C. Delete an index
await pc.deleteIndex("knowledge-base");
console.log("Index dropped successfully.");Published on Last updated: