-
Notifications
You must be signed in to change notification settings - Fork 0
Smart Search Quick Reference
github-actions[bot] edited this page Sep 2, 2026
·
46 revisions
# 1. Run migration
cd .backend && nself db migrate up
# 2. Set environment variable
export OPENAI_API_KEY=sk-...
# 3. Start worker (in code or separate process)import { startEmbeddingWorker } from '@/lib/workers/embedding-worker'
await startEmbeddingWorker({ batchSize: 20, pollIntervalMs: 10000 })import { getSmartSearch } from '@/lib/ai/smart-search'
const search = getSmartSearch()
const results = await search.search('how to deploy', messages, {
limit: 20,
threshold: 0.7,
})import { getVectorStore } from '@/lib/database/vector-store'
const vectorStore = getVectorStore()
// Search
const results = await vectorStore.searchSimilar('query text', {
similarityThreshold: 0.7,
limit: 20,
channelId: 'abc-123',
})
// Find similar
const similar = await vectorStore.findSimilarMessages('msg-id', {
threshold: 0.8,
limit: 10,
})
// Queue for embedding
await vectorStore.queueEmbedding('msg-id', priority)import { createFilterBuilder } from '@/lib/search/filters'
const { sql, params } = createFilterBuilder()
.query('deployment')
.after(new Date('2024-01-01'))
.fromUsers(['alice', 'bob'])
.hasAttachments(true)
.semantic(0.75)
.limit(50)
.buildQuery()# Generate embeddings
POST /api/ai/embed
{
"texts": ["message 1", "message 2"]
}
# Search
POST /api/ai/search
{
"query": "deployment",
"messages": [...],
"options": { "limit": 20, "threshold": 0.7 }
}
# Suggestions
GET /api/search/suggestions?q=deploy&limit=5
# Worker status
GET /api/workers/embeddings
# Start/stop worker
POST /api/workers/embeddings
{
"action": "start",
"config": { "batchSize": 20 }
}-- Semantic search
SELECT * FROM nchat_search_messages_semantic(
'[...]'::vector(1536), -- query embedding
0.7, -- threshold
20, -- limit
'channel-id'::uuid, -- channel filter
NULL, -- user filter
NULL, -- date from
NULL, -- date to
FALSE -- include deleted
);
-- Find similar
SELECT * FROM nchat_find_similar_messages(
'message-id'::uuid,
0.8, -- threshold
10 -- limit
);
-- Queue stats
SELECT * FROM nchat_embedding_queue_stats();
-- Coverage stats
SELECT * FROM nchat_embedding_coverage;# Basic
deployment issues
# Filters
from:alice in:engineering after:2024-01-01
has:link has:file is:pinned
// Worker stats
const worker = getEmbeddingWorker()
const stats = worker.getStats()
// Queue stats
const vectorStore = getVectorStore()
const queueStats = await vectorStore.getQueueStats()
// Coverage
const coverage = await vectorStore.getCoverageStats()
console.log(`Coverage: ${coverage.coveragePercentage}%`)- Use batch operations - 50-100 embeddings per batch
- Enable caching - 70-90% hit rate saves costs
- Filter before searching - Reduces comparisons
- Use HNSW index - Faster than IVFFlat
- Adjust threshold - Higher = faster but fewer results
const service = getEmbeddingService()
const stats = service.getStats()
console.log(`Tokens: ${stats.totalTokens}`)
console.log(`Cost: $${stats.totalCost.toFixed(4)}`)
console.log(`Hit rate: ${(stats.hitRate * 100).toFixed(1)}%`)text-embedding-3-small: $0.02/1M tokens
- 1K messages (~50 tokens each) = $0.001
- 10K messages = $0.01
- 100K messages = $0.10
- With 90% cache = ~10% actual cost
# Check pgvector
psql -c "SELECT * FROM pg_extension WHERE extname = 'vector';"
# Check index
psql -c "SELECT indexname FROM pg_indexes WHERE tablename = 'nchat_messages' AND indexname LIKE '%embedding%';"
# Queue status
curl http://localhost:3000/api/workers/embeddings
# Restart worker
curl -X POST http://localhost:3000/api/workers/embeddings \
-H "Content-Type: application/json" \
-d '{"action": "stop"}'
curl -X POST http://localhost:3000/api/workers/embeddings \
-H "Content-Type: application/json" \
-d '{"action": "start", "config": {"batchSize": 20}}'.backend/migrations/
028_pgvector_semantic_search.sql # Database schema
src/lib/ai/
embeddings.ts # Embeddings service
smart-search.ts # Search engine (existing)
src/lib/database/
vector-store.ts # pgvector integration
src/lib/search/
filters.ts # Advanced filters
src/lib/workers/
embedding-worker.ts # Background processor
src/app/api/ai/
embed/route.ts # Embeddings API
search/route.ts # Search API (existing)
src/app/api/search/
suggestions/route.ts # Suggestions API
src/app/api/workers/
embeddings/route.ts # Worker management
docs/
Smart-Search-System.md # Full documentation
Smart-Search-Quick-Reference.md # This file
# Required
OPENAI_API_KEY=sk-...
# Optional
DATABASE_URL=postgresql://...
NEXT_PUBLIC_EMBEDDING_MODEL=text-embedding-3-small
# Worker config (in code)
{
batchSize: 20, # Messages per batch
pollIntervalMs: 10000, # Poll every 10s
idleDelayMs: 60000, # Wait 60s when idle
maxRetries: 3 # Retry failed 3x
}- Coverage > 95%
- Cache hit rate > 80%
- Search latency < 100ms (p95)
- Queue pending < 1000
- Failed rate < 1%
- Monthly cost < budget
- Full docs:
docs/Smart-Search-System.md - Implementation:
.claude/implementation/smart-search-implementation.md - Database: Check migrations and functions
- API: Test endpoints individually
- Worker: Check logs and stats
Smart Search v0.7.0 | Quick Reference
nself-chat v0.3.0 | GitHub | Issues | Discussions | Demo
Edit this page | MIT License | Β© 2026
(See π Security section below for 2FA, PIN Lock, and security audits.)
(Search lives in π Reference below.)
- π¬ Advanced Messaging
- π E2EE Setup
- π Search Setup
- π Call Management
- πΊ Live Streaming
- π₯οΈ Screen Sharing
- πΉ Video Calling
- ποΈ Voice Calling
- π± Mobile Optimization
- π§ͺ Testing
- π i18n
- π API Overview
- π Complete Reference
- π» API Examples
- π€ Bot API
- π Auth API
- π GraphQL Schema
- π Deployment Overview
- π³ Docker
- βΈοΈ Kubernetes
- β Helm Charts
- β Production Checklist
- π Production Validation
- π’ Multi-Tenant
- ποΈ Architecture
- π Diagrams
- ποΈ Database Schema
- π Project Structure
- π TypeScript Types
- π SPORT Reference
- π 2FA
- π¬ Messaging
- π Call Management
- π Call State Machine
- π E2EE
- πΊ Live Streaming
- π± Mobile Calls
- π PIN Lock
- π Polls
- π₯οΈ Screen Sharing
- π Search
- π Social Media
- ποΈ Voice Calling
- π Security Overview
- π‘οΈ Security Audit
- β‘ Performance
- π Best Practices
- π 2FA
- π PIN Lock
- π E2EE
- π‘οΈ E2EE Audit
v1.0.0 β’ 2026