Run Phase - Visual Product Search
Describes how to implement visual product search using on-device CLIP embeddings and Couchbase Lite vector similarity queries.
What this file does
Describes how to implement visual product search using on-device CLIP embeddings and Couchbase Lite vector similarity queries.
When to use it
- Building a mobile app that lets users search products by photo
- Integrating vector search with Couchbase Lite in a React Native app
- Combining on-device ML inference with local vector databases
- Implementing hybrid search that blends full-text and vector queries
Assumes this stack
Run Phase - Visual Product Search
Overview
The "Run" phase implements visual product search - take a picture of an item and query the Couchbase Lite database using vector similarity to find matching products.
Architecture
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ Camera/Image │────▶│ ML Model (CLIP) │────▶│ Vector Query │
│ Input │ │ Generate Vector │ │ Couchbase Lite │
└─────────────────┘ └──────────────────┘ └─────────────────┘
│
▼
┌─────────────────┐
│ Similar Products│
│ (Top N) │
└─────────────────┘
Key Components
1. Image Embedding Generation
Convert images to vectors using CLIP or similar models that can run on-device.
Recommended Libraries:
- react-native-executorch - On-device AI inference (CLIP model support)
- react-native-fast-tflite - TensorFlow Lite for React Native with GPU acceleration
- @react-native-rag/executorch - RAG utilities with ExecuTorch integration
2. Couchbase Lite Vector Search
Use APPROX_VECTOR_DISTANCE() function to find similar vectors.
Vector Index Configuration:
const config = new VectorIndexConfiguration({
expression: 'embedding', // field containing the vector
dimensions: 512, // CLIP base model outputs 512 dimensions
centroids: 100, // ~sqrt(num_documents)
metric: 'cosine', // or 'euclidean'
});
await collection.createIndex('embedding_index', config);
Query Example:
SELECT META().id, *, APPROX_VECTOR_DISTANCE(embedding, $queryVector) AS distance
FROM catalog.vectors
WHERE APPROX_VECTOR_DISTANCE(embedding, $queryVector) < 0.5
ORDER BY APPROX_VECTOR_DISTANCE(embedding, $queryVector)
LIMIT 10
3. Hybrid Search
Combine vector search with full-text search for better results:
SELECT META().id, *
FROM catalog.products
WHERE MATCH(description, 'red shirt')
AND APPROX_VECTOR_DISTANCE(embedding, $queryVector) < 0.5
ORDER BY APPROX_VECTOR_DISTANCE(embedding, $queryVector)
LIMIT 10
Implementation Plan
Phase 1: Camera Integration
- Install
expo-cameraorreact-native-vision-camera - Create camera capture screen
- Allow photo selection from gallery
Phase 2: Image Embedding
- Install
react-native-executorchwith CLIP model - Load CLIP image encoder (352 MB model)
- Generate 512-dim embedding from captured image
Phase 3: Vector Query
- Create vector index on
catalog.vectorscollection - Implement
APPROX_VECTOR_DISTANCE()query - Display top matching products
Phase 4: UI/UX
- Create visual search screen with camera preview
- Show search results with similarity scores
- Add product detail view
Dependencies to Add
# Camera
npx expo install expo-camera expo-image-picker
# ML/Embeddings (choose one approach)
# Option A: ExecuTorch (recommended for CLIP)
npm install react-native-executorch @react-native-rag/executorch
# Option B: TensorFlow Lite
npm install react-native-fast-tflite
Model Options
CLIP (Recommended)
- Size: ~352 MB
- Dimensions: 512
- Inference Time: ~50-70ms on modern devices
- Pros: Same embedding space for text and images
- Source: https://huggingface.co/software-mansion/react-native-executorch-clip-vit-base-patch32
MobileNet V3
- Size: ~5-15 MB
- Dimensions: 1024
- Inference Time: ~20-30ms
- Pros: Smaller model size
- Cons: Image-only embeddings
Couchbase Lite Vector Search Reference
Documentation
Key Functions
APPROX_VECTOR_DISTANCE(vector-expr, target-vector, [metric])- Find approximate nearest neighborsMATCH(field, text)- Full-text search (can combine with vector search)
Vector Index Parameters
| Parameter | Description |
|---|---|
expression | Field containing the vector |
dimensions | Vector size (2-4096) |
centroids | Buckets for clustering (~sqrt(docs)) |
metric | Distance: cosine, euclidean, dot |
encoding | Compression: None, SQ (8-bit), PQ |
Data Flow
- User captures image via camera or gallery
- CLIP model processes image → 512-dim float array
- Query Couchbase Lite using
APPROX_VECTOR_DISTANCE() - Display matching products sorted by similarity
Considerations
Performance
- CLIP model is ~352 MB - consider lazy loading
- First inference may take 2x longer (model initialization)
- Cache model in memory for repeated searches
Accuracy
- CLIP works best with clear, well-lit images
- Training vectors should use same model as query
- Consider using cosine similarity for normalized vectors
Offline-First
- Model runs entirely on-device
- No network required for inference
- Vectors sync from Sync Gateway for catalog updates
What's inside
Architecture diagram, 4 key components, implementation plan with 4 phases, dependency commands, model options, vector index config, SQL examples, and data flow
Change this for your project
- Replace
catalog.vectorswith your actual Couchbase collection name - Replace
embeddingwith the field name storing your vectors - Replace
https://huggingface.co/software-mansion/react-native-executorch-clip-vit-base-patch32with your model source URL
Where it goes
Save in docs/ or the repository root. Gives agents and new contributors a map of the codebase.
Worth borrowing
- Using
APPROX_VECTOR_DISTANCE()with a threshold and ordering by distance for ranked results - Combining
MATCH()andAPPROX_VECTOR_DISTANCE()in a single SQL query for hybrid search - Structuring the implementation as four sequential phases: camera, embedding, query, UI
Related Documents
Design Document: BharatSeva AI
Describes a 10-agent AWS system that helps India's informal workers access government schemes via voice-first, serverless architecture.
OpenClaw Enterprise Transformation Plan
Transforms a single-user AI agent into a dual-mode platform supporting both viral open-source and Fortune 500 enterprise deployments through phased security, IAM, audit, multi-tenancy, and Kubernetes features.
Qwen Image and Edit: Open-sourcing and Local GGUF Generations with Lightning
Documents the Qwen-Image and Qwen-Image-Edit models, covering architecture, training, benchmarks, ComfyUI setup, and prompting techniques for local GGUF deployment.
University of Guelph Rocketry Club - Complete Tech Stack
Documents the full tech stack of a university rocketry club website with AI chatbot, member management, and project showcases.