Skip to content
This repository has been archived by the owner on Mar 28, 2019. It is now read-only.

Commit

Permalink
Merge pull request #10 from mdevils/master
Browse files Browse the repository at this point in the history
BuildExcerpts implementation.
  • Loading branch information
touv committed Jun 10, 2013
2 parents b3ca39e + a6c0994 commit 4ba65fc
Showing 1 changed file with 87 additions and 1 deletion.
88 changes: 87 additions & 1 deletion lib/sphinxapi.js
Expand Up @@ -827,7 +827,93 @@ SphinxClient.prototype.RunQueries = function (fn) {
fn(err, results)
})
};
SphinxClient.prototype.BuildExcerpts = function (docs, index, words, opts) {
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) {
if (mva === undefined) mva = false;
Expand Down

0 comments on commit 4ba65fc

Please sign in to comment.