// therefore we seperate value lists we query for with " OR ". // otherwise ( // we had to set this to " ". const ACCORDING_TO_SOLR_DEFAULT_OPERATOR = " OR "; // place here all queries for the field results: // use the correct field var names of the index doc (should match with model field)! // MAYDO use form model to know which case is on private static function getPartialQuery($field, $value, $model) { switch ($field) { // the easy ones: case 'locality' : case 'collectors' : case 'collectorNo' : case 'unitID' : return $field . ":*" . $value . "*"; break; // checkboxes // they always deliver a value so we have to check // if we need a query case 'hasImage' : case 'hasTypestatus' : if ($value == 1) { return $field . ":" . $value; } else { return false; } break; // suggestion lists case 'fullScientificName' : return $field . ":\"" . $value . "\""; break; // selection lists // they alway deliver a value so we have to check // if it is the "nothing choosen value" // then we won't need a query case 'country' : case 'continent' : case 'sea' : case 'ocean' : case 'kindofunit' : case 'institution' : // if nothing is choosen look for all of them if ($value == $model->getSelectionListNonChosen ()) { return false; // return $field . ":*"; } else { return $field . ":\"" . $value . "\""; } break; case 'familyName' : case 'fullScientificName' : // TODO, what happens if user does not choose a value // but fills in her own string and pushes form button? return $field . ":" . $value; break; // fields with that come as a pair and provide a range for the value case 'latitudeFrom' : // TODO this has to go to form validation!!! $value2; if (floatval ( $value )) { if (floatval ( $model->attributes ['latitudeTo'] )) { $value2 = $model->attributes ['latitudeTo']; // TODO: build query string // blank latitudeTo because it is already considered here // and needed not be used for an other query $model->attributes = array ( 'latitudeTo' => '' ); // if only from is set } else { $value2 = "*"; } return "latitude:[" . $value . " TO " . $value2 . "]"; } else { // TODO remove when yii validation is doing this return false; } break; case 'latitudeTo' : // TODO this has to go to form validation!!! if (floatval ( $value )) { // this is only the case when "to" is set but not "from" // the other cases are already covered in the longitudeFrom case if (! (floatval ( $model->attributes ['latitudeFrom'] ))) { return "latitude:[ * TO " . $value . "]"; // if only from is set } } return false; break; case 'longitudeFrom' : // TODO this has to go to form validation!!! $value2; if (floatval ( $value )) { if (floatval ( $model->attributes ['longitudeTo'] )) { $value2 = $model->attributes ['longitudeTo']; // TODO: build query string // blank latitudeTo because it is already considered here // and needed not be used for an other query $model->attributes = array ( 'longitudeTo' => '' ); // if only from is set } else { $value2 = "*"; } return "longitude:[" . $value . " TO " . $value2 . "]"; } else { // TODO remove when yii validation is doing this return false; } break; case 'longitudeTo' : // TODO this has to go to form validation!!! if (floatval ( $value )) { // this is only the case when "to" is set but not "from" // the other cases are already covered in the longitudeFrom case if (! (floatval ( $model->attributes ['longitudeFrom'] ))) { return "longitude:[ * TO " . $value . "]"; // if only from is set } } return false; break; case 'collectionYearFrom' : // TODO this has to go to form validation!!! $value2; if (floatval ( $value )) { if (floatval ( $model->attributes ['collectionYearTo'] )) { $value2 = $model->attributes ['collectionYearTo']; // TODO: build query string // blank collectionYearTo because it is already considered here // and needed not be used for an other query $model->attributes = array ( 'collectionYearTo' => '' ); // if only from is set } else { $value2 = "*"; } return "gatheringyear:[" . $value . " TO " . $value2 . "]"; } else { // TODO remove when yii validation is doing this return false; } break; // collection year case 'collectionYearTo' : // TODO this has to go to form validation!!! if (floatval ( $value )) { // this is only the case when "to" is set but not "from" // the other cases are already covered in the collectionYearFrom case if (! (floatval ( $model->attributes ['collectionYearFrom'] ))) { return "gatheringyear:[ * TO " . $value . "]"; // if only from is set } } return false; break; default : // this should never happen // TODO error handling return false; break; } // return $query[$field]; } public static function getSolrClient($mainCore) { // the constant values are defined in protected.config.solrConfig.php $client = new SolrClient ( array ( 'hostname' => SOLR_SERVER_HOSTNAME, 'login' => SOLR_SERVER_USERNAME, 'password' => SOLR_SERVER_PASSWORD, 'path' => $mainCore ? SOLR_MAIN_CORE : SOLR_ASSOCIATED_CORE, // 'wt' => 'xml', 'port' => SOLR_SERVER_PORT ) ); return $client; } /** * this fun creates an index query for the search request * * @param unknown $model * @param unknown $selectionLists * @param unknown $requestedAttributes * @param number $pageSize * @return SolrQuery - if any value in the form was set, else return false */ public static function createSearchQuery($model, $selectionLists, $requestedAttributes, $pageSize = 1000000) { $doQuery = false; // ---------- preparations before composing the query --------- // $criteria = new ASolrCriteria (); $criteria = new SolrQuery (); $criteria->setQuery ( "*:*" ); // lucene query syntax // the fields of the result records we want to show (modify them in the SearchController) foreach ( $requestedAttributes as $attribute ) { $criteria->addField ( $attribute ); } $criteria->setRows ( $pageSize ); // create the query // take the values from the form and add a query for them // if they are not empty (filled in by the user) foreach ( $model->attributes as $key => $value ) { if (str_replace ( " ", "", $value ) != "") { // $fq=$key.":".$value; // get query filter for this field // sometimes the fields depend on each other // like longitudeFrom and longitudeTo // so we don't come up with a query for each // field. $fq = GGBNQueryManager::getPartialQuery ( $key, $value, $model ); if ($fq) { $criteria->addFilterQuery ( $fq ); // we found at least one filter, so the // the query will be send $doQuery = true; } } } // $country = $selectionLists ['country'] [$model->attributes ['country']]; // $criteria->query = "country:" . $model->attributes ['country']; // lucene query syntax if ($doQuery) { return $criteria; } else return false; } public static function createAssociatedQuery($tripleIds, $requestedFields) { // create the string for the tripleidstoreis to be queried // TODO pageing instead of a limit $limit = 1000; if (sizeof ( $tripleIds ) > $limit) { $helper = array_chunk ( $tripleIds, $limit ); $tripleIds = $helper [0]; } $tripleIdsString = implode ( self::ACCORDING_TO_SOLR_DEFAULT_OPERATOR, $tripleIds ); // $tripleIdsString ="18492 OR 20651 OR 20659"; // $tripleIdsString ="(28156 OR 28098 OR 28123)"; // create query $criteria = new SolrQuery (); $criteria->setQuery ( "tripleidstoreid:(" . $tripleIdsString . ")" ); foreach ( $requestedFields as $field ) { $criteria->addField ( $field ); } $criteria->setRows ( 100000000 ); return $criteria; } /** * * @param SolrQuery $criteria * @return SolrObject */ public static function getQueryResponse($criteria, $core) { // if (! isset ( $GGBNSolrClient )) { // TODO get this a better performance: $client = GGBNQueryManager::getSolrClient ( $core ); // } /* SolrQueryResponse */ $query_response = $client->query ( $criteria ); return $query_response; // SolrObject } } ?>