Exemple #1
0
 /**
  * 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);
 }
    // 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());
    exit;