我有一个symfony2 / doctrine 2 / elastica / fosElasticaBundle / pagerFanta的应用程序。
我想将自定义和动态的queryBuilder与pagerfanta和elastica结合使用。不要改变结果,而是预先过滤它们。
到目前为止,我能够: 1.使用pagerfanta本身与我的自定义queryBuilder:
$page = $request->get('page', 1);
$search = $request->get('search');
$querybuilder = $this->getDoctrine()->getRepository('AppBundle:FoodAnalytics\Recipe')->findByTopCategoryQueryBuilder($category);
$explorerManager = $this->get('explorer_manager');
$pagerFanta = $explorerManager->getPagerFanta($querybuilder, $page, 4);
$recipes = $pagerFanta->getCurrentPageResults();
将Elastica与Pagerfanta一起使用,但不使用我的自定义QueryBuilder:
$page = $request->get('page', 1);
$search = $request->get('search');
$finder = $this->container->get('fos_elastica.finder.website.recipe');
$pagerFanta = $finder->findPaginated($search);
$recipes = $pagerFanta->getCurrentPageResults();
现在,我如何使用自定义QueryBuilder?我知道你可以在elastica配置中设置一个自定义的但我必须是动态的=接受一个参数,所以我想在控制器中设置它。这可能吗?
答案 0 :(得分:0)
你可以pass custom Elastica\Query to findPaginated
方法:
$query = new \Elastica\Query::create(new \Elastica\Query\Term(array(
'name' => $request->get('search')
));
$finder = $this->container->get('fos_elastica.finder.website.recipe');
$pagerFanta = $finder->findPaginated($query);
$recipes = $pagerFanta->getCurrentPageResults();
也许代码更清晰,你可以在没有FOSElasticaBundle的情况下使用PagerFanta with ElasticaAdapter时看到它是如何工作的:
// Searchable can be any valid searchable Elastica object. For example a Type or Index
$finder = new \Elastica\Index($elasticaClient, 'website.recipe');
// A Query can be any valid Elastica query (json, array, Query object)
$query = new \Elastica\Query::create(new \Elastica\Query\Term(array(
'name' => $request->get('search')
));
$elasticaAdapter = new \Pagerfanta\Adapter\ElasticaAdapter($finder, $query);
$paginator = new \Pagerfanta\Pagerfanta($elasticaAdapter);
$results = $paginator
->setMaxPerPage($limit)
->setCurrentPage($page)
->getCurrentPageResults();