Nova doesn't correctly handle Scout results ordering #6220
Unanswered
dm-pf
asked this question in
Ideas & Feature Requests
Replies: 3 comments
-
The suggested changes doesn't work in Postgres and MSSQL. |
Beta Was this translation helpful? Give feedback.
0 replies
-
That is unfortunately the case indeed. I shared my "workaround" if others using mysql hit this issue - but indeed a better (more generic) solution should be implemented. |
Beta Was this translation helpful? Give feedback.
0 replies
-
@dm-pf Thanks for your post. I'm having the same experience. I tried your work around but it's not working for me. Are you still using your method? I'm on Nova 5.7.5 maybe things have changed? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Description:
When using a scout engine (e.g. meilisearch) to filter results - one would expect that whatever order the search engine returns - it's what's going to get painted in the UI. But this is not the case.
The problem lies within
vendor/laravel/nova/src/Query/Builder.php::paginateFromScout
:$scoutResultKeys
is the array of IDs in correct order from Scout. Lets imagine it contains the following:[20, 21, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.......]
After calling:
Everything is scrambled by the SQL engine - and we get a Collection with this order
[1, 2, 3, 4.....]
. As we're aware of this, we try to re-order everything manually through the$paginator->setCollection(getCollection())
logic, but this is not enough. Why? Because at this point, the->simplePagine($perPage = 10)
has already gotten only the first 10 results from SQL (from ID:1 to ID10).As you saw before, ID:20 and ID:21 must be the first two items, but they will never show up in the first Nova resource page if perPage is 10. If we change perPage to 25 - then the result show up immediately in the first place.
Ideally, we should be sorting what Scout gave us - at the SQL level - and this is pretty straightforward with MySQL using
ORDER BY FIELD()
. For our own project, we have modified this file:Before:
After:
This way, results are sorted on the SQL level and simplePaginate() will work correctly.
Detailed steps to reproduce the issue on a fresh Nova installation:
Beta Was this translation helpful? Give feedback.
All reactions