It looks like you're trying to use Laravel Scout with Meilisearch and you want to access the raw search results from Meilisearch, including metadata like estimatedTotalHits, hitsCount, etc. However, when you use the ->get() method on the Scout builder, it returns an Eloquent collection of models, not the raw Meilisearch results.
To achieve what you want, you can directly interact with the Meilisearch client within your callback function and return the raw search results. Here's how you can modify your search method to achieve this:
class SearchController extends Controller
{
public function search(Request $request)
{
$search_text = trim($request->input('q')) ?? '';
$search_category = trim($request->input('categories')) ?? '';
// Directly interact with Meilisearch client
$results = Seed::search(
query: $search_text,
callback: function($meilisearch, $query, $options) use($request) {
return $meilisearch->search(
$query,
[
'attributesToHighlight' => ['name'],
'attributesToRetrieve' => ['id', 'name', 'url', 'thumbnail'],
]
);
}
);
// $results is an instance of Meilisearch\Search\SearchResult
// You can now access the raw data and metadata
$rawResults = $results->raw();
$estimatedTotalHits = $rawResults['estimatedTotalHits'];
$hitsCount = count($rawResults['hits']);
$limit = $rawResults['limit'];
// Pass the raw results to the view or process them as needed
return view('search.results', [
'results' => $rawResults['hits'],
'estimatedTotalHits' => $estimatedTotalHits,
'hitsCount' => $hitsCount,
'limit' => $limit,
]);
}
}
In this solution:
- The
Seed::searchmethod is used to perform the search. - The callback function directly interacts with the Meilisearch client and returns the raw search results.
- The raw search results are accessed using the
raw()method on theMeilisearch\Search\SearchResultinstance. - The raw data and metadata are then passed to the view or processed as needed.
This way, you can access all the additional useful data from Meilisearch that you need.