Searching documents in Sanity Studio using references and filters

5 replies
Last updated: Jan 21, 2025
Can I use Studio to search for documents based on reference? eg search Movies that have Tom Cruise?
AI Update

Yes, you can search for documents based on references in Sanity Studio! There are a few ways to do this:

When you're creating or editing a document with a reference field, Sanity Studio provides built-in search functionality. According to the Connected Content documentation, a reference field in the studio lets you "search and select a document you want to reference."

For example, if you have a movie document with a cast field that references actor documents, when you're editing a movie, you can search for and select actors like Tom Cruise directly in that reference field.

Reverse Reference Search (Finding Movies with Tom Cruise)

If you want to search backwards - finding all Movies that reference a specific Actor - you have several options:

1. Using Structure Builder

You can create custom list views using the Structure Builder that filter documents based on references. For example:

S.listItem()
  .title('Movies with Tom Cruise')
  .child(
    S.documentList()
      .title('Movies with Tom Cruise')
      .filter('_type == "movie" && references($actorId)')
      .params({ actorId: 'tom-cruise-actor-id' })
  )

2. Using GROQ Queries

You can use the references() function in GROQ to find documents that reference a specific document:

*[_type == "movie" && references("actor-id-here")] {
  title,
  cast[]->{ name }
}

This finds all movies that reference the specified actor ID anywhere in their structure.

Customizing Search Behavior

According to the Studio search configuration docs, you can also customize how search works in Studio by:

  • Defining custom field weights - Make certain fields (like actor names) more important in search results
  • Configuring search strategies - Choose between different search implementations (groqLegacy, groq2024, textSearch)

So while the global Studio search doesn't automatically show "reverse references" out of the box, you have powerful tools to set up exactly the kind of reference-based search you need!

Hi thereπŸ‘‹
You can use GROQ in the vision tool with a query like this:

*[_type == "movie" && references(*[
  _type == "actor" && 
  name match "Tom Cruise"
]._id)]
yeah that part i figured out, i was wondering if it's possible straight up in the studio without GROQing
Just got a chance to test this. It look like you can use the main search with the "Contains document, image or file" filter to search for which type has a particular reference. In my case, I was able to find all drills that use cones. Please let me know if this is helpful!
Just got a chance to test this. It look like you can use the main search with the "Contains document, image or file" filter to search for which type has a particular reference. In my case, I was able to find all drills that use cones. Please let me know if this is helpful!
oh i see, main search, cool, i was looking at just document search πŸ‘ thank you for validating this πŸ™

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?