Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I cant paginate search result #75

Open
sergeymarshennikov opened this issue May 4, 2016 · 11 comments
Open

I cant paginate search result #75

sergeymarshennikov opened this issue May 4, 2016 · 11 comments

Comments

@sergeymarshennikov
Copy link

sergeymarshennikov commented May 4, 2016

I like to use elasticquent
It is my pleasure to use elasticquent
this is my code

$orders = Order::searchByQuery(
    [
     'query' => [
      'bool' => [
       'filter' => [
        [
         'term' => [
          'price' => $query
         ]
        ]
       ]
      ]
     ]
    ]
   )->sortByDesc('id')->paginate(15);

As you can see, I want to see 15 data per page. But, I can see 10 result per page but not 15
And when I click page 2 on result page, it goes page=2 but this page=2 is not elastic search result
How can I integrate pagination with elasticquent
if you help me, it will be appreciated

@cambricorp
Copy link
Contributor

cambricorp commented May 5, 2016

class ElasticquentResultCollection extends \Illuminate\Database\Eloquent\Collection
{
    /**
     * Paginate Collection
     *
     * @param int $pageLimit
     *
     * @return Paginator
     */
    public function paginate($pageLimit = 25)
    {
        $page = Paginator::resolveCurrentPage() ?: 1;
        //$sliced_items = array_slice($this->items, ($page - 1) * $pageLimit, $pageLimit);

        return new Paginator($this->items, $this->hits, $this->totalHits(), $pageLimit, $page, ['path' => Paginator::resolveCurrentPath()]);
    }
}

This should get your pagination going! I have submitted pull request for this issue.

@timgws
Copy link
Member

timgws commented May 13, 2016

@cambri can you please link me to the PR?

Edit: never mind, found the PR, it has already been merged.

@sergeymarshennikov, please do a composer update on dev-master, this should fix your issue.

@sergeymarshennikov
Copy link
Author

sergeymarshennikov commented Jun 2, 2016

@timgws
Thanks
I updated composer on dev-master

But, it does not work correctly
this is my code

// controller side

$query = Input::get('query');
if($query) {
$posts= Post::searchByQuery(
[
'multi_match' => [
'query' => $query,
'fields' => ['firstname', 'lastname']
]
]
)->sortBy('id', 0, true)->paginate(10);
$posts->appends(Input::only('query'))->render();
}else{
$posts= Post::orderBy('id', 'DESC')->paginate(10);
}
return view(testSearch')->with(['posts' => $posts]);

// view side : testSearch.blade
.....
{!! $orders->render() !!}

as a search result, total 31 pages links rendered in the bottom of view for pagination

in this case, url is : http://localhost:8000/testSearch?query=xxx+yyy

when I click page 2 link, url is http://localhost:8000/testSearch?query=xxx+yyy?page=2

But posts on page2 are all the same as on page 1

when I click page 3 link, the same result as on page 1 rendered.

How can I fix this

I need to see result on page 2 unlike page 1

please help me

@cambricorp
Copy link
Contributor

cambricorp commented Jun 12, 2016

It has been a little while since I crossed this bridge, the issue is likely from not coordinating your elasticsearch result size and from value to your application result limit, from, to value.

@sergeymarshennikov
Copy link
Author

Sorry I did not understand correctly

Would you explain again?

@cambricorp
Copy link
Contributor

cambricorp commented Jun 12, 2016

Search for "paginating elasticsearch result", I believe the problem is that you are not telling elasticsearch to return the next result set.

@sergeymarshennikov
Copy link
Author

can you teach me detailed code?

@cambricorp
Copy link
Contributor

cambricorp commented Jun 12, 2016

$q = \Request::input('q');
$page = \Request::input('page');

if($page==null){$page=1;}

$offset = ($page - 1) * 15;

$items = $this->model->searchByQuery([
  "query" => [
    "bool" => [
      "must" => [
        ["match" => ["name" => 
          [
            "query" => $q
          ]
        ]]],
      "should" => [
        ["match" => ["url" => 
          [
          "query" => $q,
          "boost" => 2
          ]
        ]]]
    ]
  ]
], null, null, 15, $offset, null)->paginate(15);

@sergeymarshennikov
Copy link
Author

I will check soon

Thanks

@Kharestani
Copy link

this code will work for the first page but it has a flaw for next pages you need to add a code like this to make it work fully:

        $items->appends(['q' => $q]);

@ronydebnath
Copy link

Those who are facing similar problem, here is an example that works

in Controller:

        $number_per_page = 20;
        if ($request->input('page') == null) {
            $page = "1";
        } else {
            $page = $request->input('page');
        }
        
        if($request->has('search') && $request->search != null){
            
            $searchTerm = $request->search;
            
            $totalresults = Tour::complexSearch(array(
                'body' => array(
                    'multi_match' => [
                        'query' => array(
                        'match' => array(
                            'title' => $request->get('search')
                        )
                    ),
                ],
                'from' => ($page-1) * $number_per_page,
                'size' => $number_per_page
                )
            ));
            $results = $totalresults->paginate(20);

in view paginate like this:

{{ $results->appends(Request::only('search'))->render() }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants