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

Pagination throws error Undefined array key "totalHits" #895

Closed
vwmoose opened this issue Dec 11, 2024 · 3 comments · Fixed by #900
Closed

Pagination throws error Undefined array key "totalHits" #895

vwmoose opened this issue Dec 11, 2024 · 3 comments · Fixed by #900

Comments

@vwmoose
Copy link

vwmoose commented Dec 11, 2024

Scout Version

10.11

Scout Driver

Meilisearch

Laravel Version

11.31

PHP Version

8.2

Database Driver & Version

MySQL 8.0.32

SDK Version

meilisearch/meilisearch-php v1.11

Meilisearch CLI Version

No response

Description

When performing a meilisearch query using ->get() everything works fine, I get back the default 20 rows and the totalHits is returned and correct.

As soon as I replace ->get() with ->paginate(20) it throws the error Undefined array key "totalHits" as the key is missing from the meilisearch $results passed to MeilisearchEngine.php getTotalCount() method.

If I alter the getTotalCount() method to return $results['totalHits'] ?? $results['nbHits'] I get paginated results as expected but only for one page even though $results['nbHits'] equals 1000.

Here is my code in the controller:

$filters = [ "filter" => "price.amount >= 100 AND price.amount <= 200" ];`

$results = Property::search($search, function(Indexes $meilisearch, $query, $options) use ($filters) {
return $meilisearch->search($query, $filters);
})
->query(fn (Builder $query) => $query->withCount(['images']))
->paginate(20);

The above code only works with my hack.

Steps To Reproduce

As above.

Copy link

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

@Boorinio
Copy link
Contributor

Boorinio commented Jan 18, 2025

Hey, I did some digging and although you are correct the error shouldn't be thrown (#900)
Even if your meilisearch instance returned the totalHits you would still get only 1 page and it is kind of the expected behaviour because you have a query callback that can alter the results.

Image

Since you are not doing any filtering i would suggest removing the with query and rather do it in the collection?

Best Regards

@crynobone crynobone linked a pull request Jan 22, 2025 that will close this issue
@crynobone
Copy link
Member

Fixed via #900

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants