public function searchAction(Request $request) { $resultsTemplate = $this->container->getParameter('jonafrank.search.results.template'); $params = array(); $query = $request->query->get('query'); $page = $request->query->get('page', 1); switch ($this->container->getParameter('jonafrank.search.engine')) { case 'google': $params = array('title' => 'Search', 'query' => SearchParams::requestedQuery($request, 'query'), 'page' => SearchParams::requestedPage($request, 'page')); break; case 'elasticsearch': $finder = $this->container->get('fos_elastica.finder.example_blog.post'); $paginator = $this->get('knp_paginator'); $results = $finder->createPaginatorAdapter($query); $pagination = $paginator->paginate($results, $page, 10); $params = array('query' => $query, 'title' => 'Search', 'pagination' => $pagination); break; case 'doctrine': $pagination = $this->doctrineSearch($request); $params = array('query' => $query, 'title' => 'Search', 'pagination' => $pagination); break; } return $this->render($resultsTemplate, $params); }
/** * Search method. * * @param mixed $query string current search query or null * @param mixed $page string current result page to show or null * @param mixed $lang string language to use for restricting search results, or null * @param array $options any options which should be passed along to underlying search engine * @param Request $request request object, will be automatically injected by symfony when called as an action * * @return Response */ public function searchAction($query = null, $page = null, $lang = null, $options = array(), Request $request = null) { if (null === $page) { // If the page param is not given, it's value is read in the request $page = SearchParams::requestedPage($request, $this->pageParameterKey); } if (null === $query) { // If the query param is not given, it's value is read in the request $query = SearchParams::requestedQuery($request, $this->queryParameterKey); } $lang = $this->queryLanguage($lang, $request); $showPaging = false; $searchResults = array(); $estimated = 0; if ('' !== $query) { /** @var $dm \Doctrine\ODM\PHPCR\DocumentManager */ $dm = $this->registry->getManager($this->managerName); // TODO: use createQueryBuilder to use the ODM builder $qb = $dm->createPhpcrQueryBuilder(); $this->buildQuery($qb, $query, $page, $lang); if ($this->showPaging) { $estimated = $this->getEstimated($qb); } $searchResults = $this->buildSearchResults($dm->getPhpcrSession(), $qb->execute()); if (!$this->showPaging) { $estimated = count($searchResults); } else { $showPaging = $estimated > $this->perPage; } } $params = array('searchTerm' => $query, 'searchResults' => $searchResults, 'estimated' => $estimated, 'translationDomain' => $this->translationDomain, 'showPaging' => $this->showPaging ? $showPaging : false, 'start' => ($page - 1) * $this->perPage + 1, 'perPage' => $this->perPage, 'searchRoute' => $this->searchRoute); return new Response($this->templating->render('LiipSearchBundle:Search:search.html.twig', $params)); }