Issue with the `score` and `boost` functions not working with array operations in GROQ queries.
19 replies
Last updated: Dec 7, 2022
V
Any clue why the
scoreand
boostfunction do not work with array operations?
Dec 6, 2022, 5:15 PM
V
In fact any query that joins any other document fails if put inside
score
Dec 6, 2022, 5:32 PM
Hi Victor. Unfortunately, the
score()function does not accept the dereference operator nor joins/subqueries. There’s a gotcha at the end of this section of the docs .
Dec 6, 2022, 5:36 PM
V
Ohh I see
Dec 6, 2022, 5:43 PM
V
Any workarounds?
Dec 6, 2022, 5:43 PM
V
I guess I could hack some webhook that inserts a hidden _tags value with the array in the form I need
Dec 6, 2022, 5:45 PM
V
It would be cool that document schemas could defined some kind of GROQ projection that acts similiar to SQL trigger
Dec 6, 2022, 5:45 PM
V
To compute values
Dec 6, 2022, 5:45 PM
Maybe an initial query that resolves the reference and then this becomes your second query that now “knows” which
_refto use (eliminating the need to dereference).
Dec 6, 2022, 5:45 PM
V
mmm
Dec 6, 2022, 5:46 PM
V
oh
Dec 6, 2022, 5:46 PM
Maybe this is an XY problem. Is this what you’re after?
It would be cool that document schemas could defined some kind of GROQ projectionMaybe there’s another way to go about it. Can you elaborate a bit on what you want to accomplish?
Dec 6, 2022, 5:47 PM
V
Sorry I was speculating a possible solution
Dec 6, 2022, 5:47 PM
V
The solution you mention could be enought
Dec 6, 2022, 5:48 PM
V
But I don't know if understand how to write the corresponding GROQ query
Dec 6, 2022, 5:48 PM
I’ll see if I can offer something more concrete, but I may not be able to get to it for a few days. In the meantime, you can chain projections together and each will pass its result to the next. In this (admittedly not great) example, the final projection is the one that returns a result and it’s using a dereference from the previous projection. It returns the
_idif the author’s name contained “Victor” and null if it didn’t.
*[_type == 'post']{ _id, 'valid': author->name match 'Victor' } { 'victorPostId': select(valid == true => _id, null ) }
Dec 6, 2022, 5:56 PM
You can think of the output from this:
As replacing the
*[_type == 'post']{ _id, 'valid': author->name match 'Victor' }
*in a typical GROQ query (with respect to the second projection). In other words, the output from the above becomes the new “source” data.
Dec 6, 2022, 5:57 PM
V
I got it working using the examples you provided
Dec 7, 2022, 3:52 PM
V
Thank you!!
Dec 7, 2022, 3:52 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.