👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

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
May 25, 2022, 7:09 AM
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)
)
May 25, 2022, 7:12 AM
May 25, 2022, 7:13 AM
That's awesome, will give it a try. Many thanks
May 25, 2022, 7:13 AM
No worries, you might have to add the
| order(_score desc)
to order them correctly 😊
May 25, 2022, 7:14 AM
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],
  },
}
May 25, 2022, 8:25 AM
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 🙏
May 25, 2022, 8:32 AM
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 😃
May 25, 2022, 10:14 AM
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.
May 25, 2022, 9:43 PM
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.
May 25, 2022, 9:47 PM

Sanity– build remarkable experiences at scale

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

Was this answer helpful?