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

9 replies
Last updated: Jan 28, 2026
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:

  1. The score() function computes a relevance score for each document based on your criteria
  2. The boost() function assigns different weights to each condition - in this case, title matches get a weight of 4, while subtitle matches get a weight of 1
  3. Results are ordered by the computed _score field in descending order, so title matches appear first

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

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?