Prompt
what is the best way to search embeddings with metadata
Latest observation
The best way depends on your scale and filters, but the usual “best practice” is:
1) Store embeddings and metadata together in a vector database
Use a vector store that supports:
- vector similarity search on embeddings
- metadata filtering on fields like
source,date,user_id,category, etc.
Examples: Pinecone, Weaviate, Milvus, Qdrant, pgvector, Elasticsearch/OpenSearch with vector support.
2) Use metadata as a pre-filter when possible
If you know constraints up front, filter first and then search only within that subset.
Example:
- “Find similar documents for user 123”
- “Search only published docs from last 30 days”
This is usually faster and gives cleaner results than searching everything and filtering afterward.
3) Use hybrid retrieval for best quality
Combine:
- vector similarity for semantic meaning
- keyword/BM25 for exact terms
- metadata filters for structured constraints
This is often the best overall approach for search quality.
4) Index metadata fields you filter on often
For performance:
- keep frequently filtered fields indexed
- use low-cardinality fields carefully
- avoid overly complex nested filters unless your DB supports them efficiently
5) Keep metadata small and queryable
Good metadata fields:
doc_idtenant_idcreated_attagslanguagesourceauthor
Avoid storing large blobs in metadata if you need fast filtering.
6) Common query pattern
Typical flow:
- embed the user query
- apply metadata filters
- run nearest-neighbor search
- optionally rerank top results
Example conceptually:
{
"vector": [0.12, 0.98, ...],
"filter": {
"tenant_id": "acme",
"doc_type": "faq",
"created_at": { "$gte": "2025-01-01" }
},
"top_k": 10
}
7) If you’re using a relational DB
If your dataset is smaller or you already use Postgres:
- store embeddings in pgvector
- store metadata in normal columns
- use SQL
WHEREclauses for metadata filters plus vector distance search
This is often the simplest and most maintainable option.
Rule of thumb
- Small/medium scale: Postgres + pgvector + indexed metadata
- Large scale / high QPS: dedicated vector DB with metadata filtering
- Best search quality: vector + keyword + metadata filters + reranking
If you want, I can recommend the best setup for your specific stack (e.g. Postgres, Pinecone, Qdrant, Elasticsearch) and use case.