Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 15 additions & 31 deletions src/controllers/searchResult/base.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
let _kuzzle;

class SearchResultBase {

/**
Expand All @@ -10,7 +8,7 @@ class SearchResultBase {
* @param {object} response
*/
constructor (kuzzle, request = {}, options = {}, response = {}) {
_kuzzle = kuzzle;
this._kuzzle = kuzzle;
this._request = request;
this._response = response;
this._options = options;
Expand All @@ -31,26 +29,19 @@ class SearchResultBase {
}

if (this._request.scroll) {
return _kuzzle.query({
return this._kuzzle.query({
controller: this._request.controller,
action: this._scrollAction,
scrollId: this._response.scrollId
}, this._options)
.then(response => {
const result = response.result;
this.fetched += result.hits.length;
this._response = result;
this.aggregations = result.aggregations;
this.hits = result.hits;
return this;
});
.then(response => this._buildNextSearchResult(response));
}
else if (this._request.size && this._request.body.sort) {
const
request = Object.assign({}, this._request, {
action: this._searchAction
}),
hit = this._response.hits[this._response.hits.length -1];
hit = this._response.hits[this._response.hits.length - 1];

request.body.search_after = [];

Expand All @@ -65,33 +56,19 @@ class SearchResultBase {
request.body.search_after.push(value);
}

return _kuzzle.query(request, this._options)
.then(response => {
const result = response.result;
this.fetched += result.hits.length;
this._response = result;
this.aggregations = result.aggregations;
this.hits = result.hits;
return this;
});
return this._kuzzle.query(request, this._options)
.then(response => this._buildNextSearchResult(response));
}
else if (this._request.size) {
if (this._request.from >= this._response.total) {
return Promise.resolve(null);
}

return _kuzzle.query(Object.assign({}, this._request, {
return this._kuzzle.query(Object.assign({}, this._request, {
action: this._searchAction,
from: this.fetched
}), this._options)
.then(response => {
const result = response.result;
this.fetched += result.hits.length;
this._response = result;
this.aggregations = result.aggregations;
this.hits = result.hits;
return this;
});
.then(response => this._buildNextSearchResult(response));
}

throw new Error('Unable to retrieve next results from search: missing scrollId, from/sort, or from/size params');
Expand All @@ -110,6 +87,13 @@ class SearchResultBase {
return this._get(object[key], path);
}

_buildNextSearchResult (response) {
const nextSearchResult = new this.constructor(this._kuzzle, this._request, this._options, response.result);
nextSearchResult.fetched += this.fetched;

return nextSearchResult;
}

}


Expand Down
10 changes: 5 additions & 5 deletions src/controllers/searchResult/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ class ProfileSearchResult extends SearchResultBase {

next () {
return super.next()
.then(result => {
if (! result) {
return result;
.then(nextSearchResult => {
if (! nextSearchResult) {
return null;
}

this.hits = this._response.hits.map(hit => new Profile(this._kuzzle, hit._id, hit._source.policies));
return this;
nextSearchResult.hits = nextSearchResult._response.hits.map(hit => new Profile(nextSearchResult._kuzzle, hit._id, hit._source.policies));
return nextSearchResult;
});
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/controllers/searchResult/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ class RoleSearchResult extends SearchResultBase {
}

return super.next()
.then(result => {
if (!result) {
return result;
.then(nextSearchResult => {
if (! nextSearchResult) {
return null;
}

this.hits = this._response.hits.map(hit => new Role(this._kuzzle, hit._id, hit._source.controllers));
nextSearchResult.hits = nextSearchResult._response.hits.map(hit => new Role(nextSearchResult._kuzzle, hit._id, hit._source.controllers));

return this;
return nextSearchResult;
});
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/controllers/searchResult/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ class UserSearchResult extends SearchResultBase {

next () {
return super.next()
.then(result => {
if (!result) {
return result;
.then(nextSearchResult => {
if (! nextSearchResult) {
return null;
}

this.hits = this._response.hits.map(hit => new User(this._kuzzle, hit._id, hit._source, hit._meta));
return this;
nextSearchResult.hits = nextSearchResult._response.hits.map(hit => new User(nextSearchResult._kuzzle, hit._id, hit._source, hit._meta));
return nextSearchResult;
});
}
}
Expand Down
51 changes: 27 additions & 24 deletions test/controllers/searchResult/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,18 @@ describe('DocumentSearchResult', () => {
kuzzle.query.resolves({result: nextResponse});
});

it('should call document/scroll action with scrollId parameter and resolve the current object', () => {
it('should call document/scroll action with scrollId parameter and resolve to a new DocumentSearchResult', () => {
return searchResult.next()
.then(res => {
.then(nextSearchResult => {
should(kuzzle.query)
.be.calledOnce()
.be.calledWith({
controller: 'document',
action: 'scroll',
scrollId: 'scroll-id'
}, options);
should(res).be.equal(searchResult);
should(nextSearchResult).not.be.equal(searchResult);
should(nextSearchResult).be.instanceOf(DocumentSearchResult);
});
});

Expand All @@ -141,11 +142,11 @@ describe('DocumentSearchResult', () => {
should(searchResult._response).be.equal(response);
should(searchResult.aggregations).equal(response.aggregations);
return searchResult.next()
.then(() => {
should(searchResult.fetched).be.equal(4);
should(searchResult._response).be.equal(nextResponse);
should(searchResult.hits).be.equal(nextResponse.hits);
should(searchResult.aggregations).equal(nextResponse.aggregations);
.then(nextSearchResult => {
should(nextSearchResult.fetched).be.equal(4);
should(nextSearchResult._response).be.equal(nextResponse);
should(nextSearchResult.hits).be.equal(nextResponse.hits);
should(nextSearchResult.aggregations).equal(nextResponse.aggregations);
});
});
});
Expand Down Expand Up @@ -177,9 +178,9 @@ describe('DocumentSearchResult', () => {
kuzzle.query.resolves({result: nextResponse});
});

it('should call document/search action with search_after parameter and resolve the current object', () => {
it('should call document/search action with search_after parameter and resolve to a new DocumentSearchResult', () => {
return searchResult.next()
.then(res => {
.then(nextSearchResult => {
should(kuzzle.query)
.be.calledOnce()
.be.calledWith({
Expand All @@ -196,7 +197,8 @@ describe('DocumentSearchResult', () => {
action: 'search',
size: 2
}, options);
should(res).be.equal(searchResult);
should(nextSearchResult).not.be.equal(searchResult);
should(nextSearchResult).be.instanceOf(DocumentSearchResult);
});
});

Expand All @@ -205,11 +207,11 @@ describe('DocumentSearchResult', () => {
should(searchResult._response).be.equal(response);
should(searchResult.aggregations).equal(response.aggregations);
return searchResult.next()
.then(() => {
should(searchResult.fetched).be.equal(4);
should(searchResult._response).be.equal(nextResponse);
should(searchResult.hits).be.equal(nextResponse.hits);
should(searchResult.aggregations).equal(nextResponse.aggregations);
.then(nextSearchResult => {
should(nextSearchResult.fetched).be.equal(4);
should(nextSearchResult._response).be.equal(nextResponse);
should(nextSearchResult.hits).be.equal(nextResponse.hits);
should(nextSearchResult.aggregations).equal(nextResponse.aggregations);
});
});
});
Expand Down Expand Up @@ -253,9 +255,9 @@ describe('DocumentSearchResult', () => {
});


it('should call document/search action with from/size parameters and resolve the current object', () => {
it('should call document/search action with from/size parameters and resolve to a new DocumentSearchResult', () => {
return searchResult.next()
.then(res => {
.then(nextSearchResult => {
should(kuzzle.query)
.be.calledOnce()
.be.calledWith({
Expand All @@ -267,7 +269,8 @@ describe('DocumentSearchResult', () => {
size: 2,
from: 2
}, options);
should(res).be.equal(searchResult);
should(nextSearchResult).not.be.equal(searchResult);
should(nextSearchResult).be.instanceOf(DocumentSearchResult);
});
});

Expand All @@ -276,11 +279,11 @@ describe('DocumentSearchResult', () => {
should(searchResult._response).be.equal(response);
should(searchResult.aggregations).be.equal(response.aggregations);
return searchResult.next()
.then(() => {
should(searchResult.fetched).be.equal(4);
should(searchResult._response).be.equal(nextResponse);
should(searchResult.hits).be.equal(nextResponse.hits);
should(searchResult.aggregations).equal(nextResponse.aggregations);
.then(nextSearchResult => {
should(nextSearchResult.fetched).be.equal(4);
should(nextSearchResult._response).be.equal(nextResponse);
should(nextSearchResult.hits).be.equal(nextResponse.hits);
should(nextSearchResult.aggregations).equal(nextResponse.aggregations);
});
});
});
Expand Down
Loading