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
6 changes: 6 additions & 0 deletions src/controllers/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,17 @@ class DocumentController extends BaseController {
body,
action: 'search',
};

for (const opt of ['from', 'size', 'scroll', 'includeTrash']) {
request[opt] = options[opt];
delete options[opt];
}

request.size = request.size || 10;
if (!request.scroll && !request.body.sort && !request.from) {
request.from = 0;
}

return this.query(request, options)
.then(response => new DocumentSearchResult(this.kuzzle, request, options, response.result));
}
Expand Down
49 changes: 45 additions & 4 deletions test/controllers/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1014,8 +1014,8 @@ describe('Document Controller', () => {
index: 'index',
collection: 'collection',
body: {foo: 'bar'},
from: undefined,
size: undefined,
from: 0,
size: 10,
scroll: undefined,
includeTrash: undefined
}, options);
Expand Down Expand Up @@ -1050,8 +1050,8 @@ describe('Document Controller', () => {
index: 'index',
collection: 'collection',
body: {foo: 'bar'},
from: undefined,
size: undefined,
from: 0,
size: 10,
scroll: undefined,
includeTrash: true
}, {});
Expand Down Expand Up @@ -1098,6 +1098,47 @@ describe('Document Controller', () => {
should(res.total).be.equal(3);
});
});

it('should set default value for from and size', () => {
const result = {
hits: [],
total: 0
};
kuzzle.document.query = sinon.stub().resolves({result});

return kuzzle.document.search('index', 'collection', {})
.then(() => {
should(kuzzle.document.query).be.calledOnce();

const request = kuzzle.document.query.getCall(0).args[0];
should(request.from).be.eql(0);
should(request.size).be.eql(10);
});
});

it('should not set default value for from if scroll or sort are specified', () => {
const result = {
hits: [],
total: 0
};
kuzzle.document.query = sinon.stub().resolves({result});

return kuzzle.document.search('index', 'collection', {}, { scroll: '42s' })
.then(() => {
should(kuzzle.document.query).be.calledOnce();

const request = kuzzle.document.query.getCall(0).args[0];
should(request.from).be.undefined();

return kuzzle.document.search('index', 'collection', { sort: { some: 'thing' }});
})
.then(() => {
should(kuzzle.document.query).be.calledTwice();

const request = kuzzle.document.query.getCall(1).args[0];
should(request.from).be.undefined();
});
});
});

describe('update', () => {
Expand Down