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

Using `match` in GROQ query to search for slugs starting with a specific path

3 replies
Last updated: Jun 14, 2023
I tried to use
match
in a GROQ query to get all the slugs that starts with the same path as the current category.
*[slug.current match "/path/something" + "*"]
But this returns some extra documents that has
path and something elsewhere in the slug, like /path/different/something-document. So it seems like the slashes do something magic here, but I can’t find any documentation on it. How can I get the documents that start with this slug?
May 19, 2023, 1:31 PM
match
is for text, and will do tokenization. These will all evaluate to true:

"/something/path" match "/path/something"
"/something/path" match "something something path"
"/something/path" match "path? something!"
If you want to search by prefix, you should use
string::startsWith()
.
May 19, 2023, 1:52 PM
Ah, thanks, I only found the string global function when searching, but I see there is a string functions further down on the page.
May 19, 2023, 1:59 PM
In all the times I've seen that document I never noticed the tokenization part. Super cool!
Jun 14, 2023, 11:04 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?