Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13815 anikendra 1
<?php
2
App::uses('HttpSocket', 'Network/Http');
3
require_once($_SERVER['DOCUMENT_ROOT'].'/app/Vendor/Apache/Solr/Service.php');
4
 
5
class SolrSource extends DataSource {
6
 
7
	protected $_schema = array();
8
	protected $_host;
9
	protected $_port;
10
 
11
	public function __construct($config){
12
		parent::__construct($config);
13
		$this->Http = new HttpSocket();
14
 
15
		$this->host = $config['host'];
16
		$this->port = $config['port'];
17
		$this->_schema = $config['path'];
18
 
19
		$this->solr=new Apache_Solr_Service($this->host, $this->port, $this->_schema);
20
	}
21
 
22
	public function read(Model $model, $queryData = array(), $recursive = null) {
23
		$results = array();
24
		$query = $queryData['conditions']['solr_query'];
25
		if (Configure::read('log_solr_queries') === true) {
26
			CakeLog::write('debug', $query); /* Logs the solr query in app/tmp/logs/debug.log file */
27
		}
28
		try {
29
			$solr_docs = $this->solr->search($query, $queryData['offset'], $queryData['limit'], array('sort' => $queryData['order']));
30
		} catch (Exception $e) {
31
			CakeLog::write('error', $e->getMessage()); /* Logs the solr errors message in app/tmp/logs/error.log file */
32
			CakeLog::write('error', $query); /* Logs the solr query in app/tmp/logs/error.log file */
33
		}
34
		$model->params = $solr_docs->responseHeader->params;
35
		$model->numFound = $solr_docs->response->numFound;
36
		$docs = $solr_docs->response->docs;
37
		foreach ($docs as $doc) {
38
			$document = array();
39
			foreach ($doc as $fieldName => $fieldValue) {
40
				$document[$fieldName] = $fieldValue;
41
			}
42
			$results[] = $document;
43
		}
44
		return array($model->alias => $results);
45
	}
46
 }
47
 ?>