Primitives.org.ai

search

Hybrid vector + full-text search

Semantic search across all entities using embeddings and full-text indexing.

Import

import { DB } from 'ai-database'

const { db } = DB({
  Post: { title: 'string', content: 'markdown' },
})
const results = await db.Post.search('AI tutorials for beginners')

With Filters

const results = await db.Post.search({
  query: 'machine learning basics',
  where: { published: true },
  limit: 10,
})

Search across all types:

const results = await db.search('TypeScript best practices')
// Returns matches from Post, Author, Comment, etc.

Search Options

OptionTypeDescription
querystringSearch query
whereobjectFilter criteria
limitnumberMax results
offsetnumberSkip results
typesstring[]Types to search
thresholdnumberSimilarity threshold (0-1)

Search Results

interface SearchResult {
  $id: string
  $type: string
  score: number     // relevance score
  highlights: {     // matched snippets
    field: string
    snippet: string
  }[]
  data: unknown     // full entity data
}

Combines vector similarity and full-text matching:

const results = await db.Post.search({
  query: 'getting started with AI',
  mode: 'hybrid',  // 'semantic' | 'fulltext' | 'hybrid'
})

Embedding Configuration

const { db } = DB(schema, {
  embeddings: {
    model: 'text-embedding-3-large',
    dimensions: 3072,
  },
})

Re-embedding

When content changes, embeddings regenerate automatically:

await db.Post.update(id, { content: 'New content...' })
// Embedding recalculated in background

Find similar entities:

const similar = await db.Post.similar(post.$id, { limit: 5 })

Use Cases

// Content discovery
const related = await db.Post.search({
  query: currentPost.title,
  where: { $id: { $ne: currentPost.$id } },
  limit: 5,
})

// Recommendations
const forUser = await db.Post.search({
  query: user.interests.join(' '),
  where: { published: true },
  limit: 10,
})
Was this page helpful?

On this page