-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsphinxapi.lua
478 lines (368 loc) · 12.4 KB
/
sphinxapi.lua
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
-- Copyright (C) 2014 nd791899 [email protected]
local bit = require "bit"
local band = bit.band
local lshift = bit.lshift
local rshift = bit.rshift
local SPH_TRUE = 1
local SPH_FALSE = 0
local SPH_MATCH_ALL = 0
local SPH_MATCH_ANY = 1
local SPH_MATCH_PHRASE = 2
local SPH_MATCH_BOOLEAN = 3
local SPH_MATCH_EXTENDED = 4
local SPH_MATCH_FULLSCAN = 5
local SPH_MATCH_EXTENDED2 = 6
local SPH_SORT_RELEVANCE = 0
local SPH_SORT_ATTR_DESC = 1
local SPH_SORT_ATTR_ASC = 2
local SPH_SORT_TIME_SEGMENTS = 3
local SPH_SORT_EXTENDED = 4
local SPH_SORT_EXPR = 5
local SPH_GROUPBY_DAY = 0
local SPH_GROUPBY_WEEK = 1
local SPH_GROUPBY_MONTH = 2
local SPH_GROUPBY_YEAR = 3
local SPH_GROUPBY_ATTR = 4
local SPH_GROUPBY_ATTRPAIR = 5
local SPH_RANK_PROXIMITY_BM25 = 0
local SPH_RANK_BM25 = 1
local SPH_RANK_NONE = 2
local SPH_RANK_WORDCOUNT = 3
local SPH_RANK_PROXIMITY = 4
local SPH_RANK_MATCHANY = 5
local SPH_RANK_FIELDMASK = 6
local SPH_RANK_SPH04 = 7
local SPH_RANK_DEFAULT = SPH_RANK_PROXIMITY_BM25
local MAX_REQS =32
local SEARCHD_COMMAND_SEARCH = 0
local SEARCHD_COMMAND_EXCERPT = 1
local SEARCHD_COMMAND_UPDATE = 2
local SEARCHD_COMMAND_KEYWORDS = 3
local SEARCHD_COMMAND_PERSIST = 4
local SEARCHD_COMMAND_STATUS = 5
local SPH_ATTR_INTEGER = 1
local SPH_ATTR_TIMESTAMP = 2
local SPH_ATTR_ORDINAL = 3
local SPH_ATTR_BOOL = 4
local SPH_ATTR_FLOAT = 5
local SPH_ATTR_BIGINT = 6
local SPH_ATTR_STRING = 7
local SPH_ATTR_MULTI = 0x40000001
local SPH_ATTR_MULTI64 = 0x40000002
local SPH_FILTER_VALUES = 0
local SPH_FILTER_RANGE = 1
local SPH_FILTER_FLOATRANGE = 2
local sphinx ={ver_search =0x119}
local sx = { __index = sphinx }
local _sphinx_client ={ver_search =0x119}
_sphinx_client.copy_args=SPH_TRUE
_sphinx_client.head_alloc=Nil
_sphinx_client.error=Nil
_sphinx_client.warning=Nil
_sphinx_client.local_error_buf=Nil
_sphinx_client.host="localhost"
_sphinx_client.port=9312
_sphinx_client.timeout=0
_sphinx_client.offset= 0;
_sphinx_client.limit=20
_sphinx_client.mode=SPH_MATCH_ALL
_sphinx_client.num_weights=0
_sphinx_client.weights={}
_sphinx_client.sort=SPH_SORT_RELEVANCE
_sphinx_client.sortby = ""
_sphinx_client.minid = 0
_sphinx_client.maxid= 0
_sphinx_client.group_by = ""
_sphinx_client.group_func = SPH_GROUPBY_ATTR
_sphinx_client.group_sort = "@groupby desc"
_sphinx_client.group_distinct = ""
_sphinx_client.max_matches = 1000
_sphinx_client.cutoff = 0
_sphinx_client.retry_count = 0
_sphinx_client.retry_delay = 0
_sphinx_client.geoanchor_attr_lat = Nil
_sphinx_client.geoanchor_attr_long = Nil
_sphinx_client.geoanchor_lat = 0
_sphinx_client.geoanchor_long = 0
_sphinx_client.num_filters = 0
_sphinx_client.max_filters = 0
_sphinx_client.filters = Nil
_sphinx_client.num_index_weights = 0
_sphinx_client.index_weights_names = Nil
_sphinx_client.index_weights_values = Nil
_sphinx_client.ranker = SPH_RANK_DEFAULT
_sphinx_client.max_query_time = 0
_sphinx_client.num_field_weights = 0
_sphinx_client.field_weights_names = {}
_sphinx_client.field_weights_values = {}
_sphinx_client.num_overrides = 0
_sphinx_client.max_overrides = 0
_sphinx_client.overrides = Nil
_sphinx_client.select_list = ""
_sphinx_client.num_reqs = 0
_sphinx_client.response_len = 0
_sphinx_client.response_buf = Nil
_sphinx_client.num_results = {}
for i=1,MAX_REQS do
result={}
result.values_pool = Nil;
result.words = Nil;
result.fields = Nil;
result.attr_names = Nil;
result.attr_types = Nil;
table.insert(_sphinx_client.num_results, result);
end
_sphinx_client.sock = -1;
_sphinx_client.persist = SPH_FALSE;
function send_int(value,str)
str = str .. string.char(band(rshift(value,24),255)) .. string.char(band(rshift(value,16),255)) .. string.char(band(rshift(value,8),255)) .. string.char(band(value,255))
return str
end
function send_float(value,str)
str = str .. string.char(band(rshift(value,24),255)) .. string.char(band(rshift(value,16),255)) .. string.char(band(rshift(value,8),255)) .. string.char(band(value,255))
return str
end
function send_word(value,str)
str = str .. string.char(band(rshift(value,8),255)) .. string.char(band(value,255))
return str
end
function send_str(value,str)
str=send_int(string.len(value),str)
str = str .. value
return str
end
function send_qword(value,str)
str=send_int ( rshift(value,32) , str );
str=send_int ( band(value, 0xffffffff) ,str );
return str
end
function unpack_short(value)
val= string.byte(value,2)
val= val + string.byte(value,2)*256
return string.sub(value,3,#value),val
end
function unpack_int(value)
val= string.byte(value,4)
val= val + string.byte(value,3)*256
val= val + string.byte(value,2)*256*256
val= val + string.byte(value,1)*256*256*256
return string.sub(value,5,#value),val
end
function unpack_str(value)
local value,len= unpack_int(value)
local val = string.sub(value,len+1,#value)
local str = string.sub(value,1,len)
return val,str
end
function unpack_qword(value)
local value,high= unpack_int(value)
local value,low= unpack_int(value)
return value,high*2^32+low
end
function sphinx.new(self)
local client = _sphinx_client
client.sock, err = ngx.socket.tcp()
if not client.sock then
return nil, "not initialized"
end
return setmetatable({ client = client },sx)
end
function sphinx.set_server(self,host,port)
self.client.host = host
self.client.port = port
ok, err = self.client.sock:connect(host, port)
return ok,err
end
function sphinx.set_match_mode(self,mode)
self.client.mode = mode
end
function sphinx.set_sort_mode(self,mode,sortby)
self.client.mode = mode
self.client.sortby = sortby
end
function sphinx.set_field_weights(self,num_weights,field_names,field_weights)
self.client.num_field_weights = num_weights
self.client.field_weights_names = field_names
self.client.field_weights_values = field_weights
end
function sphinx.add_query(self,query,index_list, comment)
req=""
req=send_int(self.client.offset,req)
req=send_int(self.client.limit,req)
req=send_int(self.client.mode,req)
req=send_int(self.client.ranker,req)
req=send_int(self.client.sort,req)
req=send_str(self.client.sortby,req)
req=send_str(query,req)
req=send_int(#self.client.weights,req)
for i=1,#self.client.weights do
req=send_int( self.client.weights[i],req )
end
req=send_str ( index_list,req );
req=send_int ( 1,req ) -- id range bits
req=send_qword (self.client.minid,req )
req=send_qword(self.client.maxid,req )
req=send_int(self.client.num_filters,req )
for i=1, self.client.num_filters do
req=send_str ( self.client.filters[i].attr,req )
req=send_int ( self.client.filters[i].filter_type,req )
if self.client.filters[i].filter_type == SPH_FILTER_VALUES then
req=send_int ( self.client.filters[i].num_values,req )
if self.client.ver_search>=0x114 then
for j=1, self.client.filters[i].num_values do
req=send_qword (self.client.filters[i].values[j],req)
end
else
for j=1, self.client.filters[i].num_values do
req=send_int ( self.client.filters[i].values[j],req )
end
end
elseif self.client.filters[i].filter_type == SPH_FILTER_RANGE then
if self.client.ver_search>=0x114 then
req=send_qword ( self.client.filters[i].umin ,req )
req=send_qword ( self.client.filters[i].umax,req )
else
req=send_int ( self.client.filters[i].umin,req )
req=send_int ( self.client.filters[i].umax ,req )
end
elseif self.client.filters[i].filter_type == SPH_FILTER_FLOATRANGE then
req=send_float ( self.client.filters[i].fmin ,req)
req=send_float ( self.client.filters[i].fmax ,req)
end
req=send_int ( self.client.filters[i].exclude ,req)
end
req=send_int ( self.client.group_func,req )
req=send_str ( self.client.group_by,req )
req=send_int ( self.client.max_matches,req )
req=send_str ( self.client.group_sort,req )
req=send_int ( self.client.cutoff,req )
req=send_int ( self.client.retry_count,req )
req=send_int ( self.client.retry_delay,req )
req=send_str ( self.client.group_distinct,req )
if self.client.geoanchor_attr_lat or self.client.geoanchor_attr_long then
req=send_int ( 1,req )
req=send_str ( self.client.geoanchor_attr_lat,req )
req=send_str ( self.client.geoanchor_attr_long,req )
req=send_float ( self.client.geoanchor_lat,req )
req=send_float ( self.client.geoanchor_long,req )
else
req=send_int ( 0 ,req)
end
req=send_int ( self.client.num_index_weights,req )
for i=1, self.client.num_index_weights do
req=send_str ( self.client.index_weights_names[i],req )
req=send_int ( self.client.index_weights_values[i],req )
end
req=send_int ( self.client.max_query_time,req )
req=send_int ( self.client.num_field_weights,req )
for i=1,#self.client.field_weights_names do
req=send_str (self.client.field_weights_names[i],req );
req=send_int ( self.client.field_weights_values[i],req );
end
req=send_str(comment,req);
if self.client.ver_search>=0x115 then
req=send_int ( self.client.num_overrides,req )
for i=1, self.client.num_overrides do
req=send_str ( self.client.overrides[i].attr,req )
req=send_int ( SPH_ATTR_INTEGER ,req)
req=send_int ( self.client.overrides[i].num_values ,req)
for j=1, self.client.overrides[i].num_values do
req=send_qword ( self.client.overrides[i].docids[j],req )
req=send_int ( self.client.overrides[i].uint_values[j],req )
end
end
end
if self.client.ver_search>=0x116 then
req=send_str ( self.client.select_list,req )
end
_sphinx_client.num_reqs = 1
return req
end
function sphinx.query(self,query,index_list, comment)
input=""
input=send_int(1,"")
self.client.sock:send(input)
local input, recvt, sendt, status,response, receive_status
response, receive_status = self.client.sock:receive(4)
req = self.add_query(self,query,index_list, comment);
local len = 8 + #req
local req_header=""
req_header=send_word(SEARCHD_COMMAND_SEARCH,req_header)
req_header=send_word(self.client.ver_search,req_header)
req_header=send_int(len,req_header)
req_header=send_int(0,req_header)
req_header=send_int(self.client.num_reqs,req_header)
self.client.sock:send(req_header)
self.client.sock:send(req)
response, receive_status = self.client.sock:receive(8)
value, status =unpack_short(response)
value, ver =unpack_short(value)
value, len =unpack_int(value)
response, receive_status = self.client.sock:receive(len)
res = format_response(response)
return res
end
function format_response(response)
res={}
value, status = unpack_int ( response )
res.status = status
--pass
-- fields
value,res.num_fields = unpack_int ( value )
res.fields={}
for i=1,res.num_fields do
value,str= unpack_str(value)
end
value, res.num_attrs = unpack_int ( value )
res.attr_names = {}
res.attr_types = {}
for i=1,res.num_attrs do
value,res.attr_names[i] = unpack_str ( value )
value,res.attr_types[i] = unpack_int ( value )
end
value, res.num_matches = unpack_int ( value )
value,id64 = unpack_int ( value )
res.matches ={}
for i=1,res.num_matches do
res.matches[i]={}
if ( id64 ~= 0 ) then
value,res.matches[i].doc = unpack_qword ( value )
else
value,res.matches[i].doc = unpack_int ( value )
end
-- weight
value,res.matches[i].weight = unpack_int( value )
res.matches[i].attr={}
for j=1,res.num_attrs do
res.matches[i].attr[j]={}
if res.attr_types[i] == SPH_ATTR_MULTI64 or res.attr_types[i] == SPH_ATTR_MULTI then
z=1
elseif res.attr_types[i] == SPH_ATTR_FLOAT then
value,res.matches[i].attr[j].value = unpack_int ( value )
elseif res.attr_types[i] == SPH_ATTR_BIGINT then
value,res.matches[i].attr[j].value = unpack_qword ( value )
elseif res.attr_types[i] == SPH_ATTR_STRING then
value,res.matches[i].attr[j].value = unpack_str ( value )
else
value,res.matches[i].attr[j].value = unpack_int ( value )
end
end
end
-- totals
value,res.total = unpack_int ( value )
value,res.total_found = unpack_int ( value )
value,res.time_msec = unpack_int ( value )
value,res.num_words = unpack_int ( value )
res.words={}
-- words
for i=1,res.num_words do
res.words[i]={}
value,res.words[i].word = unpack_str ( value )
value,res.words[i].docs = unpack_int ( value )
value,res.words[i].hits = unpack_int ( value )
end
return res
end
return sphinx
--ngx.say("hello,lua-sphinx")
--ngx.say("hello,lua-sphinx")