laravel / scout

Laravel Scout provides a driver based solution to searching your Eloquent models.
https://laravel.com/docs/scout
MIT License
1.54k stars 327 forks source link

Incorrect Total Count in Laravel Scout Pagination #819

Closed razvaniacob closed 5 months ago

razvaniacob commented 5 months ago

Scout Version

10.8.5

Scout Driver

Typesense

Laravel Version

11.2

PHP Version

8.3.3

Database Driver & Version

No response

SDK Version

No response

Meilisearch CLI Version

No response

Description

When using Laravel Scout for searching with pagination, I'm encountering an issue where the total count of records returned by the pagination (paginate(200)) method is equal to the number of items per page specified (200), instead of reflecting the actual total number of matching records.

Steps To Reproduce

  1. I executed the following Scout search query with pagination:
    
    dd(ImportedProperty::search('pipera')
       ->query(fn (Builder $query) => $query->with(['imported_district']))
       ->paginate(200)->onEachSide(1)->withQueryString()
       ->through(fn ($obj) => [
           'name' => $obj->source_id,
           'district' => $obj->imported_district?->name ?? '',
       ]));

Observe that the LengthAwarePaginator returned indicates a total of 200 records, regardless of the actual total count of matching records. When I changed it to 10 per page I got a total of 10 records found.

Expected Behavior: The total count ($searchResults->total()) returned by Laravel Scout's pagination should accurately reflect the complete number of records that match the search criteria ('pipera'), independent of the pagination settings.

Actual Behavior: The total count ($searchResults->total()) is consistently equal to the number of items per page specified in paginate(200), leading to an incorrect representation of the total number of matching records.

Output of dd() showing the LengthAwarePaginator and its properties.

Screenshot 2024-04-08 at 1 39 14 PM

Currently, there is no workaround as the issue seems to be related to the pagination behavior within Laravel Scout.

Please advise on how to resolve this issue or if any additional information is required to further investigate.

Thank you.

driesvints commented 5 months ago

Hey there,

Can you first please try one of the support channels below? If you can actually identify this as a bug, feel free to open up a new issue with a link to the original one and we'll gladly help you out.

Thanks!

jackkitley commented 2 weeks ago

@driesvints Same issue here. This is a bug.

@razvaniacob


        return $this->model->search()
            ->query(fn(Builder $query) => $query->with(['mainListingImage']))
            ->options([
                'query_by' => 'vehicle_description, transmission, fuel_type, colour, year'
            ])
            ->paginate(12)
            ->withQueryString();```
jackkitley commented 2 weeks ago

if ->query() is removed then it is correct again