How to search for a word across all fields in Sanity documents?

16 replies
Last updated: Aug 9, 2021
Can anyone help me how can i get All articles which has the word "demo" in article document. Here I have article document in that i have title, description, body, source etc i want that "demo" word is searched inside whole Article document and would return me all articles which has demo present in any field.
AI Update

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 thread
16 replies
HI - you can find articles with the string demo in the body field (assuming it's blocks) like this
*[body[].children[].text match "demo"]
, but you would need to use a series of filters to catch "demo" in other fields as well, with something like

*[body[].children[].text match "demo" || title match "demo"]
though that could get cumbersome. What about a boolean field called "demo":
https://www.sanity.io/docs/boolean-type ?
HI - you can find articles with the string demo in the body field (assuming it's blocks) like this
*[body[].children[].text match "demo"]
, but you would need to use a series of filters to catch "demo" in other fields as well, with something like

*[body[].children[].text match "demo" || title match "demo"]
though that could get cumbersome. What about a boolean field called "demo":
https://www.sanity.io/docs/boolean-type ?
On top of what Joseph said, the docs on Query Scoring and GROQ Parameters may be relevant as you’re working on this.
HI - you can find articles with the string demo in the body field (assuming it's blocks) like this
*[body[].children[].text match "demo"]
, but you would need to use a series of filters to catch "demo" in other fields as well, with something like

*[body[].children[].text match "demo" || title match "demo"]
though that could get cumbersome. What about a boolean field called "demo":
https://www.sanity.io/docs/boolean-type ?
if i use like this then is it fine? *[_type == "article"] | ["Times of India" in [title,publicdescription,body[].children[].text,source]]
can you give brief whats difference of using "in" and "match" when i tried using match it was causing me error. I dont know why
in
will look for an operand in an array or path . It won’t work in your case as title, publicdescription, source, etc. are probably not arrays but string fields.

match
is a text search operator and would be the operator I would use here. The array traversal might cause an issue so I would use the pt::text() function to return a string instead of traversing through body[] and children[]:

*[_type == 'article'][[title, publicdescription, pt::text(body), source] match "Times of India"]
On using above query i m getting error.
Any chance you're using v1 of the GROQ API? To use the
pt::text()
function you'll need to use v2021-03-25 or later.
My word is not getting search in body which is rich text other all fields its works perfectly.Even though i changed the version from v1 to v20121-03-25 its gives same error.
Interesting. When you run this query do you get an array of objects containing the conjoined content from each document’s rich text field?

*[_type == 'article'] {
	'richText': pt::text(body)
}
Interesting. When you run this query do you get an array of objects containing the conjoined content from each document’s rich text field?

*[_type == 'article'] {
	'richText': pt::text(body)
}
Thanks for help
Thanks for help
No problem. Were you able to get things working?
Yes its working with some 10-15sec delay but it works for me thanks

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.

Was this answer helpful?