AI & Search
Foir includes built-in semantic search. It finds content by meaning rather than by exact keywords, and it runs on your records without any external machine-learning infrastructure.
Overview
Traditional search matches keywords. Semantic search matches meaning. Someone searching for “affordable running shoes” also finds content about “budget sneakers for jogging”, even when none of those words appear in it.
You turn embeddings on for a model. Foir keeps the vectors in step with your content from then on, and you query with natural language.
Your content Vector space
+--------------------------+ +--------------------------+
| "Lightweight running | embed | [0.12, -0.34, 0.87, ...] |
| shoes for beginners" | -----> | |
+--------------------------+ +--------------------------+
A query Ranked by similarity
+--------------------------+ +--------------------------+
| "beginner jogging gear" | -----> | 0.91 running shoes |
+--------------------------+ search | 0.85 starter kit |
+--------------------------+The part worth knowing before you read further: results are confined to the content the caller would actually be served. Their locale, their targeted variants, their own records. A hit never comes back for content the caller could not have read, and the similarity score is never computed from text they would never see.
Turning it on
Two levels, both in the console.
The model capability
Open Content > Models, pick the model, and switch on Embeddings in its settings. This is a model capability like versioning or publishing.
Like the rest of a model’s settings, the change takes effect when you publish the model. From then on Foir maintains embeddings for the records written after it, and turning the capability off, or changing which fields embed, likewise waits for the publish, so a draft never changes what search serves. It does not embed the records you already have. That is deliberate: backfilling an existing model is the most expensive thing this feature does, so it is priced and confirmed separately rather than happening as a side effect of a toggle. See Cost and backfill.
Inline models cannot have embeddings. Their content is embedded through the record that contains them.
Which fields are embedded
Each prose field gets an Embedded for semantic search switch in the schema builder, shown once the model’s capability is on.
The default is by field type, and the default is usually right:
| Field type | Embedded by default |
|---|---|
text, prose | Yes |
| Everything else | No, and no switch is offered |
Only prose is embedded. Slugs, ids, URLs, enum and select values, numbers, dates, booleans, colours and geo points are all excluded, and not as an oversight: mixing them in with prose measurably degrades ranking. Identifiers match on fragments, closed-vocabulary values are not sentences, and the rest carry nothing a text embedding can rank.
Structured fields (json, list, model), references and media are compositions or pointers rather than prose, so they are not embedded either. Their prose leaves opt in through their own schemas. A prose field embeds per passage run rather than whole, so a long document is retrievable by the part that matched.
The one case where you should override the default is a text field that holds an identifier. A slug field is a text field by type, so it embeds by default and should be switched off. That is what the per-field switch is for.
Set it in the schema builder, or in foir.config.ts so the override survives the next push:
fields: [
{ key: 'title', type: 'text' }, // embeds, by type default
{ key: 'slug', type: 'text', embed: false }, // an identifier, kept out of the index
]embed is tri-state: omit it to follow the type default, true to always embed, false to never. A config-as-code project should write the false down rather than setting it only in the console — the field is part of the model definition, so a push reconciles it like any other.
What gets embedded, and when
Which content
| Model | What is embedded |
|---|---|
| Publishing or versioning enabled | The published content. A record that has never been published, or has been unpublished, has nothing embedded and cannot match. |
| Everything else | The live row. |
This follows from what search should return. On a publishing model, a draft is not what a reader gets, so it is not what a search should rank.
When
After a record write commits, embedding runs in the background with a short delay, and repeated edits inside that window collapse into one job. Save a record five times while editing and it embeds once. Expect a searchable record within roughly half a minute of the write settling, not instantly.
A reconciliation sweep runs behind that, so records that predate the capability being switched on, or whose job was lost, get picked up rather than staying invisible.
If content has not changed since the last embedding, the work is skipped and costs nothing.
Locales and variants
Embeddings fan out across the axes your content actually uses: locale, record variant, field, and field variant.
The fan-out follows authored content only. A locale with no translation of its own, or a variant that inherits the parent’s value, gets no embedding of its own. At search time those resolve back to the base content, so nothing is missing from results. It just means you are not paying to embed the same sentence once per locale that never translated it.
Content that stops existing stops being searchable. Un-embedding a field, removing a translation, emptying a body, or switching the capability off all converge on the same outcome: the vectors go away.
Cost and backfill
Embedding spends AI credits, from the same balance as the rest of Foir’s AI features. It appears on your workspace’s usage page alongside everything else.
Two things keep the bill predictable.
Ongoing edits are cheap. Cost is metered per job rather than per record, edits collapse into one job, and unchanged content is skipped. A project editing normally spends very little here.
Backfilling is priced before it runs. Turning the capability on never backfills. To embed a model’s existing records you estimate first and then confirm, and the estimate reports:
- how many records the backfill covers
- how many already hold an embedding, since unchanged content re-embeds free
- approximate tokens
- the approximate AI-credit cost of the whole backfill
The estimate is deliberately high-side, so the real spend tends to come in under it rather than over. You can ask for it while the capability is still off, which is the point: it is what you weigh when deciding whether to turn it on at all.
In the console, open the model’s Embeddings tab in Content > Models, click Estimate backfill, then Start backfill and confirm the spend. The estimate and the backfill are also available over MCP (
foir_manage_embeddings, actionsbackfill_estimateandbackfill_start) and the platform API.
To see where a model stands, open its Embeddings tab in Content > Models. It reports embedded records against total, coverage, and how many are still pending. The tab appears once the capability is on or the model has at least one embedding. With nothing embedded yet, it offers the backfill.
Who is asking changes what comes back
This is the part that has no equivalent in other platforms’ semantic search, and it is worth stating plainly.
Every hit is resolved in the caller’s own context before it is returned:
- Access. A record the caller cannot read never appears. A user’s private records are invisible to other users. This holds on every transport, because the confinement happens on the platform rather than in any one client.
- Locale. A hit is returned only when the matched text is the value that resolves for the locale the caller asked for, field by field, exactly as a normal read would resolve it.
- Variants and targeting. A hit is returned only when the matched content is the variant the caller resolves to. Content targeted at another audience does not match for someone outside it.
A record that fails any of these is silently absent, indistinguishable from never having matched. That is intentional: whether content exists is itself something the caller is not entitled to know.
The corollary matters when you search for someone other than the caller. If you do not say who is asking, you get the base, default-audience view. A personalised or localised variant will never match, and nothing will tell you it did not. So pass the end user’s identity, locale and targeting context when the search is for them, and omit them when you genuinely want an unpersonalised view across the corpus.
Passing a user’s id personalises targeting. It cannot widen reach: it never unlocks another user’s private records, because reach comes from the credential making the call.
Querying from the API
Per-model search
Every model with embeddings gets its own typed search field, named from its key. A model keyed product gets searchProducts.
Required scope: search:read:<model>, for example search:read:product
query SearchProducts {
searchProducts(
query: "lightweight shoes for trail running"
first: 10
locale: "en"
) {
score
record {
_id
title
price
}
}
}record is the model’s normal GraphQL type, so you can select any of its fields.
The same search through the data client, where search is the model’s own operation:
const hits = await foir.product.search({
query: 'lightweight shoes for trail running',
first: 10,
locale: 'en',
});| Argument | Type | Required | Description |
|---|---|---|---|
query | String | Yes | Natural-language search query |
first | Int | No | Maximum hits to return (default 10) |
locale | String | No | Locale to search within |
| Result field | Type | Description |
|---|---|---|
score | Float | Similarity, higher is closer |
record | <Model> | The matching record, fully typed |
The caller’s targeting context and user identity come from the request itself, exactly as they do for any other read. There is no separate audience argument to remember.
Across models
searchRecords searches every model that has embeddings and returns generic rows rather than a typed record.
Required scope: search:read
query GlobalSearch {
searchRecords(
query: "how to set up two-factor authentication"
modelKey: "support_article" # optional; omit to search every model
first: 5
) {
recordId
modelKey
naturalKey
score
content
}
}const hits = await foir.ns.search.searchRecords({
query: 'how to set up two-factor authentication',
modelKey: 'support_article',
first: 5,
});| Argument | Type | Required | Description |
|---|---|---|---|
query | String | Yes | Natural-language search query |
modelKey | String | No | Limit to one model |
first | Int | No | Maximum hits to return |
modelKey is the only filter cross-model search takes.
Querying from an agent
An agent connected over MCP searches with foir_semantic_search, which takes three actions:
| Action | What it does |
|---|---|
by_text | Rank records against a natural-language query, embedded server-side |
by_vector | Rank records against a query vector you supply |
similar_to_record | Rank records against a source record’s own embedding |
similar_to_record resolves the source record for the audience you describe first, so the same record id searches from different content for different audiences.
The audience arguments are the same three ideas as above: pass user_id and locale when searching for an end user, pass targeting keys in context when the content has targeted variants, and omit all three for an unpersonalised corpus query. Omitting them is not an error, and it silently gives you the base view, so pass them when they matter.
foir_embedding_coverage reports how much of a model is embedded, including for a single record, which is how an agent confirms a record became searchable after a write.
Coverage and maintenance
A small set of user queries and mutations inspect and manage stored vectors directly. Reads require embeddings:read, writes require embeddings:write.
# Records still missing an embedding: the model's own list, narrowed.
# An empty slot means any slot; name one to check just that slot.
query Missing {
products(missingEmbedding: "", first: 50) {
edges {
node {
_id
_naturalKey
}
}
}
}
# Coverage for one model, or omit modelKey for every model
query Coverage {
embeddingCoverage(modelKey: "product") {
modelKey
totalRecords
embeddedRecords
pendingRecords
lastEmbeddedAt
}
}const missing = await foir.product.findMany({ missingEmbedding: '', first: 50 });
const coverage = await foir.ns.search.embeddingCoverage({ modelKey: 'product' });You rarely need to embed a record by hand, since writes are picked up automatically. When the automatic path missed a record, queue it yourself. It runs on the same delay as a write, about half a minute:
mutation EmbedRecord {
generateEmbedding(recordId: "rec_abc123", modelKey: "product")
}await foir.ns.search.generateEmbedding({ recordId: 'rec_abc123', modelKey: 'product' });It returns a Boolean saying whether the job was queued, and the job is skipped on the worker side when content has not changed. writeEmbeddings pushes a precomputed vector directly, for the case where you are generating vectors yourself.
generateEmbeddingandwriteEmbeddingsappear on your client only if your key holdsembeddings:write. A publishable key’s manifest lists thesearchreads alone, which makes the namespace look read-only. That is scope shaping rather than the namespace’s contents. See Namespace operations.
From the CLI
# Semantic search
foir search "lightweight running shoes" --first 10
foir search "return policy for damaged items" --models faq,support-article --first 5
# Inspect and manage vectors
foir embeddings stats product --json
foir embeddings stats --json
foir embeddings list rec_abc123
foir embeddings similar rec_abc123 --model-key product --first 5
foir embeddings write --file ./embedding.json
foir embeddings delete rec_abc123 --confirmSee foir embeddings for the full reference.
Errors
Errors arrive in the GraphQL response under extensions.code:
| Code | Meaning |
|---|---|
PERMISSION_DENIED | The key lacks the required scope |
FAILED_PRECONDITION | Embeddings are not configured for this project |
NOT_FOUND | Record or model not found |
INVALID_ARGUMENT | Invalid input |
Practical notes
- Turn the capability on before content exists where you can. A model that has embeddings from the start never needs a backfill.
- Switch off
embedon identifier-shaped text fields. A slug in the embedding is noise that costs money and hurts ranking. - Estimate before backfilling a large model. It is the one action here that can cost real money in a single step.
- Publish before expecting a hit. On a publishing model, a draft is not searchable, and this is the most common reason a record seems to be missing.
- Pass the audience when searching for a user. A missing
user_idor locale does not error, it just quietly searches the default view. - Narrow with
modelKeywhen you know which content is relevant.
Where it fits
- Product discovery. Let users describe what they want instead of navigating category trees, and have the results respect their locale and segment automatically.
- Help centre search. Let people ask questions in plain language against your support articles.
- Related content. Recommend by meaning rather than by tags, with
similar_to_recordorfoir embeddings similar. - Agent retrieval. An agent searching your content over MCP gets the same confinement a browser would, so it cannot surface content the person it is acting for could not read.