Closed thannaske closed 1 month ago
It's not possible to combine query
and paginate
in a reliable with Scout. Please see many previous issues around this: https://github.com/laravel/scout/issues?q=is%3Aissue+query+paginate+is%3Aclosed
If there are that many issues around that topic, it's clearly missing in the documentation.
Please take a look at here to see if it's any help
Scout Version
10.10.1
Scout Driver
Typesense
Laravel Version
10.48.16
PHP Version
8.3
Database Driver & Version
No response
SDK Version
Typesense PHP 4.9.3
Meilisearch CLI Version
No response
Description
Summary
When searching a model using Scout with the Typesense driver, using the
query()
method in combination with thepaginate()
method causes the pagination to return only the firstn
results (depending on the max items per page).Example with
query()
methodIn my concrete implementation, I'm searching through the full-text of messages. When displaying them, I need several relationships to be eagerly loaded in order to avoid n+1 query issues. According to the documentation, the
query()
method can used for such things:As you can see, we're searching by the
content
field in Typesense, applying some filtering and then applying some eager loading on all models found by the Scout search before paginating the results.The following code above leads to the following result (shortened for readability):
A
LengthAwarePaginator
is correctly being returned, and it shows a total of 10 results.Example without
query()
methodNow I'm commenting out the
query
method but leaving the rest of the code completely untouched:This now yields the following result:
The exact same search query now yields 176 total results with a total of 18 paginated pages which matches my test dataset. The only difference is now that no eager loading can be performed as the
query()
method has been commented out.Steps To Reproduce
Message
with acontent
attribute and create and save several instances with similar or same contentAuthor
to have a relationship that can be eagerly loadedquery()
method and compare the results