This Gist contains a sample query for searching text content in GROQ, Sanity's custom query language. We use something similar to this for Causal's documentation site. See more on our blog!
-
-
Save essejmclean/b513bdfd072854338833c932bad3f014 to your computer and use it in GitHub Desktop.
Documentation Search in Sanity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*[_type == "docPage"] | |
| score( | |
boost(title match $queryString, 7), | |
boost(description match $queryString, 1), | |
boost(pt::text(content) match $queryString, 0.5) | |
) | |
{ | |
_score, | |
title, | |
content, | |
... | |
}[ _score > 0 ] | |
| order(_score desc)[0...$limit] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is the code we use to convert a user's raw text query to the `queryString` we pass | |
// to the GROQ query above | |
// For example, "category agg" would be converted to "*category* *agg*" | |
const queryString = query | |
.trim() | |
.replaceAll(/\s+/g, " ") // replace multiple spaces with a single space | |
.split(" ") | |
.map(s => `*${s}*`) // allow partial matches (e.g. "agg" can match "aggregation") | |
.join(" "); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment