David Coder
David Coder

Reputation: 1138

How to paginate in elasticsearch with Laravel 5

I am new in elasticsearch i want to use pagination in it.

I am using from and size. I have total 10 records right now and i am ging 0 to from and 5 to size. I am getting 5 results. But how to give link to view page and how to increase the records for second page ?

My Query :

       $params = [
                'index' => 'my_index',
                'type' => 'product',
                'body' =>  [
                    "sort" =>[
                                ["default_product_low_price.sale_price" => ["order" => $sort]]
                            ],
                    "from" => 0, "size" =>5,
                    'query'=> $query,
                  ]
                ];
          $response = \Es::Search($params);

This is my query now where to give pagination link ?

Upvotes: 1

Views: 996

Answers (1)

Veerendra Borra
Veerendra Borra

Reputation: 1286

In repository

        $params['size'] = $per_page;
$params['from'] = $from;
$params['index'] = config('elastic.Admin_Logs');
$params['type'] = config('elastic.Admin_Type');
$params['body']['sort']['default_product_low_price.sale_price']['order'] = "desc";
$params['body']['query']['filtered']['filter']['bool']['must'][]['match_all'] = [];

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

In controller i set $per_page and $from

$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()!!}}

Upvotes: 1

Related Questions