Skip to content
This repository was archived by the owner on May 13, 2021. It is now read-only.

paginateRaw() returns wrong amount of total items #131

Closed
jan-krueger opened this issue Apr 27, 2021 · 4 comments
Closed

paginateRaw() returns wrong amount of total items #131

jan-krueger opened this issue Apr 27, 2021 · 4 comments

Comments

@jan-krueger
Copy link

jan-krueger commented Apr 27, 2021

Hello, first up, thank you for this package, but I run into an issue. I have a search interface, and the search itself looks like this:

$data = $request->validated();
$search = Paper::search($data['query'], function (Indexes $meilisearch, $query, $options) use ($or_generator, $data) {

    $conditions = [];
    //... code that generates filters based on the user's input. this works fine.
    if(!empty($conditions)) {
        $options['filters'] = join(' AND ', $conditions);
    }

    return $meilisearch->search($query, $options);
});

$result = $search->orderBy('published_date', 'desc')->paginateRaw(2, 'page', $data['page']);
Log::info($result->total());
return $result;

I have in total 3 records in the database, so for testing purposes I set the amount of items per page to 2.

  1. When no $options['fliters'] are applied it works perfectly.
  2. When I select a category in the UI that generates a $options['fliters'] that is filled with something like e.g. category_id = 1 then it starts to break down. In this example, there are only two items that match this requirement yet when I select the first page, it thinks that there are 3 items in total resulting in two pages. When I go to the second page which causes another search request but with $data['page'] = 2 it "notices" that there is no second page, and then says that the total amount of items is 2. But at that point I am already displaying an empty page to the user.

The issue also persists when I set the max items per page to 1. It will initially tell me that there are 3 pages available when I visit the first or second page but as soon as I go to the third, it returns no more items and then it also actually returns that there are only 2 in this category.

Do you have any idea what could cause this? I use Laravel 8.38. Scout 8.6.1, and meilisearch-laravel-scout 0.12.5.

@jan-krueger
Copy link
Author

I had already looked around a bit but I just found these two issues:
meilisearch/meilisearch#1120
meilisearch/documentation#561

Maybe you should consider to throw an error when the user tries to use paginateRaw or the in-build pagination methods in general since it doesn't work like officially documented & expected.

@curquiza
Copy link
Member

Hello @jan-krueger! Sorry for the confusion and the issue! As you found out we are aware of this issue. We have just hired someone as a product owner who will help us improve this kind of feature and remove the confusion around the pagination in the future.

thanks for your feedback 🙂

@barryvdh
Copy link

barryvdh commented May 7, 2021

We are experiencing the same issue when attempting to use pagination together with a filter. The workaround works (take 1000 items, array_slice them) but doesn't feel optimal. It's also not supported by the default pagination in this package (or laravel/scout). Maybe the implementation can be fixed to use array_slice or another workaround when filters are present?

Or is it expected that offset/limit will work as expected soon?

@curquiza
Copy link
Member

curquiza commented May 10, 2021

If you mean soon like "next month", it will not. The core team is currently working hard on a huge refactor that might be released during the summer. Once this release is done, the pagination will be the first prioritization. So it will be improved by the end of the year 🙂
As you said the array_slice work-around is not optimal either. However if you have a fork of this repo (or laravel/scout) with this workaround implemented it might worth to check 🙂

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

No branches or pull requests

3 participants