Pinecone JS SDK Cheat Sheet
This quick reference guide summarizes the most common patterns and configurations in Pinecone using the official Node.js SDK.
1. Client Initialization and Index Operations
import { Pinecone } from "@pinecone-database/pinecone";
// 1. Initialize client instance
const pc = new Pinecone({ apiKey: "YOUR_API_KEY" });
// 2. Create a serverless index
await pc.createIndex({
name: "new-index",
dimension: 1536,
metric: "cosine",
spec: {
serverless: {
cloud: "aws",
region: "us-east-1",
},
},
});
// 3. Drop an index
await pc.deleteIndex("new-index");2. Ingesting Vectors (Upsert)
const index = pc.index("new-index");
// Upsert vectors with unique IDs and custom JSON metadata
await index.upsert([
{
id: "item_a",
values: [0.012, -0.045, 0.089], // Dimension values array
metadata: { category: "web", price: 29.99 },
},
{
id: "item_b",
values: [-0.005, 0.098, -0.012],
metadata: { category: "database", price: 14.50 },
},
]);3. Querying Similar Vectors with Metadata Filters
const index = pc.index("new-index");
// Execute a similarity search
const results = await index.query({
vector: [0.010, -0.040, 0.080],
topK: 5,
includeMetadata: true,
filter: {
category: { $eq: "web" },
price: { $lte: 30.00 },
},
});4. Querying inside a Namespace Partition
// Target a specific client namespace partition
const userSpace = pc.index("new-index").namespace("user-123");
// Query will search only vectors within user-123 partition
const userResults = await userSpace.query({
vector: [0.010, -0.040, 0.080],
topK: 3,
includeMetadata: true,
});Published on Last updated: