Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

score() returns null when used in a joined query #75

Open
troymcginnis opened this issue May 12, 2022 · 5 comments
Open

score() returns null when used in a joined query #75

troymcginnis opened this issue May 12, 2022 · 5 comments

Comments

@troymcginnis
Copy link

score() on joined queries

I'm attempting to organize posts by categories and further organize each grouping of posts in categories by a variety of variables.

Single Category Listing page

I'm using score() on each category listing page that looks like the following:

*[_type == "post" && primaryCategory->slug.current == 'nhl'] | 
  score(
    "boston-bruins" in categories[].value.slug.current, 
    pt::text(content) match "Boston Bruins"
  ) | 
  order(_score desc, publishedDate desc) {
  _id,
  _score,
}[0..9]

Data:

[
  {
    "_id": "nf-nhl-42073",
    "_score": 9.318251
  },
  {
    "_id": "nf-nhl-42067",
    "_score": 9.157886
  },
  {
    "_id": "nf-nhl-42072",
    "_score": 8.695723
  },
  {
    "_id": "nf-nhl-42078",
    "_score": 8.657026
  },
  {
    "_id": "nf-nhl-42097",
    "_score": 8.460868
  },
  // ...
]

Main Listing page

However, on the main listing page, with groupings of categories, the query and results look as follows:

*[_type == "category" && parent->slug.current == "league"] | order(categoryOrder) {
  _id,
  name,
  "posts": *[_type == "post" && references(^._id)] | 
    score(
      "boston-bruins" in categories[].value.slug.current, 
      pt::text(content) match "Boston Bruins"
    ) | 
    order(_score desc, publishedDate desc) {
    _id,
    _score,
  } [0..2]
}

Output:

[
  {
    "_id": "c22f4b7f-1669-4349-9d6e-ad1c9c2aed39",
    "name": "NHL",
    "posts": [
      {
        "_id": "nf-nhl-42073",
        "_score": null
      },
      {
        "_id": "nf-nhl-42067",
        "_score": null
      },
      {
        "_id": "nf-nhl-42072",
        "_score": null
      }
    ]
  },
  // ...
]

Questions

There are several Gotchas in the official documentation, but nothing to suggest that this isn't support. My assumption this is unsupported by the score() function based on the return of null rather than 0 or an actual score.

  1. Is this indeed unsupported? Or am I just using it incorrectly?
  2. Is this on the roadmap? (this would be an awesome feature to have)
@atombender
Copy link
Member

This should work. If you can give me your project ID and dataset name, I can investigate. You can either email me or reach out to me by DM in the community Slack, which is a better place for this kind of support problem.

@atombender
Copy link
Member

Actually I'm able to reproduce the problem here.

@atombender
Copy link
Member

We have a fix in testing that we'll probably get out today or Monday.

@atombender
Copy link
Member

I guess I forgot to update this issue. The fix was released Oct 27. Closing this, but we can reopen if it's still a problem for you.

@kennyheard
Copy link

kennyheard commented Dec 19, 2023

@atombender I think this might be the exact issue I'm currently having.

Here's the query I'm using:

*[_type == "instrumentType"] {
  "instruments": *[_type == "instrument" && references(^._id)] | score(title match "example") {
    title,
    _score
  }
}

Here's the result (in Sanity Vision):

[] 2 items
  0: {} 1 property
    instruments: [] 46 items
      0: {} 2 properties
        title: Example
        _score: null
...

Running that same query as a standalone query, however:

*[_type == "instrument"] | score(title match "example") {
  title,
  _score
}

Shows the following (in Sanity Vision):

instruments: [] 80 items
  0: {} 2 properties
    title: Example
    _score: 5.1123605
...

@atombender atombender reopened this Dec 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants