Skip to content

Commit

Permalink
added scan/scroll methods to client
Browse files Browse the repository at this point in the history
  • Loading branch information
enetzer committed Apr 9, 2014
1 parent d1255e5 commit 45f81bb
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions lib/client.js
Expand Up @@ -275,6 +275,72 @@ var client_instance = function (args) {
cb(error, [], raw);
}
});
},

scan_search: function (args, cb) {
args = merge_defaults(args);
args.index = args.index || '';
args.type = args.type || '';
args.scroll_ttl = args.scroll_ttl || 1;

var req_args = {
method: 'post',
path: path.join(args.index, args.type, '_search?search_type=scan&scroll=' + args.scroll_ttl + "m"),
body: args.search
};

self.request(req_args, function (err, obj, raw) {
if (err) { return cb(err); }
if (!obj._scroll_id) {
return cb(null, null, raw);
}
cb(null, obj._scroll_id, raw);
});
},

scroll_search: function (args, cb) {
validator.validate_args(args, ['scroll_id'], function (err) {
if (err) { return cb(err); }

args = merge_defaults(args);
args.index = args.index || '';
args.type = args.type || '';
args.scroll_ttl = args.scroll_ttl || 1;
args.scroll_id = args.scroll_id;
var path = '_search/scroll?scroll_id=' + args.scroll_id + '&scroll=' + args.scroll_ttl + "m";

var req_args = {
method: 'get',
path: path
};

self.request(req_args, function (err, obj, raw) {
if (err) { return cb(err); }
if (obj && obj.hits && obj.hits.total >= 1) {
var result = {};
result.ids = obj.hits.hits.map(function (row) { return row._id; });
result.objects = obj.hits.hits.map(function (row) { return row._source; });
result.total = obj.hits.total;
result.max_score = obj.hits.max_score;
cb(null, result, raw);
} else {
var error;

try {
var parsed = JSON.parse(raw);
if (parsed.error) {
var msg = 'ElasticsearchError: ' + parsed.error;
error = new Error(msg);
}
} catch (e) {
error = new Error('ElasticsearchError: failed to parse JSON:' + util.inspect(raw));
}

cb(error, [], raw);
}
});
});

}
};

Expand Down

0 comments on commit 45f81bb

Please sign in to comment.