Querying Vectors

1. Performing Similarity Query

Example: Basic Query

res = index.query(
    vector=[0.1, 0.2, ...],
    top_k=10,
    include_metadata=True,
)
for match in res["matches"]:
    print(match["id"], match["score"])

2. Querying with Vector Values

ParamTypeNotes
vectorList[float]Must match index dim
sparse_vector{indices, values}Hybrid only
idstrAlternative to vector
top_kint1–10000

3. Querying by ID

Example: Query by ID

res = index.query(id="doc_42", top_k=5)
# Returns vectors similar to the vector stored under id=doc_42
BehaviorDetail
Looks upInternal vector for given ID
Self-matchThe ID itself appears as top result
Missing IDReturns empty matches

4. Setting Top K Results

top_kUse Case
1Best match (recommendation)
5–10RAG retrieval
50–100Re-ranking pipeline
10000Max (paginate beyond)

5. Including Metadata in Results

Example: With Metadata

res = index.query(
    vector=qv,
    top_k=5,
    include_metadata=True,
)
for m in res["matches"]:
    print(m["metadata"]["title"])
ParamDefault
include_metadataFalse
include_valuesFalse

6. Including Values in Results

Warning: include_values=True dramatically increases response size. Only enable when needed.
Use CaseRecommend
Re-rank locallyYes
Display resultsNo
Pipeline retrievalUsually no

7. Querying Specific Namespace

Example: Namespaced Query

res = index.query(
    vector=qv,
    top_k=5,
    namespace="tenant-a",
)
BehaviorDetail
DefaultEmpty namespace ""
Cross-namespaceRun N queries and merge

8. Understanding Match Scores

MetricScore Field Meaning
cosineSimilarity in [-1, 1]; higher = closer
dotproductRaw dot; higher = closer
euclideanDistance; lower = closer (API still sorts best-first)

9. Handling Query Timeouts

Example: Timeout Handling

try:
    res = index.query(vector=qv, top_k=10, timeout=5)
except Exception as e:
    # fallback / log / retry
    pass
CauseFix
Cold startPre-warm with a dummy query
Large top_kReduce top_k
Complex filterPre-filter / simplify metadata

10. Optimizing Query Performance

LeverEffect
Lower top_kFaster, smaller payload
Skip metadata/valuesSmaller response
Filter on indexed fieldsReduces candidate set
Co-locate regionSub-10ms network RTT
Connection reuseHTTP keep-alive / pool