forked from bejean/advanced-search-by-my-solr-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solr.class.inc.php
131 lines (111 loc) · 3.23 KB
/
solr.class.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
Class Mss_Solr {
private $_solr = null;
private $_solrHost = null;
private $_solrPort = null;
private $_solrPath = null;
private $_lastErrorCode = "";
private $_lastErrorMessage = "";
public function Mss_Solr () {}
public function getLastErrorCode() {
return $this->_lastErrorCode;
}
public function getLastErrorMessage() {
return $this->_lastErrorMessage;
}
public function connect($options, $ping = false) {
// get the connection options
$this->_solrHost = $options['mss_solr_host'];
$this->_solrPort = $options['mss_solr_port'];
$this->_solrPath = $options['mss_solr_path'];
// double check everything has been set
if ( ! ($this->_solrHost and $this->_solrPort and $this->_solrPath) ) {
$this->_lastErrorCode = -1;
$this->_lastErrorMessage = "Invalid options";
return false;
}
// create the solr service object
try {
require_once("SolrPhpClient/Apache/Solr/HttpTransport/Curl.php");
$httpTransport = new Apache_Solr_HttpTransport_Curl();
$this->_solr = new Apache_Solr_Service($this->_solrHost, $this->_solrPort, $this->_solrPath, $httpTransport);
if ($options['mss_solr_proxy']!='' && $options['mss_solr_proxyport']!='') {
$this->_solr->setProxy($options['mss_solr_proxy'], $options['mss_solr_proxyport'], $options['mss_slor_proxyusername'], decrypt($options['mss_solr_proxypassword']));
}
} catch ( Exception $e ) {
$this->_lastErrorCode = $e->getCode();
$this->_lastErrorMessage = $e->getMessage();
return false;
}
// if we want to check if the server is alive, ping it
if ($ping) {
try {
if (!$this->_solr->ping()) {
//$this->_solr = null;
$this->_lastErrorCode = -1;
$this->_lastErrorMessage = "Ping failed";
return false;
}
} catch ( Exception $e ) {
$this->_lastErrorCode = $e->getCode();
$this->_lastErrorMessage = $e->getMessage();
return false;
}
}
return true;
}
public function commit() {
try {
$this->_solr->commit();
return true;
} catch ( Exception $e ) {
$this->_lastErrorCode = $e->getCode();
$this->_lastErrorMessage = $e->getMessage();
return false;
}
}
public function optimize() {
try {
$this->_solr->optimize();
return true;
} catch ( Exception $e ) {
$this->_lastErrorCode = $e->getCode();
$this->_lastErrorMessage = $e->getMessage();
return false;
}
}
public function addDocuments($documents) {
try {
$this->_solr->addDocuments($documents);
return true;
} catch ( Exception $e ) {
$this->_lastErrorCode = $e->getCode();
$this->_lastErrorMessage = $e->getMessage();
return false;
}
}
public function deleteAll($optimize = false) {
try {
$this->_solr->deleteByQuery('*:*');
if (!$this->commit()) return false;
if (optimize) return $this->optimize();
return true;
} catch ( Exception $e ) {
$this->_lastErrorCode = $e->getCode();
$this->_lastErrorMessage = $e->getMessage();
return false;
}
}
public function deleteById( $doc_id ) {
try {
$this->_solr->deleteById( $doc_id );
$this->_solr->commit();
} catch ( Exception $e ) {
echo $e->getMessage();
}
}
public function search($qry, $offset, $count, $params) {
return $this->_solr->search($qry, $offset, $count, $params);
}
}
?>