-
-
Notifications
You must be signed in to change notification settings - Fork 7
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
[Question]: How to get more than 20 search results back? #10
Comments
I assume you use the Have you tried customizing the pagination settings on the table itself? https://filamentphp.com/docs/3.x/tables/advanced#pagination |
Hi, Thanks for the response. Yeah I have the trait added, the search itself works it's just that i only ever get 20 results back (which i understand to be the meilisearch default). My understanding is that you can pass additional method calls to the search using
and i was just wondering if you have that running to pass in the current tables pagination info (results per page, current page etc) or if you had an example of which method to override to achieve it :-) |
Ok, for others I managed to get something working: adding this to my Resource/Pages/ListXXX.php file ( the one you added the protected function applySearchToTableQuery(Builder $query): Builder
{
$this->applyColumnSearchesToTableQuery($query);
$search = $this->getTableSearch();
if (blank($search)) {
return $query;
}
// sets the hard limit in meilisearch to a huge figure
resolve($this->getModel())->updateScoutSettings([
'pagination' => [
'maxTotalHits' => 100000000
]
]);
$allIds = collect();
$page = 1;
$perPage = 1000; // Define how many items you want per page
do {
$results = $this->getModel()::search($search)->query(function ($query) {
return $query->select('id');
})->paginate($perPage, 'page', $page);
$allIds = $allIds->concat($results->pluck('id'));
$page++;
} while ($results->count() > 0);
return $query->whereIn('id', $allIds->toArray());
} I then needed to create a method on my model: in App\Models\XXX.php use Meilisearch\Client;
class XXXX extends Model
{
... existing model code
public function updateScoutSettings($settings = null) {
$settings = $settings ?? [
'pagination' => [
'maxTotalHits' => 10000000 // Set to the maximum hits you require
]
];
// Initialize the MeiliSearch client
$client = new Client(config('scout.meilisearch.host'), config('scout.meilisearch.key'));
// Update settings for the specific index
$res = $client->index($this->searchableAs())->updateSettings($settings);
}
} ideally you would fire the |
This looks quite complex. I drafted PR #11 which should do what you want. Meilisearch has a limit of 20 items per The maximum of hits can be configured per index (https://www.meilisearch.com/docs/reference/api/settings#pagination). Default is 1000. I will merge the PR in the comming days. Perhaps you can check it as well 🙂 |
looking at the source code of the PR it looks a lot cleaner thanks. :-) There will still be an issue that by default meillisearch limits your searching (even pagination) to 1000 records by default. as per: https://www.meilisearch.com/docs/reference/api/settings#pagination. regardless of what you set your limits and offset to.
so even if you set a You are also missing the need to continue looping until you have no results left on your search to get all 5000 results back and then let the filament query actually do the pagination within it's subsequent pagination query , otherwise, you will always return only the first 100 results (using your default 100 as an example) The only way (i can see) to fix this is to change the value of What would be cool is to create an artisan console command in your package that you can run once, something like:
So your PR just to gather all the results (but still limited to 1000 unless you change $searchLimit = config('kainiklas-filament-scout.search_limit'); // default 100
$primaryKeyName = app($this->getModel())->getKeyName();
$keys = collect();
$page_count = 1;
do {
$search_results = $this->getModel()::search($search)->query(function ($query) use ($primaryKeyName) {
$query->select($primaryKeyName);
})->paginate($searchLimit, page: $page_count)
->pluck($primaryKeyName);
$keys = $keys->concat($search_results);
$page_count++;
} while (count($search_results) > 0);
return $query->whereIn($primaryKeyName, $keys->toArray()); |
actually my console command idea would also need to include the model
|
If it helps others i created this: App\Console\Commands\MeillisearchSettings.php. feel free to add it into your package if you want.
you can either use options of
|
also added it as a GIST here, in case others want to improve on it: https://gist.github.com/shopapps/e6ea29c4e208388e3f381703165a07ec |
You can also add the 'index-settings' => [
MyClass::class => [
'pagination' => [
'maxTotalHits' => 10000
],
],
], And then call |
ahh i did not know that - brilliant thanks |
What happened?
Searching only ever returns 20 results.
How do i add pagination to the search results?
How to reproduce the bug
search in table search box always returns 20 (or less) results.
Package Version
0.3.0
PHP Version
8.2
Laravel Version
10.00
Which operating systems does with happen with?
Linux
Notes
This is using Scout & Meilisearch
The text was updated successfully, but these errors were encountered: