Querying nested documents with GROQ and implementing search in Sanity.io
61 replies
Last updated: May 7, 2020
J
Hi, I was wondering if anyone could help me with GROQ?
Apr 15, 2020, 7:39 PM
J
Okay so basically I'm trying to search my data set for certain criterias
Apr 15, 2020, 7:42 PM
J
but my dataset has references to other documents that I need to query also
Apr 15, 2020, 7:42 PM
J
for instance I have a documents that have _type == "dress"
Apr 15, 2020, 7:43 PM
J
and inside the dress documents there's a fabric key that has a _ref to a fabric document htat has a name
Apr 15, 2020, 7:43 PM
J
I want to query by the name of the dress and the name of the fabric
Apr 15, 2020, 7:43 PM
J
does that make sense?
Apr 15, 2020, 7:43 PM
J
like is there anyway I can join all of this stuff and then query off of that query result?
Apr 15, 2020, 7:44 PM
J
yeah that's for the join, right?
Apr 15, 2020, 7:46 PM
You can learn more about joins here https://www.sanity.io/docs/groq-joins
Apr 15, 2020, 7:46 PM
J
I basically want to do like a search though like `[_type === "dress" && fabricname == "name"]
Apr 15, 2020, 7:46 PM
J
[_type === "dress" && fabricname == "name"]
Apr 15, 2020, 7:46 PM
J
I understand how to do the joins
Apr 15, 2020, 7:46 PM
J
I want to be able to do conditionals on a document and the document that are referenced
Apr 15, 2020, 7:46 PM
J
I can do that without joining in the first place?
Apr 15, 2020, 7:47 PM
J
Ok, gonna try it real quick
Apr 15, 2020, 7:47 PM
Covered in the “semijoins” section here https://www.sanity.io/docs/groq-joins#semijoins-662d6d368945
Apr 15, 2020, 7:48 PM
J
Thank you sir
Apr 15, 2020, 7:49 PM
J
What if my document has an array of properties which has a property that references another document?
Apr 15, 2020, 7:59 PM
J
this works for the join
Apr 15, 2020, 7:59 PM
J
colors[]{color->{name}}
Apr 15, 2020, 7:59 PM
J
but what would be the syntax for that for a conditional?
Apr 15, 2020, 8:00 PM
J
colors[].color->name doesn't work
Apr 15, 2020, 8:00 PM
J
cool
Apr 15, 2020, 8:12 PM
J
so -> always looks for a _ref?
Apr 15, 2020, 8:13 PM
J
Quick one more question
Apr 15, 2020, 9:15 PM
J
so all of this worked and here is my result
Apr 15, 2020, 9:15 PM
J
defaultImage: {
asset: {
url: '<https://cdn.sanity.io/images/6ci6a8f7/production/3abd50a00ca8269ae3e0fe33a64103e0c2099bc2-1200x1638.jpg>'
}
}
Apr 15, 2020, 9:15 PM
J
is there anyway to take that url out of that nested structure and have it on a parent property?
Apr 15, 2020, 9:15 PM
J
Hey Knut, thanks for much for yur time
Apr 16, 2020, 3:56 PM
J
your*
Apr 16, 2020, 3:56 PM
J
I just have one more question
Apr 16, 2020, 3:56 PM
J
I have the following part in my query
Apr 16, 2020, 4:24 PM
J
colors[]{color->{name,"url": image{asset->{url}}}}
Apr 16, 2020, 4:24 PM
J
this makes a property called colors which is an array of the following objects
Apr 16, 2020, 4:24 PM
J
{ color: { name: 'Auburn Moon', url: [Object] } }
Apr 16, 2020, 4:25 PM
J
I just want it to be
Apr 16, 2020, 4:25 PM
J
colors: [{ name 'Auburn Moon', url: 'string' }, ....]
Apr 16, 2020, 4:25 PM
J
nevermind, I figured it out! thx
Apr 16, 2020, 4:31 PM
J
oh, so I have this:
"images": [defaultImage.asset->url]
Apr 16, 2020, 4:32 PM
J
how can I have it return an empty array if nothign is in there? instead of [null]
Apr 16, 2020, 4:32 PM
J
cool, ty
Apr 16, 2020, 7:15 PM
J
Hi, is there a limit to the number of request we can hit
Apr 17, 2020, 6:02 PM
J
Like do you recommend making a backend endpoint to hit your endpoint and add a caching service to limit the amount of times the sanity endpoint gets hit?
Apr 17, 2020, 6:11 PM
Here’s the details - we also offer caching on CDN https://www.sanity.io/docs/technical-limits
Apr 17, 2020, 6:19 PM
J
Hi Knut, I was wondering if there's a way to remove case sensitivity while searching for something in a query?
May 4, 2020, 10:23 PM
Not at the moment, but I believe we’re working on a solution for that. Stay tuned!
May 5, 2020, 6:33 AM
J
ok, so what would be the best way to implement searching?
May 5, 2020, 6:33 AM
J
kk
May 5, 2020, 6:34 AM
If you hit the wall with GROQ, you can also look at dedicated search services, like https://www.sanity.io/blog/indexing-in-algolia-using-serverless-functions-and-observables
May 5, 2020, 6:34 AM
J
Thanks for this, quick question. I have to run GROQ queries to get the data shaped into a certain format, how can I stream this with using the client rather than the export API so I can leverage observables?
May 7, 2020, 6:00 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.