Wednesday, January 19, 2022

[FIXED] elasticquent/Elasticquent paginate issue, It returns all data

Issue

I'm using 'elasticquent/Elasticquent' elasticsearch package for Laravel 5.2

here is my code

 $videos = Video::searchByQuery(['match_all' => []],null, null, 300, null, ['id' => 'desc']) ->paginate(42);

I want to get last 300 items with pagination 42 item per page, but it still returns all 300 items, not 42. Does anyone knows how to solve that problem?


Solution

As mentioned in the documentation found here, you can query by using the following function:

public static function searchByQuery(
    $query = null, 
    $aggregations = null, 
    $sourceFields = null, 
    $limit = null, 
    $offset = null, 
    $sort = null
)

Parameter 4 ($limit) and 5 ($offset) can be used to create pagination.


In your case the following query would grants you 42 results, from offset, 0.

$videos = Video::searchByQuery(['match_all' => []],null, null, 42, 0, ['id' => 'desc']);


Answered By - Nicklas Kevin Frank

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.