How to search for a word across all fields in Sanity documents?
To search for all articles containing the word "demo" across any field in your article document, you'll want to use GROQ's match operator. Since you want to search across multiple fields (title, description, body, source, etc.), you have a couple of approaches:
Option 1: Search specific fields explicitly
*[_type == "article" && (
title match "demo" ||
description match "demo" ||
source match "demo" ||
body match "demo"
)]This approach lists out each field you want to search. Add all the fields from your article schema where you want to search for "demo".
Option 2: Search within Portable Text (if your body is Portable Text)
If your body field uses Portable Text (Sanity's rich text format), you'll need to search within the text blocks:
*[_type == "article" && (
title match "demo" ||
description match "demo" ||
body[].children[].text match "demo" ||
source match "demo"
)]Option 3: Ranked search results with scoring
If you want to rank results by relevance (prioritizing matches in certain fields like title over body), you can use the score() function:
*[_type == "article"]
| score(
boost(title match "demo", 3),
boost(description match "demo", 2),
boost(body[].children[].text match "demo", 1)
)
| order(_score desc)This gives higher weight to matches in the title (3x), then description (2x), then body (1x).
Important note about match: The match operator performs tokenized text search, not exact string matching. It breaks text into words, so "demo" will match "This is a demo" or "demonstration" if tokenized that way. Special characters like @ and . are treated as word separators.
If you need to search across many fields dynamically or don't want to list them all, there isn't a built-in "search all fields" wildcard in GROQ, so you'll need to specify each field you want to include in your search.
Show original thread16 replies
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.