Skip to content

Commit 6410513

Browse files
committed
Using SearchResult instance as result of searchSpecifications & scrollSpecifications
1 parent c2a3e74 commit 6410513

File tree

2 files changed

+69
-20
lines changed

2 files changed

+69
-20
lines changed

src/Collection.js

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,6 @@ Collection.prototype.search = function (filters, options, cb) {
699699

700700
query = self.kuzzle.addHeaders({body: filters}, this.headers);
701701

702-
703702
self.kuzzle.query(this.buildQueryArgs('document', 'search'), query, options, function (error, result) {
704703
var documents = [];
705704

@@ -811,7 +810,8 @@ Collection.prototype.scroll = function (scrollId, options, filters, cb) {
811810
*/
812811
Collection.prototype.scrollSpecifications = function (scrollId, options, cb) {
813812
var
814-
data = { scrollId: scrollId };
813+
data = { scrollId: scrollId },
814+
self = this;
815815

816816
if (!scrollId) {
817817
throw new Error('Collection.scrollSpecifications: scrollId is required');
@@ -828,14 +828,36 @@ Collection.prototype.scrollSpecifications = function (scrollId, options, cb) {
828828
data.scroll = options.scroll;
829829
}
830830

831-
this.kuzzle.query(
832-
{ controller: 'collection', action: 'scrollSpecifications'},
833-
this.kuzzle.addHeaders(data, this.headers),
834-
options,
835-
function (err, res) {
836-
cb (err, err ? undefined : res.result);
831+
this.kuzzle.query({ controller: 'collection', action: 'scrollSpecifications'}, this.kuzzle.addHeaders(data, this.headers), options, function (err, res) {
832+
var
833+
documents = [];
834+
835+
if (err) {
836+
return cb(err);
837+
}
838+
839+
res.result.hits.forEach(function (doc) {
840+
var newDocument = new Document(self, doc._id, doc._source, doc._meta);
841+
842+
newDocument.version = doc._version;
843+
844+
documents.push(newDocument);
845+
});
846+
847+
if (res.result.scrollId) {
848+
options.scrollId = res.result.scrollId;
837849
}
838-
);
850+
851+
cb(null, new KuzzleSearchResult(
852+
self,
853+
res.result.total,
854+
documents,
855+
res.result.aggregations ? res.result.aggregations : {},
856+
options,
857+
null,
858+
options.previous || null
859+
));
860+
});
839861
};
840862

841863
/**
@@ -860,7 +882,34 @@ Collection.prototype.searchSpecifications = function (filters, options, cb) {
860882
data = self.kuzzle.addHeaders(data, this.headers);
861883

862884
self.kuzzle.query({ controller: 'collection', action: 'searchSpecifications' }, data, options, function (err, res) {
863-
cb(err, err ? undefined : res.result);
885+
var
886+
documents = [];
887+
888+
if (err) {
889+
return cb(err);
890+
}
891+
892+
res.result.hits.forEach(function (doc) {
893+
var newDocument = new Document(self, doc._id, doc._source, doc._meta);
894+
895+
newDocument.version = doc._version;
896+
897+
documents.push(newDocument);
898+
});
899+
900+
if (res.result.scrollId) {
901+
options.scrollId = res.result.scrollId;
902+
}
903+
904+
cb(null, new KuzzleSearchResult(
905+
self,
906+
res.result.total,
907+
documents,
908+
res.result.aggregations ? res.result.aggregations : {},
909+
options,
910+
filters,
911+
options.previous || null
912+
));
864913
});
865914
};
866915

test/Collection/methods.test.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,11 @@ describe('Collection methods', function () {
179179

180180
collection.scrollSpecifications(scrollId, options, function(err, res) {
181181
should(err).be.null();
182-
should(res).be.an.instanceOf(Object).and.match(result.result);
183-
should(res.total).be.a.Number().and.be.exactly(result.result.total);
184-
should(res.hits).be.an.Array();
185-
should(res.hits.length).be.exactly(result.result.hits.length);
186-
should(res.scrollId).be.exactly('1337');
182+
should(res).be.an.instanceOf(SearchResult);
183+
should(res.getTotal()).be.a.Number().and.be.exactly(result.result.total);
184+
should(res.getDocuments()).be.an.Array();
185+
should(res.getDocuments().length).be.exactly(result.result.hits.length);
186+
should(res.options.scrollId).be.exactly('1337');
187187

188188
done();
189189
});
@@ -1252,11 +1252,11 @@ describe('Collection methods', function () {
12521252

12531253
collection.searchSpecifications(filters, options, function (err, res) {
12541254
should(err).be.null();
1255-
should(res).be.an.instanceOf(Object).and.match(result.result);
1256-
should(res.total).be.a.Number().and.be.exactly(result.result.total);
1257-
should(res.hits).be.an.Array();
1258-
should(res.hits.length).be.exactly(result.result.hits.length);
1259-
should(res.scrollId).be.exactly('1337');
1255+
should(res).be.an.instanceOf(SearchResult);
1256+
should(res.getTotal()).be.a.Number().and.be.exactly(result.result.total);
1257+
should(res.getDocuments()).be.an.Array();
1258+
should(res.getDocuments().length).be.exactly(result.result.hits.length);
1259+
should(res.options.scrollId).be.exactly('1337');
12601260

12611261
done();
12621262
});

0 commit comments

Comments
 (0)