RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Eruo Fredoline
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /Vector Stores and Embeddings
  6. /Ch. 6
Vector Stores and Embeddings

06. Similarity Search

Chapter 6 of 18 · 20 min
KEY INSIGHT

Query the collection with natural language and retrieve the most semantically similar documents. The `query` method takes a query text (or pre-computed query vector) and returns the `n` most similar results. ```python import chromadb client = chromadb.PersistentClient(path="./chroma_db") collection = client.get_collection(name="docs") # Query for semantically similar documents results = collection.query( query_texts=["I cannot log into my account"], n_results=3 ) print("Query results:") for i, (doc, distance, doc_id) in enumerate(zip( results['documents'][0], results['distances'][0], results['ids'][0] )): print(f"\n{i+1}. [ID: {doc_id}, Distance: {distance:.4f}]") print(f" {doc}") ``` Output: ``` Query results: 1. [ID: doc2, Distance: 0.2341] Password reset not working after email change 2. [ID: doc1, Distance: 0.3122] How to reset a forgotten password 3. [ID: doc3, Distance: 0.5891] Contact customer support for account recovery ``` The distance metric depends on how the collection was configured. Default is squared L2 (Euclidean distance). Lower distance means more similar. You can also query with pre-computed embeddings: ```python from sentence_transformers import SentenceTransformer model = SentenceTransformer('all-MiniLM-L6-v2') query_embedding = model.encode("I cannot log into my account") results = collection.query( query_embeddings=[query_embedding.tolist()], n_results=3 ) ``` Returning results includes metadata if you included it during insertion: ```python results = collection.query( query_texts=["security best practices"], n_results=2, include=["documents", "metadatas", "distances"] ) for doc, meta, dist in zip( results['documents'][0], results['metadatas'][0], results['distances'][0] ): print(f"Document: {doc}") print(f"Metadata: {meta}") print(f"Distance: {dist:.4f}\n") ```

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE

Populate a collection with 20 sentences about various topics. Query for three different concepts and verify the top results are semantically relevant.

← Chapter 5
Adding Documents
Chapter 7 →
Metadata Filtering