Querying and Filtering in Pinecone
What you'll learn: How to perform similarity searches in Pinecone with metadata filters and understand what the results tell you.
The Core Idea
Now that you can insert vectors into Pinecone, it's time to retrieve relevant results. Querying in Pinecone means providing a query vector and getting back the most similar vectors from your index. You can also narrow down results using metadata filters, combining semantic similarity with attribute-based constraints.
Query Parameters
When you query Pinecone, you specify:
vector: Your query embedding (the vector you're searching with)top_k: How many similar results you want backfilter: Optional metadata conditions (e.g.,{"category": "electronics"})include_metadata: Whether to return stored metadata with resultsinclude_values: Whether to return the actual vectors
Think of it like searching a library: the vector finds books by content similarity, while filters act like "only show me books published after 2020" or "only in the fiction section."
Response Structure
Pinecone returns a dictionary with a matches list. Each match contains:
id: The vector's unique identifierscore: Similarity score (higher = more similar)metadata: Associated data (if requested)values: The vector itself (if requested)
The matches are automatically sorted by score, so the first result is always the most similar.
Filtering Strategy
Pinecone applies pre-filtering, meaning it filters by metadata before performing similarity search. This ensures your top-k results all satisfy the filter conditions, not just the top-k from all vectors filtered afterward.
Key Takeaway: Pinecone queries combine vector similarity with metadata filtering to retrieve relevant results efficiently, returning ranked matches with scores and optional metadata that help you understand why each result was selected.