// 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) { var_dump($e->errors());
// 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); $objects['current_page'] = $search_response->getFrom() / $docs + 1;