private function searchCombination(SearchParameterBag $searchParams)
 {
     $query = new \Elastica_Query_Text();
     $query->setFieldQuery('name', $searchParams->get('term'));
     $query->setFieldParam('name', 'type', 'phrase_prefix');
     $this->results = $this->container->get('foq_elastica.finder.treatments')->find($query);
     $client = $this->container->get('foq_elastica.client');
     $search = new \Elastica_Search($client);
     $postType = $this->get('foq_elastica.index.acme.post');
     $tagType = $this->get('foq_elastica.index.acme.tag');
     // add the types to the search
     $search->addType($postType)->addType($tagType);
     $index = $this->get('foq_elastica.index');
     $search->addIndex($index);
     $searchTerm = $request->query->get('terms');
     $postSubjectQuery = new \Elastica_Query_Text();
     $postSubjectQuery->setFieldQuery('subject', $searchTerm);
     $postSubjectQuery->setFieldParam('subject', 'analyzer', 'snowball');
     $tagQuery = new \Elastica_Query_Text();
     $tagQuery->setFieldQuery('tagname', $searchTerm);
     $tagQuery->setFieldParam('tagname', 'analyzer', 'snowball');
     $boolQuery = new \Elastica_Query_Bool();
     $boolQuery->addShould($nameQuery);
     $boolQuery->addShould($keywordsQuery);
     $results = $search->search($boolQuery);
     return array('results' => $results);
 }
 private function _extractVariables(SearchParameterBag $parameterBag)
 {
     $variables = array();
     if ($parameterBag instanceof BroadSearchParameterBag) {
         // processing for comination search of brroad search term
         // parameters has a termId in the parameters
         // binary check on state to check if parameters has term id
         if (0 != (BroadSearchParameterBag::STATE_HAS_TERM_ID & $parameterBag->get('state'))) {
             // process term documents
             $termDocuments = $this->container->get('services.search')->getTermDocuments($parameterBag);
             //$this->container->get('services.search')->getTermDocumentsByTermName($parameterBag); // find related terms by term label
             // term points to specific type of document
             if (1 == count($termDocuments)) {
                 $variables = $this->_extractVariablesFromTermDocument($termDocuments[0]);
             } else {
                 // TODO: related terms
             }
         } else {
             // check if it has term label
         }
         //-- end process of termId part
         //var_dump(BroadSearchParameterBag::STATE_HAS_DESTINATION_ID & $parameterBag->get('state')); exit;
         // check if parameters has a destination in it
         if (0 != (BroadSearchParameterBag::STATE_HAS_DESTINATION_ID & $parameterBag->get('state'))) {
             $variables[SearchParameterService::PARAMETER_KEY_COUNTRY_ID] = $this->_findGenericObjectById('country', $parameterBag->get(SearchParameterService::PARAMETER_KEY_COUNTRY_ID, 0));
             $city = $this->_findGenericObjectById('city', $parameterBag->get(SearchParameterService::PARAMETER_KEY_CITY_ID, 0));
             if ($city) {
                 $variables[SearchParameterService::PARAMETER_KEY_CITY_ID] = $city;
             }
         }
     }
     return $variables;
 }
 private function searchCitiesByNameWithTreatment(SearchParameterBag $searchParams)
 {
     $connection = $this->container->get('doctrine')->getEntityManager()->getConnection();
     $treatmentId = null;
     $name = '"%' . $searchParams->get('searchedTerm') . '%"';
     $institutionStatus = InstitutionStatus::getBitValueForActiveAndApprovedStatus();
     $imcStatus = InstitutionMedicalCenterStatus::APPROVED;
     switch ($searchParams->get('treatmentType')) {
         case 'specialization':
             $treatmentId = $searchParams->get('specializationId');
             $sql = "\n                    SELECT a.id AS city_id, a.name AS city_name, b.id AS country_id, b.name AS country_name\n                    FROM cities AS a\n                    LEFT JOIN countries AS b ON a.country_id = b.id\n                    INNER JOIN institutions AS c ON a.id = c.city_id AND b.id = c.country_id\n                    INNER JOIN institution_medical_centers AS d ON c.id = d.institution_id\n                    INNER JOIN institution_specializations AS e ON d.id = e.institution_medical_center_id\n                    WHERE a.name LIKE {$name}\n                    AND c.status = {$institutionStatus}\n                    AND d.status = {$imcStatus}\n                    AND e.specialization_id = {$treatmentId}\n                ";
             break;
         case 'subSpecialization':
             $treatmentId = $searchParams->get('subSpecializationId');
             $sql = "\n                    SELECT a.id AS city_id, a.name AS city_name, b.id AS country_id, b.name AS country_name\n                    FROM cities AS a\n                    LEFT JOIN countries AS b ON a.country_id = b.id\n                    INNER JOIN institutions AS c ON a.id = c.city_id AND b.id = c.country_id\n                    INNER JOIN institution_medical_centers AS d ON c.id = d.institution_id\n                    INNER JOIN institution_specializations AS e ON d.id = e.institution_medical_center_id\n                    INNER JOIN institution_treatments AS f ON f.specialization_id = e.id\n                    LEFT JOIN treatments AS g ON f.treatment_id = g.id\n                    INNER JOIN treatment_sub_specializations AS h ON g.id = h.treatment_id\n                    WHERE a.name LIKE {$name}\n                    AND c.status = {$institutionStatus}\n                    AND d.status = {$imcStatus}\n                    AND h.sub_specialization_id = {$treatmentId}\n                ";
             break;
         case 'treatment':
             $treatmentId = $searchParams->get('treatmentId');
             $sql = "\n                    SELECT a.id AS city_id, a.name AS city_name, b.id AS country_id, b.name AS country_name\n                    FROM cities AS a\n                    LEFT JOIN countries AS b ON a.country_id = b.id\n                    INNER JOIN institutions AS c ON a.id = c.city_id AND b.id = c.country_id\n                    INNER JOIN institution_medical_centers AS d ON c.id = d.institution_id\n                    INNER JOIN institution_specializations AS e ON d.id = e.institution_medical_center_id\n                    INNER JOIN institution_treatments AS f ON f.specialization_id = e.id\n                    WHERE a.name LIKE {$name}\n                    AND c.status = {$institutionStatus}\n                    AND d.status = {$imcStatus}\n                    AND f.treatment_id = {$treatmentId}\n                ";
             break;
     }
     $stmt = $connection->executeQuery($sql);
     //TODO: use prepared statements. there seems to be a bug though: the code
     //below will return an empty result
     //         $stmt = $connection->prepare($sql);
     //         $stmt->bindValue('name', '"%'.$searchParams->get('term').'%"');
     //         $stmt->bindValue('treatmentId', $treatmentId, \PDO::PARAM_INT);
     //$stmt->bindValue('institutionStatus', InstitutionStatus::INACTIVE, \PDO::PARAM_INT);
     return $stmt->fetchAll();
 }
 public function getTermDocuments(SearchParameterBag $searchParams)
 {
     $filters = array();
     if ($searchParams->get('countryId', 0)) {
         $filters['country_id'] = $searchParams->get('countryId');
     }
     if ($searchParams->get('cityId', 0)) {
         $filters['city_id'] = $searchParams->get('cityId');
     }
     return $this->searchStrategy->getTermDocuments($searchParams, array('filters' => $filters));
 }