/** * Performs a search command. Returns the documents found in an associative array with document IDs as keys and document contents as values * @param array|string $query The query array/string. see {@link CPS_SearchRequest::setQuery()} for more info on best practices. * @param int $offset Defines the number of documents to skip before including them in the results * @param int $docs Maximum document count to retrieve * @param array $list Listing parameter - an associative array with tag xpaths as keys and listing options (yes | no | snippet | highlight) as values * @param string|array $ordering Defines the order in which results will be returned. Contains either a single sorting string or an array of those. Could be conveniently generated with ordering macros, e.g. $q->setOrdering(array(CPS_NumericOrdering('user_count', 'desc'), CPS_RelevanceOrdering())) will sort the documents in descending order according to the user_count, and if user_count is equal will sort them by relevance. * @param int $returnType defines which datatype the returned documents will be in. Default is DOC_TYPE_SIMPLEXML, other possible values are DOC_TYPE_ARRAY and DOC_TYPE_STDCLASS * @return array */ public function search($query, $offset = NULL, $docs = NULL, $list = NULL, $ordering = NULL, $returnType = DOC_TYPE_SIMPLEXML, $stemLang = NULL) { $request = new CPS_SearchRequest($query, $offset, $docs, $list); if (!is_null($ordering)) { $request->setOrdering($ordering); } if (!is_null($stemLang)) { $request->setStemLang($stemLang); } $this->_lastResponse = $this->_connection->sendRequest($request); return $this->_lastResponse->getDocuments($returnType); }
$cpsConnection->setDebug(1); // Setting parameters // 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'); $listXml = '<id>yes</id> <car_params><make>yes</make></car_params> <car_params><model listas="modelis">yes</model></car_params> <car_params><year>yes</year></car_params>'; // order by year, from largest to smallest $ordering = CPS_NumericOrdering('car_params/year', 'descending'); // Searching for documents // note that only the query parameter is mandatory - the rest are optional $searchRequest = new CPS_SearchRequest($query, $offset, $docs); $searchRequest->setParam('list', $listXml); $searchRequest->setOrdering($ordering); $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<br />'; echo 'showing from ' . $searchResponse->getFrom() . ' to ' . $searchResponse->getTo() . '<br />'; foreach ($searchResponse->getDocuments() as $id => $document) { echo $document->car_params->make . ' ' . $document->car_params->model . '<br />'; echo 'first registration in ' . $document->car_params->year . '<br />'; } } else { echo 'Nothing found.'; } } catch (CPS_Exception $e) {
// creating a CPS_Connection instance $cpsConnection = new CPS_Connection($config['connection'], $config['database'], $config['username'], $config['password'], 'document', '//document/id', array('account' => $config['account'])); // Setting parameters // 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'); // Searching for documents // note that only the query parameter is mandatory - the rest are optional $searchRequest = new CPS_SearchRequest($query, $offset, $docs, $list); $searchRequest->setOrdering($ordering); $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<br />'; echo 'showing from ' . $searchResponse->getFrom() . ' to ' . $searchResponse->getTo() . '<br />'; foreach ($searchResponse->getDocuments() as $id => $document) { echo $document->car_params->make . ' ' . $document->car_params->model . '<br />'; echo 'first registration in ' . $document->car_params->year . '<br />'; } } else { echo 'Nothing found.'; } } catch (CPS_Exception $e) { var_dump($e->errors());
// 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> </shapes>';
$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 $objects['to'] = $search_response->getTo(); $objects['pages'] = ceil($objects['total'] / $docs);