See Sanity in action 👀 Join us for a live product demo + Q&A →

Query Expansion for Matching Words in Post Body

3 replies
Last updated: Jan 15, 2023
I currently have a query that is for search that looks like
export const searchQuery = groq`
*[_type == 'post' && title match "*" + $searchTerm + "*"] | order(publishedAt desc, _updatedAt desc)[0..10]{
  ${litePostFields}
}
`
however, I want to expand it to allow matching words in the body of a post. What is the
best / most performant way to do that?
Jan 15, 2023, 12:03 AM
What is the body? String or Portable Text?
Jan 15, 2023, 1:28 PM
If it’s PT, you can use this function:
pt::text(body) match ...
Jan 15, 2023, 1:29 PM
Yeah it’s portable text
Jan 15, 2023, 1:31 PM

Sanity.io – build remarkable experiences at scale

Sanity is a customizable solution that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Categorized in