在我的搜索控制器中,我使用Sphinx来获取结果。它看起来像这样:
$cl = new SphinxSearch();
$results = $cl->setMatchMode(\Sphinx\SphinxClient::SPH_MATCH_EXTENDED);
$results = $cl->setSortMode(\Sphinx\SphinxClient::SPH_SORT_ATTR_DESC, "start");
$results = $cl->search('@*' . $name, 'spots');
$results = $cl->get();
它向我展示了几百个结果,加载非常重。我知道Laravel具有分页功能(https://laravel.com/docs/5.4/pagination),但我不知道如何在这个Sphinx包中使用它。顺便说一下,我使用这个 - https://github.com/sngrl/sphinxsearch
有什么建议吗?
答案 0 :(得分:2)
您可以使用Sphinx结果创建分页器。从Sphinx获得分页结果后。
$paginator = new LengthAwarePaginator(
$results,
$totalResultsCount,
$perPage,
$currentPage
);
$paginator->setCollection(collect($results));
答案 1 :(得分:0)
手动创建分页器实例时,应手动“切片”传递给分页器的结果数组。因此,来自控制器的方法将如下所示:
public function doSearch()
{
$template = 'search/search_results';
$filters = Input::get('filter');
$currentPage = Input::get('page') ? Input::get('page') : 1;
$perPage = 5;
// prepare $sphinxQuery using the filters received
$results = SphinxSearch::search($sphinxQuery, 'indexname')
->setFieldWeights(
array(
'name' => 10,
)
)
->setMatchMode(SphinxClient::SPH_MATCH_EXTENDED)
->setSortMode(SphinxClient::SPH_SORT_EXTENDED, "name ASC")
->limit(1000)
->get(true);
$paginator = new LengthAwarePaginator(
collect($results)->forPage($currentPage, $perPage), sizeof($results), $perPage, $currentPage
);
$paginator->setPath(route('name.of.the.route'));
$paginator->setPageName('page');
$data['items'] = $paginator;
return View::make($template, $data);
}
在刀片模板中:
<?php echo $items->appends(['filter' => Request::get('filter')])->render(); ?>