forked from abhimir/yii-sphinx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESphinxConnection.php
275 lines (242 loc) · 7.01 KB
/
ESphinxConnection.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
/**
* This file contains filesource of class ESphinxConnection
*/
/**
* Class ESphinxConnection
*
* @author mitallast <mitallast@gmail.com>
* @version 0.1
* @since 0.1
* @package sphinx
*/
class ESphinxConnection extends EBaseSphinxConnection
{
/**
* Instance of SphinxClient
* @var SphinxClient $sphinxClient
*/
private $sphinxClient;
/**
* Flag check is client connected
* @var bool defaults false
*/
private $isConnected = false;
/**
* Constructor initialise component variables.
*/
public function __construct()
{
$this->sphinxClient = new SphinxClient();
$this->sphinxClient->SetArrayResult(true);
}
public function setServer(array $parameters = array())
{
if(!isset ($parameters[0]))
$parameters[0] = 'localhost';
if(!isset ($parameters[1]))
$parameters[1] = 3386;
$this->sphinxClient->SetServer($parameters[0],$parameters[1]);
}
public function openConnection()
{
if($this->isConnected)
throw new ESphinxException ("Sphinx client is already opened");
else
{
$this->sphinxClient->Open();
if($this->sphinxClient->IsConnectError())
throw new ESphinxException("Sphinx exception: ".$this->sphinxClient->GetLastError());
$this->isConnected = true;
}
}
public function closeConnection()
{
if(!$this->isConnected)
throw new ESphinxException ("Sphinx client is already closed");
else
{
$this->sphinxClient->Open();
$this->isConnected = false;
}
}
public function getIsConnected()
{
return $this->isConnected;
}
public function setConnectionTimeout($timeout)
{
$this->sphinxClient->SetConnectTimeout((int)$timeout);
}
public function setQueryTimeout( $timeout )
{
$this->sphinxClient->SetMaxQueryTime((int)$timeout);
}
public function createExcerts(array $docs, $index, $words, array $opts = array())
{
return $this->sphinxClient->BuildExcerpts($docs, $index, $words, $opts);
}
public function createKeywords($query, $index, $hits = false)
{
return $this->sphinxClient->BuildKeywords($query, $index, $hits);
}
public function escape($string)
{
return $this->sphinxClient->EscapeString((string)$string);
}
public function update($index, array $attrs, array $values, $mfa=false)
{
return $this->sphinxClient->UpdateAttributes($index, $attrs, $values, $mfa);
}
public function executeQuery(ESphinxQuery $query)
{
$this->resetClient();
$this->applyQuery($query);
$results = $this->execute();
return $results[0];
}
public function executeQueries(array $queries)
{
$this->resetClient();
foreach ($queries as $query)
$this->applyQuery($query);
return $this->execute();
}
protected function applyQuery(ESphinxQuery $query)
{
$this->applyCriteria($query->getCriteria());
$this->sphinxClient->AddQuery($query->getText(), $query->getIndexes());
}
protected function applyCriteria(ESphinxCriteria $criteria)
{
$this->applyMatchMode($criteria->matchMode);
$this->applyRankMode($criteria->rankMode);
$this->applySortMode($criteria->sortMode);
// apply select
if(strlen($criteria->select))
$this->sphinxClient->SetSelect($criteria->select);
// apply limit
if($criteria->getIsLimited())
$this->sphinxClient->SetLimits(
$criteria->offset,
$criteria->limit,
$criteria->max_matches,
$criteria->cutoff
);
// apply group
if($criteria->getIsGroupSetted())
$this->sphinxClient->SetGroupBy($criteria->getGroupBy(), $criteria->getGroupFunc());
// apply id range
if($criteria->getIsIdRangeSetted())
$this->sphinxClient->SetIDRange(
$criteria->getIdMin(),
$criteria->getIdMax()
);
// apply weights
$this->applyFieldWeights($criteria->getFieldWeights());
$this->applyIndexWeights($criteria->getIndexWeights());
// apply filters
$this->applyFilters($criteria->getInConditions());
$this->applyFilters($criteria->getNotInConditions(), true);
// apply ranges
$this->applyRanges($criteria->getInRanges());
$this->applyRanges($criteria->getNotInRanges(),true);
}
protected function applyRanges(array $ranges, $exclude=false)
{
$exclude = (boolean)$exclude;
foreach($ranges as $field => $range)
{
$isFloat = is_float($range['max']) || is_float($range['min']);
if($isFloat)
$this->sphinxClient->SetFilterRange(
$field,
(float)$range['min'],
(float)$range['max'],
$exclude
);
else
$this->sphinxClient->SetFilterRange(
$field,
(int)$range['min'],
(int)$range['max'],
$exclude
);
}
}
protected function applyFilters(array $conditions, $exclude = false)
{
$exclude = (boolean)$exclude;
foreach ($conditions as $field => $values)
{
$this->sphinxClient->SetFilter($field, $values, $exclude);
}
}
protected function applyIndexWeights(array $weights)
{
foreach( $weights as $index => $weight )
$weights[$index] = (int)$weight;
$this->sphinxClient->SetIndexWeights($weights);
}
protected function applyFieldWeights(array $weights)
{
foreach( $weights as $field => $weight )
$weights[$field] = (int)$weight;
$this->sphinxClient->SetFieldWeights($weights);
}
protected function applySortMode($mode)
{
$mode = (int)$mode;
if(in_array($mode,ESphinxCriteria::$sortModes))
$this->sphinxClient->SetSortMode($mode);
else
throw new ESphinxException("Search mode {$mode} is undefined");
}
protected function applyMatchMode($mode)
{
$mode = (int)$mode;
if(in_array($mode, ESphinxCriteria::$matchModes))
$this->sphinxClient->SetMatchMode($mode);
else
throw new ESphinxException("Match mode {$mode} is not defined");
}
protected function applyRankMode($mode)
{
$mode = (int)$mode;
if(in_array($mode, ESphinxCriteria::$rankModes))
$this->sphinxClient->SetRankingMode($mode);
}
protected function resetClient()
{
$this->sphinxClient->ResetFilters();
$this->sphinxClient->ResetGroupBy();
$this->sphinxClient->ResetOverrides();
$this->sphinxClient->SetLimits(0, 20);
$this->sphinxClient->SetArrayResult(true);
$this->sphinxClient->SetFieldWeights(array());
$this->sphinxClient->SetIDRange(0,0);
$this->sphinxClient->SetIndexWeights(array());
$this->sphinxClient->SetMatchMode(SPH_MATCH_EXTENDED2);
$this->sphinxClient->SetRankingMode(SPH_RANK_NONE);
$this->sphinxClient->SetSortMode(SPH_SORT_RELEVANCE, "");
$this->sphinxClient->SetSelect("*");
}
protected function execute()
{
$sph = $this->sphinxClient->RunQueries();
if( $error = $this->sphinxClient->GetLastError() )
throw new ESphinxException($error);
if( $error = $this->sphinxClient->GetLastWarning() )
throw new ESphinxException($error);
if( !is_array($sph) )
throw new ESphinxException("Sphinx client returns result not array");
$results = array();
foreach($sph as $result)
{
if(isset($result['error']) && strlen($result['error']))
throw new ESphinxException($result['error']);
$results[] = new ESphinxResult($result);
}
return $results;
}
}