/**
  * @param Request $request
  *
  * @return Response
  */
 public function searchAction(Request $req)
 {
     $typeFilter = $req->get('type');
     $tagsFilter = $req->get('tags');
     if ($req->has('q') || $typeFilter || $tagsFilter) {
         $solarium = new SolariumClient('localhost:8080');
         $select = $solarium->createSelect();
         // filter by type
         if ($typeFilter) {
             $filterQueryTerm = sprintf('type:%s', $select->getHelper()->escapeTerm($typeFilter));
             $filterQuery = $select->createFilterQuery('type')->setQuery($filterQueryTerm);
             $select->addFilterQuery($filterQuery);
         }
         // filter by tags
         if ($tagsFilter) {
             $tags = array();
             foreach ((array) $tagsFilter as $tag) {
                 $tags[] = $select->getHelper()->escapeTerm($tag);
             }
             $filterQueryTerm = sprintf('tags:(%s)', implode(' AND ', $tags));
             $filterQuery = $select->createFilterQuery('tags')->setQuery($filterQueryTerm);
             $select->addFilterQuery($filterQuery);
         }
         if ($req->has('q')) {
             $escapedQuery = $select->getHelper()->escapeTerm($req->get('q'));
             $select->setQuery($escapedQuery);
         }
         $paginator = new Pagerfanta(new SolariumAdapter($solarium, $select));
         $paginator->setMaxPerPage(15);
         $paginator->setCurrentPage($req->get('page', 1), false, true);
         if ($req->isXmlHttpRequest()) {
             try {
                 return $this->render('ProductBundle:Search:list.html.twig', array('products' => $paginator->getResults(), 'noLayout' => true));
             } catch (\Twig_Error_Runtime $e) {
                 if (!$e->getPrevious() instanceof \Solarium_Client_HttpException) {
                     throw $e;
                 }
                 return new JsonResponse(array('status' => 'error', 'message' => 'Could not connect to the search server'), 500);
             }
         } else {
             return $this->render('ProductBundle:Search:search.html.twig', array('products' => $paginator->getResults()));
         }
     }
     return $this->render('ProductBundle:Search:search.html.twig', array('noLayout' => $req->isXmlHttpRequest()));
 }
Example #2
0
 public function search($searchType, $keywords, $filter = null)
 {
     if ($searchType == \Config::get('constants.SEARCH.ITEM')) {
         try {
             $result = \SolariumClient::search($keywords, $filter);
             if (!isset($result) || empty($result->getTotalItems())) {
                 $result = $this->_searchInDB($keywords, $filter);
             }
             return $result;
         } catch (\Exception $exception) {
             \Log::error($exception);
             $result = $this->_searchInDB($keywords, $filter);
             if (isset($result) && !empty($result->getTotalItems())) {
                 return $result;
             }
             throw $exception;
         }
     } else {
         throw new \Exception('Unsupport search type, ' . $searchType);
     }
 }