Ordering search results by search term hierarchy in GROQ

9 replies
Last updated: May 25, 2022
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!

It seems like the usage of
score()
will the way to go - haven’t used it myself, but i definitely think your query would go somewhat like this:
*[_type == "movie" && movieRating > 3] | score(
  boost(title match $term, 4),
  boost(body match $term, 1)
)
That's awesome, will give it a try. Many thanks
No worries, you might have to add the
| order(_score desc)
to order them correctly 😊
Another approach might be to query everything of that type and then, in the projection, have two subqueries each being whatever's returned by two conditions.
Here's an example from the docs


// Projections also have syntactic sugar for inline conditionals
*[_type=='movie']{
  ...,
  releaseDate >= '2018-06-01' => {
    "screenings": *[_type == 'screening' && movie._ref == ^._id],
    "news": *[_type == 'news' && movie._ref == ^._id],
  },
  popularity > 20 && rating > 7.0 => {
    "featured": true,
    "awards": *[_type == 'award' && movie._ref == ^._id],
  },
}
Thanks for that Vincent. I made it work using the
boost
function, which is a bit cleaner, but this one also makes sense I think 🙏
Yeah that particular example is visually aggressive but the basic concept is as clean as the queries and conditions it contains. Sounds like with boost it can actually be ordered instead of grouped and all together 😃
I’m not sure if your actual query is more complex than the one in your example, but I expect you could depend on the match being a boolean as part of your order:

*[title match "whatever" || subtitle match "whatever"] | order((title match 'whatever') desc)
This would put anything matching
title
first, and since anything else must match
subtitle
, it will all come next.
That's really smart, actually. GROQ elegance is still a step or two ahead of me for everything but basic queries and projections, but it feels so retroactively intuitive once you get a walkthrough of a move to make like that.

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?