Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

<?php
App::uses('HttpSocket', 'Network/Http');
require_once($_SERVER['DOCUMENT_ROOT'].'/app/Vendor/Apache/Solr/Service.php');

class SolrSource extends DataSource {

        protected $_schema = array();
        protected $_host;
        protected $_port;

        public function __construct($config){
                parent::__construct($config);
                $this->Http = new HttpSocket();

                $this->host = $config['host'];
                $this->port = $config['port'];
                $this->_schema = $config['path'];

                $this->solr=new Apache_Solr_Service($this->host, $this->port, $this->_schema);
        }

        public function read(Model $model, $queryData = array(), $recursive = null) {
                $results = array();
                $query = $queryData['conditions']['solr_query'];
                if (Configure::read('log_solr_queries') === true) {
                        CakeLog::write('debug', $query); /* Logs the solr query in app/tmp/logs/debug.log file */
                }
                try {
                        $solr_docs = $this->solr->search($query, $queryData['offset'], $queryData['limit'], array('sort' => $queryData['order']));
                } catch (Exception $e) {
                        CakeLog::write('error', $e->getMessage()); /* Logs the solr errors message in app/tmp/logs/error.log file */
                        CakeLog::write('error', $query); /* Logs the solr query in app/tmp/logs/error.log file */
                }
                $model->params = $solr_docs->responseHeader->params;
                $model->numFound = $solr_docs->response->numFound;
                $docs = $solr_docs->response->docs;
                foreach ($docs as $doc) {
                        $document = array();
                        foreach ($doc as $fieldName => $fieldValue) {
                                $document[$fieldName] = $fieldValue;
                        }
                        $results[] = $document;
                }
                return array($model->alias => $results);
        }
 }
 ?>