Joint session with Vercel: How to build intelligent storefronts (May 15th)

Order Sanity search results by matching field priority (title before subtitle)

9 repliesLast updated: Nov 29, 2025

Hey, is there a way to order search results by some search term hierarchy? For example, let's say I have some objects that they have
title
and
subtitle
as properties. Querying them like
*[title match "whatever" || subtitle match "whatever"] {...}
. Is it possible to somehow order the results so, first will be the results that meet the first condition (
title match "whatever"
), and then the results that meet the second condition (
subtitle match "whatever"
)? Thanks
AI Update

Yes, you can definitely order search results by match priority using the score() and boost() functions in GROQ!

Here's how to prioritize title matches over subtitle matches:

*[title match "whatever" || subtitle match "whatever"]
| score(
  boost(title match "whatever", 4),
  boost(subtitle match "whatever", 1)
) 
| order(_score desc)

How it works:

The _score field is automatically added to each document when you use the score() function, and you can adjust the boost weights to whatever ratio makes sense for your use case. Higher numbers = higher priority in the results.

You can also use this pattern with variables to make it more reusable:

*[title match $term || subtitle match $term]
| score(
  boost(title match $term, 4),
  boost(subtitle match $term, 1)
) 
| order(_score desc)

This approach is super flexible - you can add as many scoring criteria as you need with different weights to fine-tune your search result ranking!

Show original thread
9 replies

Was this answer helpful?

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.

Related contributions