I am using Laravel Scout with Meilisearch issue with Default Sortting

I use Laravel Scout and Meilisearch and when I get results from Meilisearch they are in order of preference. The issue is the the table is automatically setting the defaultSort('primary_key', 'DESC') even though I am not specifying that. I do not have a defaultSort() on the table and the primary key is not even present on the table. So I will give an example. I have several columns that are sortable but when you search on a fresh load of the table I need it to not have any columns sorted. The reason is meilisearch responds with keys (primary keys) and then does the whereIn('key', Model::search($search)->keys()) and that returns the keys in a specific order that needs to be reflected on the table. Meilisearch returns more results than what is found because it does typo results as well. So while the result is there it is not at the top of the table like it should be because it is being sorted by the primary key automatically. I hope that made sense. If not let me know and I will try to explain it better. Basically I need to know is there a way to force NO default sort? I read through multiple posts and one talked about removing sortable from all the table columns but that is not what I want either. I would like for them to be able to sort if they need too but the default sort to be nothing at the start.
1 Reply
Robert 3020E KI6O
The following is a way to do it but renders the column sort orders useless if there is something in the search field. I have not put time in to figuring that part out yet but it might be possible to exclude the orderByRaw if there is a column sort order being used. But this works as expected and returns the order exactly how Meilisearch returns the results In the following License is the eloquent class and usi is the ID field sent to Meilisearch.
protected function applySearchToTableQuery(Builder $query): Builder
{
$this->applyColumnSearchesToTableQuery($query);

if (filled($search = $this->getTableSearch())) {
$query
->whereIn('usi', License::search($search)->keys())
->orderByRaw('FIELD(usi, ' . License::search($search)->keys()->join(',') . ') ASC');
}

return $query;
}
protected function applySearchToTableQuery(Builder $query): Builder
{
$this->applyColumnSearchesToTableQuery($query);

if (filled($search = $this->getTableSearch())) {
$query
->whereIn('usi', License::search($search)->keys())
->orderByRaw('FIELD(usi, ' . License::search($search)->keys()->join(',') . ') ASC');
}

return $query;
}