Issue with the `score` and `boost` functions not working with array operations in GROQ queries.

19 replies
Last updated: Dec 7, 2022
Any clue why the
score
and
boost
function do not work with array operations?
Dec 6, 2022, 5:15 PM
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
Ohh I see
Dec 6, 2022, 5:43 PM
Any workarounds?
Dec 6, 2022, 5:43 PM
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
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
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
_ref
to use (eliminating the need to dereference).
Dec 6, 2022, 5:45 PM
mmm
Dec 6, 2022, 5:46 PM
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 projection
Maybe 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
Sorry I was speculating a possible solution
Dec 6, 2022, 5:47 PM
The solution you mention could be enought
Dec 6, 2022, 5:48 PM
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
_id
if 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:

*[_type == 'post']{
  _id,
  'valid': author->name match 'Victor'
}
As replacing the
*
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
I got it working using the examples you provided
Dec 7, 2022, 3:52 PM
Thank you!!
Dec 7, 2022, 3:52 PM
Great to hear!
Dec 7, 2022, 4:28 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.

Was this answer helpful?