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

paginate() + query() = wrong pagination #821

Closed t3946 closed 4 months ago

t3946 commented 4 months ago

Scout Version

10.8

Scout Driver

Meilisearch

Laravel Version

10.17

PHP Version

8.1.4

Database Driver & Version

MariaDB 10.5

SDK Version

No response

Meilisearch CLI Version

1.7.0

Description

I have table cars (id, title, price). I need find "Opel" with price between 1 .. 1000$.

Solve:

\App\Models\Car::search('Opel')
    ->query(fn ($query) => $query->where('price', '<=', 1000))
    ->paginate(20);

Result expected: Get one page with 20 items. Result real: Got five pages with 3-5 items on every page.

Steps To Reproduce

read desc

driesvints commented 4 months ago

Hi there,

Thanks for reporting but it looks like this is a question which can be asked on a support channel. Please only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repo you can try one of the following channels:

However, this issue will not be locked and everyone is still free to discuss solutions to your problem!

Thanks.

t3946 commented 4 months ago

Ok. I think scout can't correctly work with paginate() and query() methods in one query. So my solve is extends limits like this

config/scout.php

'index-settings' => [
    \App\Models\Car::class => [
        'pagination' => [
            'maxTotalHits' => 30000,
        ],
    ],
],

controller.php

$collection= \App\Models\Car::search($text)
    ->query(...)
    ->take(30000)
    ->get();

$total = $collection->count();
$perPage = 20;
$currentPage = 1;

$paginator = new LengthAwarePaginator(
    $collection->forPage($currentPage, $perPage),
    $total,
    $perPage,
    $currentPage
);

This code will solve my problem with pagination (in fact i'm selecting whole docs and able to do anything with it). And i get next limitation the 30000 documents search result maximum (this will take about 1.5gb ram what is not problem for my server).