如何使用elasticsearch在Catalyst应用程序中进行分页?

时间:2018-04-10 14:15:21

标签: perl elasticsearch pagination catalyst

我正在使用Web框架Catalyst,我希望使用插件Data::Paginator对elasticsearch查询的结果进行分页。这是我现在的代码:

  

Root.pm

sub search :Global {
    my ( $self, $c ) = @_;
    my $search = $c->model('Search');
    my $m = $c->req->params->{m};

    my $from = 0;
    my $size = 10;          

    my $results = $search->search(
      index => 'index1',
      type => 'type1',
      body  => {
        query => {
            prefix => { entree => $m }
        },
    from => $from,
    size => $size
      }
    );

    my $pager = Data::Paginator->new(
     current_page => 1,
     entries_per_page => 10,
     total_entries => $results->{hits}{total},
    );
    my $p = { next_page=>$pager->next_page, previous_page=>$pager->previous_page };

    my @res=();
    for my $i ( @{ $results->{hits}->{hits} } ) {
      my $h = { entree => $i->{_source}->{entree}, contenu => $i->{_source}->{contenu} };
      push @res, $h;
    }

    $c->stash(template => 'search.tt', total => $results->{hits}{total}, m=>$m , res=>\@res, pager=>$p);
    $c->forward('View::HTML');
}

还有模板

  

search.tt

<!DOCTYPE html>
<html lang="fr">
  <head>
    <meta charset="utf-8">
    <title>Résultats</title>
  </head>
  <body>
    <h1>[% total %] résultat(s) pour [% m %]</h1>
    [% IF total >0 %]
      [% FOREACH h IN res %]
        <h2>[% h.entree %]</h2>
        <p>[% h.contenu %]</p>
      [% END %]
    [% ELSE %]
      <p>Pas de résultat</p>
    [% END %]
  </body>
  <footer>
    <a href="/">Nouvelle recherche</a>
    <a href="/search?offset=[% pager.previous_page %]&m=[% m %]">Previous Page</a>
    <a href="/search?offset=[% pager.next_page %]&m=[% m %]">Next Page</a>
    <a> DATA PAGINATOR TEST [% pager %]</a>
 </footer>
</html>

当我点击“下一页”或“上一页”并显示新结果时,我想通过elasticsearch创建一个新查询:

 my $results = $search->search(
      index => 'index1',
      type => 'type1',
      body  => {
        query => {
            prefix => { entree => $m }
        },
    from => $from + 10, 
  

OR from =&gt; $ -10,

    size => $size
      }
    );

关于如何做到这一点的一些提示会很好,谢谢。

编辑1:

  

Root_v2.pm

sub search :Global {
    my ( $self, $c ) = @_;
    my $search = $c->model('Search');
    my $m = $c->req->params->{m};
    my $offset = $c->req->params->{offset} ? $c->req->params->{offset} : 0;

    my $from = 0;
    my $size = 10;          

    my $results = $search->search(
      index => 'index1',
      type => 'type1',
      body  => {
        query => {
            prefix => { entree => $m }
        },
    from => $offset*$size,
    size => $size
      }
    );

    my $pager = Data::Paginator->new(
     current_page => $offset,
     entries_per_page => 10,
     total_entries => $results->{hits}{total},
    );
    my $p = { next_page=>$pager->next_page, previous_page=>$pager->previous_page };

    my @res=();
    for my $i ( @{ $results->{hits}->{hits} } ) {
      my $h = { entree => $i->{_source}->{entree}, contenu => $i->{_source}->{contenu} };
      push @res, $h;
    }

    $c->stash(template => 'search.tt', total => $results->{hits}{total}, m=>$m , res=>\@res, pager=>$p);
    $c->forward('View::HTML');
}

我从search.tt文件中获取了href的偏移量,并用它来修改elasticsearch查询中的“from”参数。

但我对Data :: Paginator的“current_page”有一个问题:即使我的$ offset本身等于0,它也永远不会等于0(参见current_page =&gt; $ offset)。当我第一次点击下一页时,我会错过第10到第20个结果。

行为是这样的:

  

单击搜索按钮:OFFSET = 0 / CURRENT_PAGE = 1 / NEXT_PAGE = 2   / PREVIOUS_PAGE =不是def

     

单击下一页按钮:OFFSET = 2 / CURRENT_PAGE = 2 / NEXT_PAGE = 3   / PREVIOUS_PAGE = 1

     

单击上一页按钮:OFFSET = 1 / CURRENT_PAGE = 1 / NEXT_PAGE = 2   / PREVIOUS_PAGE =不是def

当我第一次点击下一页按钮时,你可以看到我错过了结果,因为我的偏移直接从0跳到2跳过1.

编辑2

  

Root_v3.pm

修改:

my $offset = $c->req->params->{offset} ? $c->req->params->{offset} : O; 
▼▼▼
my $offset = $c->req->params->{offset} ? $c->req->params->{offset} : 1;

from => $offset*$size,
▼▼▼
from => ($offset-1)*$size,

现在有效!

1 个答案:

答案 0 :(得分:0)

使用我所做的两次编辑查看原始帖子。