-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSphinxClient.class.php
More file actions
240 lines (197 loc) · 4.31 KB
/
SphinxClient.class.php
File metadata and controls
240 lines (197 loc) · 4.31 KB
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
<?php
/**
* Class to interact with Sphinx using mysql protocol (SphinxQL)
*
* @author Martín Ernesto Barreyro, Marc St Raymond
*/
class HeardaboutSphinxClient
{
private $select;
private $from;
private $limit;
private $groupBy;
private $kwdsToMatch;
private $orderBy;
private $where;
private $extraMatch;
private $conn = null;
public function __construct()
{
}
private function getConnection()
{
if($this->conn == null)
{
$pdo = new PDO(sfConfig::get('app_sphinx_dsn'));
$this->conn = Doctrine_Manager::getInstance()->openConnection($pdo, 'sphinx', false);
}
return $this->conn;
}
public function updateRtIndex($sphinxql = "")
{
$this->getConnection()->execute($sphinxql);
}
public function updateMVA($index, $field, $value, $id)
{
$sphinxql = 'UPDATE ' . $index . ' SET ' . $field . '=(' . $value . ') WHERE id=' . $id;
$this->getConnection()->execute($sphinxql);
}
public function querySphinx($sphinxql = "")
{
if($sphinxql == "")
{
$sphinxql = $this->createQuery();
}
return $this->getConnection()->fetchAssoc($sphinxql);
}
public function getMetas()
{
return $this->getConnection()->fetchAssoc("SHOW META;");
}
public function getTotalFound()
{
$metas = $this->getMetas();
$total = 0;
foreach ($metas as $value)
{
if($value['Variable_name'] == 'total_found')
{
$total = $value['Value'];
}
}
return $total;
}
public function setFrom($from)
{
$this->from = $from;
return $this;
}
public function setSelect($select)
{
$this->select = $select;
return $this;
}
//takes a string with two numbers, offset and limit
public function setLimit($limit)
{
$this->limit = $limit;
return $this;
}
public function setGroupBy($groupBy)
{
$this->groupBy = $groupBy;
return $this;
}
public function setMatch($kwds)
{
$this->kwdsToMatch = $kwds;
return $this;
}
public function setExtraMatches($extraMatch)
{
$this->extraMatch = $extraMatch;
return $this;
}
public function setWhere($where)
{
$this->where = $where;
return $this;
}
public function setOrderBy($orderBy)
{
$this->orderBy = $orderBy;
return $this;
}
public function getMatch()
{
return $this->extraMatch;
}
public function getExtraMatch()
{
return $this->extraMatch;
}
public function createQuery()
{
$sphinxql = "SELECT {$this->select} ";
$sphinxql .= "FROM {$this->from} ";
if($this->where || $this->kwdsToMatch || $this->extraMatch)
{
$sphinxql .= "WHERE ";
if($this->kwdsToMatch)
{
$sphinxql .= "MATCH('".$this->prepareWordsSearchToSphinx($this->kwdsToMatch)." {$this->extraMatch}') ";
}
elseif($this->extraMatch)
{
$sphinxql .= "MATCH('{$this->extraMatch}') ";
}
if($this->where)
{
if($this->kwdsToMatch || $this->extraMatch)
{
$sphinxql .= "AND {$this->where} ";
}
else
{
$sphinxql .= " {$this->where} ";
}
}
}
if($this->groupBy)
$sphinxql .= "GROUP BY {$this->groupBy} ";
if($this->orderBy)
$sphinxql .= "ORDER BY {$this->orderBy} ";
if($this->limit)
$sphinxql .= "LIMIT {$this->limit} ";
return $sphinxql;
}
/**
* Function prepareSearchToSphinx
*
* Logic to add "*" at the start and end of all words (separators are ".", "," and " ")
*
* @param <string> $query
*/
protected function prepareWordsSearchToSphinx($query_internal)
{
$arrWords = array();
$word = "";
if ($query_internal)
{
for ($contChar = 0; $contChar < strlen($query_internal); $contChar++)
{
if (in_array($query_internal[$contChar], array('.', ',', ' ')))
{
if ($word && $word != "*")
{
$arrWords[] = $word . "*";
}
$word = "*";
if ($query_internal[$contChar] != " ")
{
$contChar++;
}
}
else
{
if ($contChar == 0)
{
$word = "*";
}
$word.=$query_internal[$contChar];
if (strlen($query_internal) == $contChar + 1)
{
$arrWords[] = $word . "*";
$word = "*";
}
}
}
}
$query_internal = implode(" ", $arrWords);
if (trim($query_internal) != "")
{
$query_internal = "( " . $query_internal . " )";
}
return $query_internal;
}
}