pagination in elasticsearch laravel

2020-02-15 01:33发布

I am using shift31/laravel-elasticsearch:~1.0. I want to implement pagination on my list page.

Search code :

   $params = [
        'index' => 'my_index',
        'type' => 'product',
        'body' =>  [
                       'query'=>[
                                 'match'=>[
                                           'category'=>$category
                                          ]
                                 ]
                   ];
   ];
  $response = \Es::Search($params);

How to use pagination in above query ?

Update :

I used from and size in query but how to give pagination link in view page ? and how to update from by click on page ?

1条回答
叼着烟拽天下
2楼-- · 2020-02-15 02:00

in repository

 $params = [
        'index' => 'my_index',
        'type' => 'product',
          'size' = $per_page;
          'from' = $from;
        'body' =>  [
                       'query'=>[
                                 'match'=>[
                                           'category'=>$category
                                          ]
                                 ]
                   ];
   ];

$response = \Es::Search($params);
 $access = $response['hits'];
        return $access;

i set $per_page and $from in controller

$per_page = $request->get('limit', 10);
    $from = ($request->get('page', 1) - 1) * $per_page;
    $access = $this->repository->Index($per_page, $from);

    $admin_exceptions = new LengthAwarePaginator(
            $access['hits'],
            $access['total'],
            $per_page,
            Paginator::resolveCurrentPage(),
            ['path' => Paginator::resolveCurrentPath()]);
 return view('adminexception.index', compact('admin_exceptions'))->withInput($request->all());

and now use render in in the views {{!!$admin_exceptions->render()!!}}

查看更多
登录 后发表回答