public function search($term) { $query = CPS_Term('*' . $term . '*'); $documents = $this->model->search($query, null, null, null, null); foreach ($documents as $id => $document) { $result[] = $document; } return $result; }
public function refineQuery($query) { if (is_array($query)) { $data = ''; foreach ($query as $key => $value) { if (is_numeric($key)) { $data = $data . CPS_Term($value); } else { $data = $data . CPS_Term($value, $key); } } return $data; } else { return $query; } }
/** * Defines which tags of the search results should be listed in the response * @param array $array an associative array with tag xpaths as keys and listing options (yes, no, snippet or highlight) as values */ public function setList($array) { $listString = ''; foreach ($array as $key => $value) { $listString .= CPS_Term($value, $key); } $this->setParam('list', $listString); }
<?php // includes require_once 'config.php'; require_once '../cps_simple.php'; try { // creating a CPS_Connection instance $cpsConnection = new CPS_Connection($config['connection'], $config['database'], $config['username'], $config['password'], 'document', '//document/id', array('account' => $config['account'])); // creating a CPS_Simple instance $cpsSimple = new CPS_Simple($cpsConnection); // search for items with category == 'cars' and car_params/year >= 2010 $query = CPS_Term('cars', 'category') . CPS_Term('>=2010', 'car_params/year'); // return documents starting with the first one - offset 0 $offset = 0; // return not more than 5 documents $docs = 5; // return these fields from the documents $list = array('id' => 'yes', 'car_params/make' => 'yes', 'car_params/model' => 'yes', 'car_params/year' => 'yes'); // order by year, from largest to smallest $ordering = CPS_NumericOrdering('car_params/year', 'descending'); $documents = $cpsSimple->search($query, $offset, $docs, $list, $ordering); foreach ($documents as $id => $document) { echo $document->car_params->make . ' ' . $document->car_params->model . '<br />'; echo 'first registration in ' . $document->car_params->year . '<br />'; } } catch (CPS_Exception $e) { var_dump($e->errors()); exit; }
/** * @param $countryID * @return array * @throws \CPS_Exception * @throws \Exception * @throws null */ public function getOnlineUsers($countryID) { $query = CPS_Term($countryID, 'country'); $list = array('id' => 'yes', "x" => 'yes', "y" => 'yes', "about" => "yes", "email" => "yes"); try { $documents = $this->cpsSimple->search($query, NULL, NULL, $list); } catch (\Exception $e) { return -2; } $results = []; foreach ($documents as $id => $document) { $tempResult = []; $tempResult["x"] = $document->x->__toString(); $tempResult["y"] = $document->y->__toString(); $tempResult["description"] = $document->about->__toString(); $tempResult["calls_count"] = 0; $tempResult["receive_calls_count"] = 0; $tempResult["rate"] = 0; $tempResult["minutes_spoken"] = 0; if (isset($document->conversations->conversation)) { //-----------------------------SUM CONVERSATION OUTGOING---------------- $count = 0; foreach ($document->conversations->conversation as $conversation) { if ($conversation->is_incoming == 0) { $tempResult["calls_count"]++; $tempResult["rate"] += $conversation->rate; } $tempResult["minutes_spoken"] += $conversation->duration; $count++; } //-----------------------------SUM CONVERSATION INCOMMING---------------- $tempResult["receive_calls_count"] = $count - $tempResult["calls_count"]; //-----------------------------AVG OF RATE CONVERSATION ---------------- if ($count > 0) { $tempResult["rate"] = $tempResult["rate"] / $count; } } $tempResult["countries_to"] = ["IR"]; //TODO make it more general $tempResult["languages"] = Converter::XML2Array($document->languages)["languages"]; $results[] = $tempResult; } return $results; }
<?php // includes include_once 'config.php'; require_once '../cps_api.php'; $config['database'] = 'public::osm_poi'; try { // creating a CPS_Connection instance $cpsConnection = new CPS_Connection($config['connection'], $config['database'], $config['username'], $config['password'], 'document', '//document/id', array('account' => $config['account'])); $cpsConnection->setDebug(1); $offset = 0; $docs = 30; $listXml = '<id>yes</id> <lat>yes</lat> <lon>yes</lon>'; $query = '{' . CPS_Term(" ><exampleshape1") . CPS_Term(" ><exampleshape2") . '}'; // $query = CPS_Term(" ><exampleshape2"); // $query = CPS_Term('cars', 'category') . CPS_Term('>=2010', 'car_params/year'); $searchRequest = new CPS_SearchRequest($query, $offset, $docs); $searchRequest->setParam('list', $listXml); $shape1 = CPS_CircleDefinition("exampleshape1", array(48.060736, -2.6902734), '1 km', 'lat', 'lon'); $shape2 = CPS_PolygonDefinition("exampleshape2", array(array(50.5735874, 9.997238299999999), array(50.574689, 10.006046), array(50.564493, 10.008982)), 'lat', 'lon'); $shapes = '<shapes> <exampleshape1> <center>48.060736 -2.6902734</center> <radius>1 km</radius> <coord1_tag_name>lat</coord1_tag_name> <coord2_tag_name>lon</coord2_tag_name> </exampleshape1> <exampleshape2>50.5735874 9.9972383; 50.574689 10.006046; 50.564493 10.008982; <coord1_tag_name>lat</coord1_tag_name> <coord2_tag_name>lon</coord2_tag_name> </exampleshape2>
/** * Performs an alternatives command for the specified query. Returns a string with the most probable query (could be identical to the given one) * * @param string $query see {@link CPS_AlternativesRequest::setQuery()} * @param float $cr see {@link CPS_AlternativesRequest::setCr()} * @param float $idif see {@link CPS_AlternativesRequest::setIdif()} * @param float $h see {@link CPS_AlternativesRequest::setH()} * @return array */ public function alternatives($query, $cr = NULL, $idif = NULL, $h = NULL) { $request = new CPS_AlternativesRequest($query, $cr, $idif, $h); $this->_lastResponse = $this->_connection->sendRequest($request); $words = $this->_lastResponse->getWords(); $res = ''; $xp_accum = ''; $prevxpath = NULL; foreach ($words as $original => $spellings) { $xpath = ''; if (($pos = strpos($original, '/')) !== FALSE) { $xpath = substr($original, 0, $pos); $original = substr($original, $pos + 1); } if (!is_null($prevxpath) && $xpath != $prevxpath) { $res .= CPS_Term($xp_accum, $prevxpath); $xp_accum = ''; } $prevxpath = $xpath; if (count($spellings) > 0) { foreach ($spellings as $key => $n) { $xp_accum .= ($xp_accum == '' ? '' : ' ') . $key; break; } } else { $xp_accum .= ($xp_accum == '' ? '' : ' ') . $original; } } if (strlen($xp_accum) > 0) { $res .= ($res == '' ? '' : ' ') . CPS_Term($xp_accum, is_null($prevxpath) ? '' : $prevxpath); } return $res; }
// search inside of defined circle if ($q !== '') { $query .= CPS_Term($q); // if there was search phrase, attach it to the query } $query_without_amenities = $query; $aggregate = 'DISTINCT tags.amenity'; // aggregate distinct values of amenity tag if (count($amenities)) { if (array_search('-', $amenities) !== false) { // if there is amenity with value "-", this means that "Amenity is not set" was selected // lets replace - with ="" which tells Clusterpoint to look for documents for which this tag does not exist $amenities[array_search('-', $amenities)] = '=""'; } // add amenities filter to search request $query .= CPS_Term('{ ' . implode(' ', $amenities) . ' }', 'tags/amenity'); } $search_request = new CPS_SearchRequest($query, $offset, $docs); $search_request->setParam('list', '<document>yes</document>'); // search should return full documents $search_request->setShape($circle); // attach previously defined circle definition if ($query_without_amenities == $query) { // if there was no amenities filter, we can aggregate data together with search request $search_request->setAggregate($aggregate); } $search_response = $cps_connection->sendRequest($search_request); if ($search_response->getHits() > 0) { $objects['total'] = $search_response->getHits(); $objects['from'] = $search_response->getFrom() + 1; // getFrom() returns offset. We want to show offset +1
<?php // includes include_once 'config.php'; require_once '../cps_api.php'; try { // creating a CPS_Connection instance $cpsConnection = new CPS_Connection($config['connection'], $config['database'], $config['username'], $config['password'], 'document', '//document/id', array('account' => $config['account'])); $cpsConnection->setDebug(1); // Setting parameters // search for items with category == 'cars' and car_params/year >= 2010 $query = CPS_Term('555', 'id'); // return documents starting with the first one - offset 0 $offset = 0; // return not more than 5 documents $docs = 5; // return these fields from the documents $list = array(); // Searching for documents // note that only the query parameter is mandatory - the rest are optional $searchRequest = new CPS_SearchRequest($query, $offset, $docs, $list); $searchResponse = $cpsConnection->sendRequest($searchRequest); if ($searchResponse->getHits() > 0) { // getHits returns the total number of documents in the storage that match the query echo 'Found ' . $searchResponse->getHits() . " documents\n"; var_dump($searchResponse->getDocuments()[555]->asXML()); } else { echo 'Nothing found.'; } } catch (CPS_Exception $e) { var_dump($e->errors());