Skip to content

Commit d5f82a5

Browse files
authored
Merge pull request #177 from kuzzleio/topic-176-fix-search-result
Remove previous and next cache in SearchResult
2 parents b5de17a + c3f7e87 commit d5f82a5

File tree

4 files changed

+37
-131
lines changed

4 files changed

+37
-131
lines changed

src/Collection.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,8 @@ Collection.prototype.search = function (filters, options, cb) {
472472
result.result.total,
473473
documents,
474474
result.result.aggregations ? result.result.aggregations : [],
475-
{options: options, filters: filters}
475+
{options: options, filters: filters},
476+
options.previous || null
476477
));
477478
});
478479
};
@@ -543,7 +544,8 @@ Collection.prototype.scroll = function (scrollId, options, filters, cb) {
543544
result.result.total,
544545
documents,
545546
result.result.aggregations ? result.result.aggregations : [],
546-
{options: options, filters: filters}
547+
{options: options, filters: filters},
548+
options.previous || null
547549
));
548550
});
549551

src/SearchResult.js

Lines changed: 33 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/**
2-
*
32
* @param {Collection} dataCollection
43
* @param {int} total
54
* @param {Document[]} documents
65
* @param {object} [aggregations]
76
* @param {object} [searchArgs]
8-
* @param {KuzzleSearchResult} [previous]
7+
* @param previous
8+
* @property {Collection} dataCollection
9+
* @property {number} fetchedDocument
910
* @constructor
1011
*/
1112
function KuzzleSearchResult (dataCollection, total, documents, aggregations, searchArgs, previous) {
@@ -32,31 +33,18 @@ function KuzzleSearchResult (dataCollection, total, documents, aggregations, sea
3233
},
3334
// writable properties
3435
fetchedDocument: {
35-
value: documents.length,
36+
value: previous instanceof KuzzleSearchResult ? documents.length + previous.fetchedDocument : documents.length,
3637
enumerable: true,
3738
writable: true
38-
},
39-
_previous: {
40-
value: previous || null,
41-
writable: true
42-
},
43-
_next: {
44-
value: null,
45-
writable: true
4639
}
4740
});
4841

