How to implement user search query in custom Sanity movie app?

26 replies
Last updated: Nov 29, 2022
Is it possible to make a search query entered by the User, in my custom movie based app (not the one used as a template) ?
AI Update

Absolutely! You can implement user search functionality in your custom movie app using Sanity. Here's how to approach it:

Basic Approach

You'll want to use the Sanity Client to query your content based on user input. First, install it:

npm install @sanity/client

Then set up a client instance in your app:

import {createClient} from '@sanity/client'

const client = createClient({
  projectId: 'your-project-id',
  dataset: 'production',
  apiVersion: '2024-01-01',
  useCdn: true, // for faster cached responses
})

Implementing Search with GROQ

For text-based searches, you'll use GROQ's match operator, which performs full-text search with tokenization. Here's an example for searching movies:

async function searchMovies(searchTerm) {
  const query = `*[_type == "movie" && (
    title match $searchTerm ||
    overview match $searchTerm ||
    cast[].name match $searchTerm
  )]`
  
  return await client.fetch(query, { searchTerm })
}

Adding Relevance Scoring

To rank your search results by relevance, use the score() function to weight different fields:

const query = `*[_type == "movie" && (
  title match $searchTerm ||
  overview match $searchTerm
)] | score(
  boost(title match $searchTerm, 3),
  boost(overview match $searchTerm, 1)
) | order(_score desc)`

const results = await client.fetch(query, { searchTerm: userInput })

This gives title matches 3x more weight than overview matches, ensuring more relevant results appear first.

Important Notes

  • The match operator tokenizes text, so it's great for free-text search but not exact string matching
  • Use wildcards with caution: match "demo*" works, but special characters like @ and . are treated as word separators
  • For better performance on public content, use useCdn: true in your client config
  • Consider debouncing your search input to avoid excessive API calls

The combination of the Sanity Client, GROQ's match operator, and the score() function gives you a powerful, flexible search implementation that's perfect for a movie app!

Show original thread
26 replies
It is! You can pass in a variable using params. For example:
const query = `*[_type == 'movie' && title = $title]
const params = { title: <user-entered-term-here> }

const results = await client.fetch(query, params)
user M
I am not sure, how will I be able to take user input and pass it through query.
I am still a beginner
Are you using vanilla javascript or a framework for your frontend?
Ohh it's Next the best
Great! You'll need to create a controlled form that will allow you to hold whatever value the user enters in state. Then you can pass that variable to your params.
Ohh
user M
that was great explanation, I will get back to you if I am facing any issues but huge Thanks
Excellent! Best of luck!
user M
Are there any examples regarding this problem statement, I am still not sure what to write and where to write.
I don't think anyone has put together a guide for this particular use case. Can you share the code in your frontend where you'd like to add this?
Ohh sure
user M
I want to make search function here for the whole dataset.
Thanks! I'll be able to pick this back up in a bit 🙂
Sure sir
user M
Did you got some time to making my project better
??
Nope, it was unfortunately a very busy day in the community yesterday. I'll try to take a look today.
Sidenote: I'm guessing you used 'sir' to be polite, but my pronouns are listed in my Slack name (feminine or gender neutral). I'm assuming this was an accident, but please don't misgender people in the community.
user M
Ohh I am really sorry, It was a mistake, don't mind me.😔
I figured that was the case! Just wanted to give you a heads up 🙂
Sorry Ma'am
It would be great if you pls take a look at it.
Will do! Just have to wrap up my morning meetings, then I'll be able to take a look.
It looks like you got some help in this thread. Did that answer your original question? Are you just looking for help with this question now?
Yes I do got the required the help
user M
Great! I'll follow up in that new question in a couple of minutes!

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?