This repository was archived by the owner on Mar 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathsphinxapi.js
More file actions
1243 lines (1127 loc) · 38.5 KB
/
sphinxapi.js
File metadata and controls
1243 lines (1127 loc) · 38.5 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
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict'
/*!
* sphinxapi
* Copyright(c) 2012 Nicolas Thouvenin <nthouvenin@gmail.com>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var net = require('net'),
bp = require('bufferpack'),
Put = require('put'),
assert = require('assert'),
util = require('util'),
debug = require('debug')('SphinxClient'),
server_say = require('debug')('server'),
client_say = require('debug')('client')
;
/**
* Useful function
*
* @api private
*/
function packUInt64(number) {
return Put().word64be(number).buffer()
}
function packInt64(number) {
return Put().word64be(number).buffer()
}
function unpackUInt64(buffer) {
var high = buffer.readUInt32BE(0)
var low = buffer.readUInt32BE(4)
return high*4294967296+low
}
function unpackInt64(buffer) {
var high = buffer.readInt32BE(0)
var low = buffer.readInt32BE(4)
return low
}
function ConcatBuffer(a, b) {
var t = new Buffer(a.length + b.length)
a.copy(t, 0, 0)
b.copy(t, a.length, 0)
return t
}
function ReduceBuffer(a, b) {
return ConcatBuffer(a, Buffer.isBuffer(b) ? b : new Buffer(b));
}
function forEach(o, fn) {
if (Array.isArray(o)) {
return o.forEach(fn);
}
else {
for (var key in o) {
if (o.hasOwnProperty(key)) {
fn(o[key], key)
}
}
}
}
function len (o){
if (Array.isArray(o)) {
return o.length;
}
else if (typeof o == 'string') {
return Buffer.byteLength(o)
}
else {
var k, l = 0;
for(k in o) {
l += Number( o.hasOwnProperty(k) );
}
return l;
}
}
function unpack(a, b) {
return bp.unpack(a, b);
}
function pack(a, b) {
return bp.pack(a, b);
}
/**
* SphinxClient Object
*
* @api public
*/
function SphinxClient() {
if (!(this instanceof SphinxClient)) {
return new SphinxClient()
}
this._host = 'localhost' // searchd host (default is "localhost")
this._port = 9312 // searchd port (default is 9312)
this._path = null // searchd unix-domain socket path
this._socket = null
this._offset = 0 // how much records to seek from result-set start (default is 0)
this._limit = 20 // how much records to return from result-set starting at offset (default is 20)
this._mode = SphinxClient.SPH_MATCH_ALL // query matching mode (default is SPH_MATCH_ALL)
this._weights = [] // per-field weights (default is 1 for all fields)
this._sort = SphinxClient.SPH_SORT_RELEVANCE // match sorting mode (default is SPH_SORT_RELEVANCE)
this._sortby = '' // attribute to sort by (defualt is "")
this._min_id = 0 // min ID to match (default is 0)
this._max_id = 0 // max ID to match (default is UINT_MAX)
this._filters = [] // search filters
this._groupby = '' // group-by attribute name
this._groupfunc = SphinxClient.SPH_GROUPBY_DAY // group-by function (to pre-process group-by attribute value with)
this._groupsort = '@group desc' // group-by sorting clause (to sort groups in result set with)
this._groupdistinct = '' // group-by count-distinct attribute
this._maxmatches = 1000 // max matches to retrieve
this._cutoff = 0 // cutoff to stop searching at
this._retrycount = 0 // distributed retry count
this._retrydelay = 0 // distributed retry delay
this._anchor = {} // geographical anchor point
this._indexweights = {} // per-index weights
this._ranker = SphinxClient.SPH_RANK_PROXIMITY_BM25 // ranking mode
this._rankexpr = '' // ranking expression for SPH_RANK_EXPR
this._maxquerytime = 0 // max query time, milliseconds (default is 0, do not limit)
this._timeout = 1.0 // connection timeout
this._fieldweights = {} // per-field-name weights
this._overrides = {} // per-query attribute values overrides
this._select = '*' // select-list (attributes or expressions, with optional aliases)
this._error = '' // last error message
this._warning = '' // last warning message
this._reqs = [] // requests array for multi-query
};
/**
* Sets and sends request packet to searchd server.
*
* @api private
*/
SphinxClient.prototype._SendRequest = function (client_ver, request, fn) {
var self = this
debug('Connecting to ' + self._host + ':' + self._port)
var client = net.connect(self._port, self._host)
client.on('connect', function () {
client_say('connected')
});
client.once('data', function (chunk) {
chunk.slice(0, 4)
var response = unpack('>L', chunk)
server_say('>L', response)
if (!Array.isArray(response)) {
fn(new Error('connection to ' + self._host + ':' + self._port + ' failed'))
} else if (Array.isArray(response) && response[0] < 1) {
fn(new Error('expected searchd protocol version, got ' + response[0]))
}
else {
client_say('received version', client_ver);
}
var content, state, version, length;
client.on('data', function (chunk) {
if (content === null || content === undefined) {
client_say('received the response');
response = unpack('>2HL', chunk)
server_say('>2HL', response)
state = response[0]
version = response[1]
length = response[2]
content = chunk.slice(8)
client_say('processing the response #1', state, version, length)
}
else {
client_say('received following the response ')
content = ConcatBuffer(content, chunk)
}
if (content.length >= length) {
var err = null
client_say('processing the response #2', state, version);
if (state == SphinxClient.SEARCHD_WARNING) {
var wend = 4 + unpack('>L', content)
warning = content.slice(4, wend);
// TODO do something with the warning !!!
}
else if (state == SphinxClient.SEARCHD_ERROR) {
err = new Error('searchd error: ' + content.slice(4).toString())
content = null;
}
else if (state == SphinxClient.SEARCHD_RETRY) {
err = new Error('temporary searchd error: ' + content.slice(4).toString())
content = null;
}
else if (state != SphinxClient.SEARCHD_OK) {
err = new Error('unknown status code ' + state)
content = null;
}
if (version < client_ver) {
self._warning = util.format('searchd command v.%d.%d older than client\'s v.%d.%d, some options might not work',
version>>8, version&0xff, client_ver>>8, client_ver&0xff)
// TODO do something with the warning !!!
}
client.end()
fn(err, content)
}
}
);
client_say('sending a request', request.toString());
// process.stdout.write(request.toString('hex')+'\n')
client.write(request);
}
);
client.on('end', function () {
client_say('disconnected');
}
);
client.on('error', function (err) {
fn(new Error('searchd connexion error'))
}
);
client.write(pack('>L', 1))
};
SphinxClient.prototype.GetLastError = function () {
var self = this
return self._error
};
SphinxClient.prototype.GetLastWarning = function () {
var self = this
return self._warning
};
/**
* Set searchd server host and port.
*
* @api public
*/
SphinxClient.prototype.SetServer = function (host, port) {
var self = this
assert.equal(typeof host, 'string')
assert.equal(typeof port, 'number')
self._host = host;
self._port = port;
};
SphinxClient.prototype.SetConnectTimeout = function (timeout ) {
var self = this
assert.equal(typeof timeout, 'number')
self._timeout = Math.max(0.001, timeout);
};
SphinxClient.prototype.SetLimits = function (offset, limit, maxmatches, cutoff) {
var self = this
assert.equal(typeof offset, 'number')
assert.equal(typeof limit, 'number')
assert(0 <= offset < 16777216)
assert(0 <= limit < 16777216)
if (maxmatches === undefined) {
maxmatches = 0
}
if (cutoff === undefined) {
cutoff = 0
}
assert(maxmatches >= 0)
self._offset = offset
self._limit = limit
if (maxmatches > 0) {
self._maxmatches = maxmatches
}
if (cutoff >= 0) {
self._cutoff = cutoff
}
};
SphinxClient.prototype.SetMaxQueryTime = function (maxquerytime) {
var self = this
assert.equal(typeof maxquerytime, 'number')
assert(maxquerytime > 0)
self._maxquerytime = maxquerytime
};
SphinxClient.prototype.SetMatchMode = function (mode) {
var self = this
var modes = [SphinxClient.SPH_MATCH_ALL, SphinxClient.SPH_MATCH_ANY, SphinxClient.SPH_MATCH_PHRASE, SphinxClient.SPH_MATCH_BOOLEAN, SphinxClient.SPH_MATCH_EXTENDED, SphinxClient.SPH_MATCH_FULLSCAN, SphinxClient.SPH_MATCH_EXTENDED2]
assert(modes.some(function (x) { return (x === mode) }))
self._mode = mode
};
SphinxClient.prototype.SetRankingMode = function (ranker, rankexpr) {
var self = this
if (rankexpr === undefined) {
rankexpr = ''
}
assert(0 <= ranker && ranker < SphinxClient.SPH_RANK_TOTAL)
self._ranker = ranker
self._rankexpr = rankexpr
};
SphinxClient.prototype.SetSortMode = function (mode, clause) {
var self = this
var modes = [SphinxClient.SPH_SORT_RELEVANCE, SphinxClient.SPH_SORT_ATTR_DESC, SphinxClient.SPH_SORT_ATTR_ASC, SphinxClient.SPH_SORT_TIME_SEGMENTS, SphinxClient.SPH_SORT_EXTENDED, SphinxClient.SPH_SORT_EXPR]
if (clause === undefined) {
clause = ''
}
assert(modes.some(function (x) { return (x === mode) }))
assert.equal(typeof clause, 'string')
self._sort = mode
self._sortby = clause
};
SphinxClient.prototype.SetWeights = function (weights) {
var self = this
assert(Array.isArray(weights))
forEach(weights, function (item, index) {
assert.equal(typeof item, 'number')
})
self._weights = weights
};
SphinxClient.prototype.SetFieldWeights = function (weights) {
var self = this
assert.equal(typeof weights, 'object')
forEach(weights, function (item, index) {
assert.equal(typeof item, 'number')
})
self._fieldweights = weights
};
SphinxClient.prototype.SetIndexWeights = function (weights) {
var self = this
assert.equal(typeof weights, 'object')
forEach(weights, function (item, index) {
assert.equal(typeof item, 'number')
})
self._indexweights = weights
};
SphinxClient.prototype.SetIDRange = function (minid, maxid) {
var self = this
assert.equal(typeof minid, 'number')
assert.equal(typeof maxid, 'number')
assert(minid <= maxid)
self._min_id = minid
self._max_id = maxid
};
SphinxClient.prototype.SetFilter = function (attribute, values, exclude) {
var self = this
if (exclude === undefined) {
exclude = 0
}
assert.equal(typeof attribute, 'string')
forEach(values, function (item, index) {
assert.equal(typeof item, 'number')
})
self._filters.push({
'type': SphinxClient.SPH_FILTER_VALUES,
'attr': attribute,
'exclude': exclude,
'values': values
})
};
SphinxClient.prototype.SetFilterString = function (attribute, value, exclude) {
var self = this
if (exclude === undefined) {
exclude = 0
}
assert.equal(typeof attribute, 'string')
assert.equal(typeof value, 'string')
self._filters.push({
'type': SphinxClient.SPH_FILTER_STRING,
'attr': attribute,
'exclude': exclude,
'value': value
})
};
SphinxClient.prototype.SetFilterRange = function (attribute, min_, max_, exclude) {
var self = this
if (exclude === undefined) {
exclude = 0
}
assert.equal(typeof attribute, 'string')
assert.equal(typeof min_, 'number')
assert.equal(typeof max_, 'number')
assert(min_<=max_)
self._filters.push({
'type': SphinxClient.SPH_FILTER_RANGE
, 'attr': attribute
, 'exclude': exclude
, 'min': min_
, 'max': max_
} )
}
SphinxClient.prototype.SetFilterFloatRange = function (attribute, min_, max_, exclude) {
var self = this
if (exclude === undefined) {
exclude = 0
}
assert.equal(typeof attribute, 'string')
assert.equal(typeof min_, 'number')
assert.equal(typeof max_, 'number')
assert(min_<=max_)
self._filters.push({
'type': SphinxClient.SPH_FILTER_FLOATRANGE,
'attr': attribute,
'exclude': exclude,
'min': min_,
'max': max_
} )
}
SphinxClient.prototype.SetGeoAnchor = function (attrlat, attrlong, latitude, longitude) {
var self = this
assert.equal(typeof attrlat, 'string')
assert.equal(typeof attrlong, 'string')
assert.equal(typeof latitude, 'number')
assert.equal(typeof longitude, 'number')
self._anchor['attrlat'] = attrlat
self._anchor['attrlong'] = attrlong
self._anchor['lat'] = latitude
self._anchor['long'] = longitude
};
SphinxClient.prototype.SetGroupBy = function (attribute, func, groupsort ) {
var self = this
if (groupsort == undefined) groupsort = '@group desc';
assert.equal(typeof attribute, 'string')
assert.equal(typeof groupsort, 'string')
var funcs = [SphinxClient.SPH_GROUPBY_DAY, SphinxClient.SPH_GROUPBY_WEEK, SphinxClient.SPH_GROUPBY_MONTH, SphinxClient.SPH_GROUPBY_YEAR, SphinxClient.SPH_GROUPBY_ATTR, SphinxClient.SPH_GROUPBY_ATTRPAIR]
assert(funcs.some(function (x) { return (x === func) }))
self._groupby = attribute
self._groupfunc = func
self._groupsort = groupsort
};
SphinxClient.prototype.SetGroupDistinct = function (attribute) {
var self = this
assert.equal(typeof attribute, 'string')
self._groupdistinct = attribute
};
SphinxClient.prototype.SetRetries = function (count, delay) {
var self = this
if (delay == undefined) delay = 0;
assert.equal(typeof count, 'number')
assert.equal(typeof delay, 'number')
assert(count >= 0)
assert(delay >= 0)
self._retrycount = count
self._retrydelay = delay
};
SphinxClient.prototype.SetOverride = function (name, type, values) {
var self = this
assert.equal(typeof name, 'string')
assert(SphinxClient.SPH_ATTR_TYPES.some(function (x) { return (x === type) }))
assert.equal(typeof values, 'object')
self._overrides[name] = {
'name': name,
'type': type,
'values': values
}
};
SphinxClient.prototype.SetSelect = function (select) {
var self = this
assert.equal(typeof select, 'string')
self._select = select
};
SphinxClient.prototype.ResetOverrides = function () {
var self = this
self._overrides = {}
};
SphinxClient.prototype.ResetFilters = function () {
var self = this
self._filters = []
self._anchor = {}
};
SphinxClient.prototype.ResetGroupBy = function () {
var self = this
self._groupby = ''
self._groupfunc = SphinxClient.SPH_GROUPBY_DAY
self._groupsort = '@group desc'
self._groupdistinct = ''
};
/**
* Connect to searchd server and run given search query.
*
* @api public
*/
SphinxClient.prototype.Query = function (query, index, comment, fn) {
var self = this
if (arguments.length == 2) {
fn = arguments[1];
index = '*';
comment = '';
}
else if (arguments.length == 3) {
fn = arguments[2];
comment = '';
}
self.AddQuery(query, index, comment)
self.RunQueries(function (err, results) {
self._reqs = [] // we won't re-run erroneous batch
if (err) {
fn(err, null)
return
}
if (results.length == 0) {
fn(err, null)
return
}
self._error = results[0].error
self._warning = results[0].warning
if (results[0].status == SphinxClient.SEARCHD_ERROR) {
fn(results[0].error, null)
return
}
fn(err, results[0])
})
};
/**
* Add query to batch.
*
* @api public
*/
SphinxClient.prototype.AddQuery = function (query, index, comment) {
var self = this
if (index === undefined) index = '*';
if (comment === undefined) comment = '';
assert.equal(typeof query, 'string');
var req = []
req.push(pack('>LLLL', [self._offset, self._limit, self._mode, self._ranker]))
if (self._ranker == SphinxClient.SPH_RANK_EXPR) {
req.push(pack('>L', [len(self._rankexpr)]))
req.push(self._rankexpr)
}
req.push(pack('>L', [self._sort]))
req.push(pack('>L', [len(self._sortby)]))
req.push(self._sortby)
// TODO : check if query is encoding in utf8
req.push(pack('>L', [len(query)]))
req.push(query)
req.push(pack('>L', [len(self._weights)]))
forEach(self._weights, function (item, index) {
req.push(pack('>L', [item])) // FIXME / TO VERIFY
});
req.push(pack('>L', [len(index)]))
req.push(index)
req.push(pack('>L', [1])) // id64 range marker
// req.push(pack('>Q', [self._min_id]))
req.push(packUInt64(self._min_id))
// req.push(pack('>Q', [self._max_id]))
req.push(packUInt64(self._max_id))
// filters
req.push(pack('>L', [len(self._filters)]))
forEach(self._filters, function (f, index) {
req.push(pack('>L', [len(f.attr)]))
req.push(f.attr)
var filtertype = f.type
req.push(pack('>L', [filtertype]))
if (filtertype == SphinxClient.SPH_FILTER_VALUES) {
req.push(pack('>L', [len(f.values)]))
forEach(f.values, function (val, index) {
// req.push(pack('>q', [val]))
req.push(packUInt64(val))
});
} else if (filtertype == SphinxClient.SPH_FILTER_RANGE) {
// req.push(pack('>q', [f.min]))
req.push(packUInt64(f.min))
// req.push(pack('>q', [f.max]))
req.push(packUInt64(f.max))
}
else if (filtertype == SphinxClient.SPH_FILTER_FLOATRANGE) {
req.push(pack ('>f', [f.min]))
req.push(pack ('>f', [f.max]))
}
else if (filtertype == SphinxClient.SPH_FILTER_STRING) {
req.push(pack ('>L', [len(f.value)]))
req.push(f.value);
}
req.push(pack('>L', [f.exclude]))
});
// group-by, max-matches, group-sort
req.push(pack('>LL', [self._groupfunc, len(self._groupby)]))
req.push(self._groupby)
req.push(pack('>LL', [self._maxmatches, len(self._groupsort)]))
req.push(self._groupsort)
req.push(pack('>LLL', [self._cutoff, self._retrycount, self._retrydelay]))
req.push(pack('>L', [len(self._groupdistinct)]))
req.push(self._groupdistinct)
// anchor point
if (len(self._anchor) == 0) {
req.push(pack('>L', [0]))
}
else {
req.push(pack('>L', [1]))
req.push(pack('>L', [len(self._anchor.attrlat)]) + self._anchor.attrlat)
req.push(pack('>L', [len(self._anchor.attrlong)]) + self._anchor.attrlong)
req.push(pack('>f', [self._anchor.lat]))
req.push(pack('>f', [self._anchor.long]))
}
// per-index weights
req.push(pack('>L', [len(self._indexweights)]))
forEach(self._indexweights, function (weight, index) {
req.push(pack('>L', [len(index)]))
req.push(index)
req.push(pack('>L', [weight]))
});
// max query time
req.push(pack('>L', [self._maxquerytime]))
// per-field weights
req.push(pack('>L', [len(self._fieldweights)]))
forEach(self._fieldweights, function (weight, field) {
req.push(pack('>L', [len(field)]))
req.push(field)
req.push(pack('>L', [weight]))
});
// comment
req.push(pack('>L', [len(comment)]))
req.push(comment)
// attribute overrides
req.push(pack('>L', [len(self._overrides)]))
forEach(self._overrides, function (v, index) {
req.push(pack('>L', [len(v['name'])]))
req.push(v['name'])
req.push(pack('>LL', [v['type'], len(v['values'])]))
forEach(v['values'], function (value, id) {
// req.push(pack('>Q', [id]))
req.push(packUInt64(id))
if (v['type'] == SphinxClient.SPH_ATTR_FLOAT) {
req.push(pack('>f', [value]))
}
else if (v['type'] == SphinxClient.SPH_ATTR_BIGINT) {
// req.push(pack('>q', [value]))
req.push(packInt64(id))
}
else {
req.push(pack('>l', [value]))
}
});
});
// select-list
req.push(pack('>L', [len(self._select)]))
req.push(self._select)
// send query, get response
req = req.reduce(ReduceBuffer, new Buffer(''));
self._reqs.push(req)
debug('New Request Added', req.toString());
return self._reqs.length - 1
};
/**
* Run queries batch.
* Returns None on network IO failure; or an array of result set hashes on success.
* @api public
*/
SphinxClient.prototype.RunQueries = function (fn) {
var self = this
var nreqs = self._reqs.length
debug('Pool requests Size : '+ nreqs)
if (nreqs == 0) {
self._error = 'no queries defined, issue AddQuery() first'
return null
}
var req = self._reqs.reduce(ReduceBuffer, new Buffer(''));
var length = req.length + 8
debug('Combined '+nreqs+' requests', req.toString())
client_say('>HHLLL', [SphinxClient.SEARCHD_COMMAND_SEARCH, SphinxClient.VER_COMMAND_SEARCH, length, 0, nreqs]);
var request = ConcatBuffer(pack('>HHLLL', [SphinxClient.SEARCHD_COMMAND_SEARCH, SphinxClient.VER_COMMAND_SEARCH, length, 0, self._reqs.length]), req)
self._SendRequest(SphinxClient.VER_COMMAND_SEARCH, request, function (err, response) {
if (response === null || response === undefined) {
return fn(err, results)
}
// parse response
var max_ = response.length
var p = 0
var results = []
for (var i = 0; i < nreqs; i++) {
debug('Parsing request #'+i)
var result = {}
results.push(result)
result['error'] = ''
result['warning'] = ''
result['status'] = unpack('>L', response.slice(p, p + 4))
p += 4
if (result['status'] != SphinxClient.SEARCHD_OK) {
length = Number(unpack('>L', response.slice(p, p + 4)))
p += 4
var message = response.slice(p, p+length)
p += length
if (result['status'] == SphinxClient.SEARCHD_WARNING) {
result['warning'] = message.toString()
}
else {
result['error'] = message.toString()
continue
}
}
// read schema
result['fields'] = []
var attrs = []
var nfields = Number(unpack('>L', response.slice(p, p + 4)))
p += 4
while (nfields > 0 && p < max_) {
nfields -= 1
length = Number(unpack('>L', response.slice(p, p + 4)))
p += 4
result['fields'].push(response.slice(p, p + length).toString())
p += length
}
var nattrs = Number(unpack('>L', response.slice(p, p + 4)))
p += 4
while (nattrs>0 && p<max_) {
nattrs -= 1
length = Number(unpack('>L', response.slice(p, p + 4)))
p += 4
var attr = response.slice(p, p + length).toString()
p += length
var type_ = Number(unpack('>L', response.slice(p, p + 4)))
p += 4
attrs.push([attr, type_])
}
result['attrs'] = attrs
// read match count
var count = Number(unpack('>L', response.slice(p, p + 4)))
p += 4
var id64 = Number(unpack('>L', response.slice(p, p + 4)))
p += 4
// read matches
result['matches'] = []
while (count>0 && p<max_) {
var doc, weight
count -= 1
if (id64) {
// doc = Number(unpack('>q', response.slice(p, p + 8)))
doc = Number(unpackUInt64(response.slice(p, p + 8)))
server_say('>q',doc)
p += 8
weight = Number(unpack('>L', response.slice(p, p + 4)))
p += 4
}
else {
doc = Number(unpack('>L', response.slice(p, p + 4)))
p += 4
weight = Number(unpack('>L', response.slice(p, p + 4)))
p += 4
}
var match = { 'id':doc, 'weight':weight, 'attrs':{} }
for (var j = 0; j < result['attrs'].length; j++) {
var attr0 = attrs[j][0]
if (attrs[j][1] == SphinxClient.SPH_ATTR_FLOAT) {
match['attrs'][attr0] = Number(unpack('>f', response.slice(p, p + 4)))
}
else if (attrs[j][1] == SphinxClient.SPH_ATTR_BIGINT) {
// match['attrs'][attr0] = Number(unpack('>q', response.slice(p, p + 8)))
match['attrs'][attr0] = Number(unpackUInt64(response.slice(p, p + 8)))
p += 4
}
else if (attrs[j][1] == SphinxClient.SPH_ATTR_STRING) {
var slen = Number(unpack('>L', response.slice(p, p + 4)))
p += 4
match['attrs'][attr0] = ''
if (slen>0) {
match['attrs'][attr0] = response.slice(p, p + slen).toString()
}
p += slen-4
}
else if (attrs[j][1] == SphinxClient.SPH_ATTR_MULTI) {
match['attrs'][attr0] = []
var nvals = Number(unpack('>L', response.slice(p, p + 4)))
p += 4
for (var n = 0; n < nvals; n++) {
match['attrs'][attr0].push(Number(unpack('>L', response.slice(p, p + 4))))
p += 4
}
p -= 4
}
else if (attrs[j][1] == SphinxClient.SPH_ATTR_MULTI64) {
match['attrs'][attr0] = []
nvals = Number(unpack('>L', response.slice(p, p + 4)))
nvals = nvals/2
p += 4
for (var n = 0; n < nvals; n++) {
// match['attrs'][attr0].push(Number(unpack('>q', response.slice(p, p + 8))))
match['attrs'][attr0].push(Number(unpackUInt64(response.slice(p, p + 8))))
p += 8
}
p -= 4
}
else {
match['attrs'][attr0] = Number(unpack('>L', response.slice(p, p + 4)))
}
p += 4
}
result['matches'].push( match )
}
result['total'] = Number(unpack('>L', response.slice(p, p + 4)))
p += 4
result['total_found'] = Number(unpack('>L', response.slice(p, p + 4)))
p += 4
result['time'] = Number(unpack('>L', response.slice(p, p + 4)))
p += 4
var words = Number(unpack('>L', response.slice(p, p + 4)))
p += 4
result['time'] = (result['time']/1000.0)
result['words'] = []
while (words>0) {
words -= 1
var length = Number(unpack('>L', response.slice(p, p + 4)))
p += 4
var word = response.slice(p, p + length).toString()
p += length
var docs = Number(unpack('>L', response.slice(p, p + 4)))
p += 4
var hits = Number(unpack('>L', response.slice(p, p + 4)))
p += 4
result['words'].push({'word':word, 'docs':docs, 'hits':hits})
}
}
fn(err, results)
})
this._reqs = []
};
SphinxClient.prototype.BuildExcerpts = function (docs, index, words, opts, cb) {
assert.equal(Array.isArray(docs), true);
assert.equal(typeof index, 'string');
assert.equal(typeof words, 'string');
if (opts) {
assert.equal(typeof opts, 'object');
} else {
opts = {};
}
opts.before_match = typeof opts.before_match !== 'undefined' ? opts.before_match : "<b>";
opts.after_match = typeof opts.after_match !== 'undefined' ? opts.after_match : "</b>";
opts.chunk_separator = typeof opts.chunk_separator !== 'undefined' ? opts.chunk_separator : " ... ";
opts.limit = typeof opts.limit !== 'undefined' ? opts.limit : 256;
opts.limit_passages = typeof opts.limit_passages !== 'undefined' ? opts.limit_passages : 0;
opts.limit_words = typeof opts.limit_words !== 'undefined' ? opts.limit_words : 0;
opts.around = typeof opts.around !== 'undefined' ? opts.around : 5;
opts.exact_phrase = typeof opts.exact_phrase !== 'undefined' ? opts.exact_phrase : false;
opts.single_passage = typeof opts.single_passage !== 'undefined' ? opts.single_passage : false;
opts.use_boundaries = typeof opts.use_boundaries !== 'undefined' ? opts.use_boundaries : false;
opts.weight_order = typeof opts.weight_order !== 'undefined' ? opts.weight_order : false;
opts.query_mode = typeof opts.query_mode !== 'undefined' ? opts.query_mode : false;
opts.force_all_words = typeof opts.force_all_words !== 'undefined' ? opts.force_all_words : false;
opts.start_passage_id = typeof opts.start_passage_id !== 'undefined' ? opts.start_passage_id : 1;
opts.load_files = typeof opts.load_files !== 'undefined' ? opts.load_files : false;
opts.html_strip_mode = typeof opts.html_strip_mode !== 'undefined' ? opts.html_strip_mode : "index";
opts.allow_empty = typeof opts.allow_empty !== 'undefined' ? opts.allow_empty : false;
opts.passage_boundary = typeof opts.passage_boundary !== 'undefined' ? opts.passage_boundary : "none";
opts.emit_zones = typeof opts.emit_zones !== 'undefined' ? opts.emit_zones : false;
opts.load_files_scattered = typeof opts.load_files_scattered !== 'undefined' ? opts.load_files_scattered : false;
var flags = 1; // remove spaces
if (opts.exact_phrase) flags = flags | 2;
if (opts.single_passage) flags = flags | 4;
if (opts.use_boundaries) flags = flags | 8;
if (opts.weight_order) flags = flags | 16;
if (opts.query_mode) flags = flags | 32;
if (opts.force_all_words) flags = flags | 64;
if (opts.load_files) flags = flags | 128;
if (opts.allow_empty) flags = flags | 256;
if (opts.emit_zones) flags = flags | 512;
if (opts.load_files_scattered) flags = flags | 1024;
var req = [];
req.push(pack('>LL', [ 0, flags ]));
req.push(pack('>L', [len(index)]), index);
req.push(pack('>L', [len(words)]), words);
req.push(pack('>L', [len(opts.before_match)]), opts.before_match);
req.push(pack('>L', [len(opts.after_match)]), opts.after_match);
req.push(pack('>L', [len(opts.chunk_separator)]), opts.chunk_separator);
req.push(pack('>LL', [opts.limit, opts.around]));
req.push(pack('>LLL', [opts.limit_passages, opts.limit_words, opts.start_passage_id]));
req.push(pack('>L', [len(opts.html_strip_mode)]), opts.html_strip_mode);
req.push(pack('>L', [len(opts.passage_boundary)]), opts.passage_boundary);
req.push(pack('>L', [docs.length]));
for (var i = 0, l = docs.length; i < l; i++) {
var doc = docs[i];
assert.equal(typeof doc, 'string');
req.push(pack('>L', [len(doc)]), doc);
}
var reqData = req.reduce(ReduceBuffer, new Buffer(''));
var length = reqData.length;
debug('Build excerpts request:', reqData.toString());
var request = ConcatBuffer(pack('>HHL', [SphinxClient.SEARCHD_COMMAND_EXCERPT, SphinxClient.VER_COMMAND_EXCERPT, length]), reqData);
this._SendRequest(SphinxClient.VER_COMMAND_EXCERPT, request, function (err, response) {
if (err) {
return cb(err, null);
}
var results = [], p = 0, rlen = response.length;
for (var i = 0, l = docs.length; i < l; i++) {
var len = unpack('>L', response.slice(p, p + 4))[0];
p += 4;
if (p + len > rlen) {
return cb(new Error('Incomplete reply from searchd'), null);
}
results.push(len ? response.slice(p, p + len).toString('utf8') : '');
p += len;
}
cb(null, results);
return null;
});
};
SphinxClient.prototype.UpdateAttributes = function (index, attrs, values, mva, fn) {
/* === Validation === */
assert.equal(typeof index, 'string');
assert.equal(typeof attrs, 'object');
assert.equal(typeof values, 'object');
assert.equal(typeof mva, 'boolean');
for (var k in attrs) {
if (!attrs.hasOwnProperty(k)) continue;
assert.equal(typeof attrs[k], 'string');
}
for (var k in values) {
if (!values.hasOwnProperty(k)) continue;
// Document must be an array of attribute values.
assert.equal(typeof values[k], 'object');
// Number of attributes do not match.
assert.equal(len(values[k]), len(attrs));
for (var v in values[k]) {
if (!values[k].hasOwnProperty(v)) continue;
if (mva) {
// MVA must be an array.
assert.equal(typeof values[k][v], 'object');
for (var vv in values[k][v]) {
if (!values[k][v].hasOwnProperty(vv)) continue;
// Attribute value must be numeric.
assert.equal(typeof values[k][v][vv], 'number');
}
} else {
// Attribute value must be numeric.
assert.equal(typeof values[k][v], 'number');
}
}
}
/* === Build request === */
var req = [];
// index
req.push(pack('>L', [len(index)]), index);
// attrs
req.push(pack('>L', [len(attrs)]));
for (var k in attrs) {
if (!attrs.hasOwnProperty(k)) continue;
req.push(pack('>L', [len(attrs[k])]), attrs[k]);
req.push(pack('>L', [mva?1:0]));
}
// values
req.push(pack('>L', [len(values)]));
for (var k in values) {
if (!values.hasOwnProperty(k)) continue;