49-
if (this._previous instanceof KuzzleSearchResult) {
50-
this._previous._next = this;
51-
this.fetchedDocument += this._previous.fetchedDocument;
52-
}
53-
5442
// promisifying
5543
if (this.dataCollection.kuzzle.bluebird) {
5644
return this.dataCollection.kuzzle.bluebird.promisifyAll(this, {
5745
suffix: 'Promise',
5846
filter: function (name, func, target, passes) {
59-
var whitelist = ['previous', 'next'];
47+
var whitelist = ['next'];
6048

6149
return passes && whitelist.indexOf(name) !== -1;
6250
}
@@ -66,107 +54,56 @@ function KuzzleSearchResult (dataCollection, total, documents, aggregations, sea
6654
return this;
6755
}
6856

69-
70-
/**
71-
* @param cb
72-
* @returns {*}
73-
*/
74-
KuzzleSearchResult.prototype.previous = function (cb) {
75-
cb(null, this._previous);
76-
77-
return this;
78-
};
79-
8057
/**
8158
* @param {function} cb
8259
*/
8360
KuzzleSearchResult.prototype.next = function (cb) {
8461
var
8562
filters,
86-
options = Object.assign({}, this.searchArgs.options),
87-
self = this;
88-
89-
if (!this._next) {
90-
// retrieve next results with scroll if original search use it
91-
if (options.scrollId) {
92-
if (this.fetchedDocument >= this.total) {
93-
cb(null, null);
94-
return;
95-
}
96-
97-
// from and size parameters are not valid for a scroll action
98-
if (typeof options.from !== 'undefined') {
99-
delete options.from;
100-
}
101-
102-
if (options.size) {
103-
delete options.size;
104-
}
63+
options = Object.assign({}, this.searchArgs.options);
64+
65+
options.previous = this;
66+
67+
// retrieve next results with scroll if original search use it
68+
if (options.scrollId) {
69+
if (this.fetchedDocument >= this.total) {
70+
cb(null, null);
71+
return;
72+
}
10573

106-
this.dataCollection.scroll(
107-
options.scrollId,
108-
options,
109-
this.searchArgs.filters || {},
110-
function(error, newSearchResults) {
111-
handleNextSearchResults(error, self, newSearchResults, cb);
112-
}
113-
);
74+
// from and size parameters are not valid for a scroll action
75+
if (typeof options.from !== 'undefined') {
76+
delete options.from;
77+
}
11478

115-
return;
79+
if (options.size) {
80+
delete options.size;
11681
}
117-
// retrieve next results with from/size if original search use it
118-
else if (options.from !== undefined && options.size !== undefined) {
119-
filters = Object.assign({}, this.searchArgs.filters);
12082

121-
// check if we need to do next request to fetch all matching documents
122-
options.from += options.size;
83+
this.dataCollection.scroll(options.scrollId, options, this.searchArgs.filters || {}, cb);
12384

124-
if (options.from >= this.total) {
125-
cb(null, null);
85+
return;
86+
}
12687

127-
return;
128-
}
88+
// retrieve next results with from/size if original search use it
89+
if (options.from !== undefined && options.size !== undefined) {
90+
filters = Object.assign({}, this.searchArgs.filters);
91+
92+
// check if we need to do next request to fetch all matching documents
93+
options.from += options.size;
12994

130-
this.dataCollection.search(
131-
filters,
132-
options,
133-
function(error, newSearchResults) {
134-
handleNextSearchResults(error, self, newSearchResults, cb);
135-
}
136-
);
95+
if (options.from >= this.total) {
96+
cb(null, null);
13797

13898
return;
13999
}
140-
}
141100

142-
if (this._next instanceof KuzzleSearchResult) {
143-
cb(null, this._next);
101+
this.dataCollection.search(filters, options, cb);
144102

145103
return;
146104
}
147105

148106
cb(new Error('Unable to retrieve next results from search: missing scrollId or from/size params'));
149107
};
150108

151-
/**
152-
* @param {Error} error
153-
* @param {KuzzleSearchResult} currentSearchResults
154-
* @param {KuzzleSearchResult} newSearchResults
155-
* @param {Function} cb
156-
*/
157-
function handleNextSearchResults (error, currentSearchResults, newSearchResults, cb) {
158-
if (error) {
159-
cb(error);
160-
return;
161-
}
162-
163-
newSearchResults.fetchedDocument += currentSearchResults.fetchedDocument;
164-
165-
newSearchResults._previous = currentSearchResults;
166-
currentSearchResults._next = newSearchResults;
167-
168-
169-
cb(null, newSearchResults);
170-
}
171-
172109
module.exports = KuzzleSearchResult;

test/SearchResult/constructor.test.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ describe('Document constructor', function () {
3535
should(searchResult.documents[0]).be.deepEqual(document);
3636
should(searchResult.searchArgs).be.deepEqual(searchArgs);
3737
should(searchResult.fetchedDocument).be.deepEqual(1);
38-
should(searchResult._previous).be.exactly(null);
39-
should(searchResult._next).be.exactly(null);
4038
});
4139

4240
it('should expose documented properties with the right permissions', function () {
@@ -47,14 +45,11 @@ describe('Document constructor', function () {
4745
should(searchResult).have.propertyWithDescriptor('documents', { enumerable: true, writable: false, configurable: false });
4846
should(searchResult).have.propertyWithDescriptor('searchArgs', { enumerable: true, writable: false, configurable: false });
4947
should(searchResult).have.propertyWithDescriptor('fetchedDocument', { enumerable: true, writable: true, configurable: false });
50-
should(searchResult).have.propertyWithDescriptor('_previous', { enumerable: false, writable: true, configurable: false });
51-
should(searchResult).have.propertyWithDescriptor('_next', { enumerable: false, writable: true, configurable: false });
5248
});
5349

5450
it('should promisify the right functions', function () {
5551
var searchResult = new KuzzleSearchResult(dataCollection, 2, [document], aggregations, searchArgs);
5652

5753
should.exist(searchResult.nextPromise);
58-
should.exist(searchResult.previousPromise);
5954
});
6055
});

test/SearchResult/methods.test.js

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,6 @@ describe('KuzzleSearchResult methods', function () {
2727
});
2828

2929
describe('#next', function () {
30-
it('should return the next SearchResult if it has already fetched', function (done) {
31-
var
32-
firstSearchResult = new KuzzleSearchResult(dataCollection, 2, [firstDocument], {}, searchArgs),
33-
secondSearchResult = new KuzzleSearchResult(dataCollection, 2, [secondDocument], {}, searchArgs, firstSearchResult);
34-
35-
firstSearchResult.next(function(error, next) {
36-
should(error).be.exactly(null);
37-
should(next).be.exactly(secondSearchResult);
38-
done();
39-
});
40-
41-
});
42-
4330
it('should be able to do a scroll request', function (done) {
4431
var
4532
mockScrollResult = new KuzzleSearchResult(
@@ -63,7 +50,6 @@ describe('KuzzleSearchResult methods', function () {
6350
should(result).be.an.instanceOf(KuzzleSearchResult);
6451
should(result.documents).be.an.Array();
6552
should(result.documents.length).be.exactly(1);
66-
should(result._previous).be.exactly(firstSearchResult);
6753
done();
6854
});
6955
});
@@ -162,18 +148,4 @@ describe('KuzzleSearchResult methods', function () {
162148
});
163149
});
164150
});
165-
166-
describe('#previous', function () {
167-
it('should call the callback with the previous SearchResult', function (done) {
168-
var
169-
firstSearchResult = new KuzzleSearchResult(dataCollection, 2, [firstDocument], {}, searchArgs),
170-
secondSearchResult = new KuzzleSearchResult(dataCollection, 2, [secondDocument], {}, searchArgs, firstSearchResult);
171-
172-
secondSearchResult.previous(function(error, previous) {
173-
should(error).be.exactly(null);
174-
should(previous).be.exactly(firstSearchResult);
175-
done();
176-
});
177-
});
178-
});
179151
});

0 commit comments

Comments
 (0)