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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ https://github.com/kuzzleio/kuzzle-sdk/issues

## Protocols used

The SDK Javascript implements two network protocols: raw WebSocket, and [Socket.IO](http://socket.io/)
The Javascript SDK implements two network protocols: raw WebSocket, and [Socket.IO](http://socket.io/)
The main reason behind this is that while Socket.IO offers better compatibility with older web browsers, our raw WebSocket implementation is about 20% faster

What protocol is used when you connect to Kuzzle depends on multiple factors:
Which protocol is used when you connect to Kuzzle depends on multiple factors:

#### NodeJS

Expand Down Expand Up @@ -71,8 +71,8 @@ Clone this github repository and run ``npm run build``. A ``dist`` directory wil
If you want to support older browser versions, you may load `socket.io` before Kuzzle, making the SDK compatible with browsers without websocket support:

```html
<!-- Don't forget to include socketio before kuzzle SDK -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.6.0/socket.io.min.js"></script>
<!-- Don't forget to include socketio before Kuzzle SDK -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.slim.js"></script>
```

## License
Expand Down
1 change: 0 additions & 1 deletion src/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,6 @@ Collection.prototype.search = function (filters, options, cb) {

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


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

Expand Down
25 changes: 24 additions & 1 deletion src/SearchResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ function SearchResult (collection, total, documents, aggregations, options, filt
SearchResult.prototype.fetchNext = function (cb) {
var
filters,
options = Object.assign({}, this.options);
options = Object.assign({}, this.options),
self = this;

options.previous = this;

Expand All @@ -96,6 +97,28 @@ SearchResult.prototype.fetchNext = function (cb) {
return;
}

// retrieve next results using ES's search_after
if (options.size && this.filters.sort) {
if (this.fetchedDocument >= this.getTotal()) {
cb(null, null);
return;
}

if (options.from) {
delete options.from;
}

filters = Object.assign(this.filters, {search_after: []});

filters.sort.forEach(function (sortRule) {
filters.search_after.push(self.documents[self.documents.length - 1].content[Object.keys(sortRule)[0]]);
});

this.collection.search(filters, options, cb);

return;
}

// retrieve next results with from/size if original search use it
if (options.from !== undefined && options.size !== undefined) {
filters = Object.assign({}, this.filters);
Expand Down
40 changes: 40 additions & 0 deletions test/SearchResult/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,46 @@ describe('SearchResult methods', function () {
collection.scroll = sinon.stub();
});

it('should be able to perform a search-after request', function (done) {
var
firstSearchResult;

searchFilters = {sort: [{foo: 'asc'}]};

this.timeout(50);

collection.search = function(filters, options, cb) {
cb(null, new SearchResult(collection, 2, [secondDocument], {}, {size: 1}, searchFilters));
};

firstSearchResult = new SearchResult(collection, 2, [firstDocument], {}, {size: 1}, searchFilters);
firstSearchResult.fetchNext(function(error, result) {
should(result).be.an.instanceOf(SearchResult);
should(result.getDocuments()).be.an.Array();
should(result.getDocuments().length).be.exactly(1);
done();
});
});

it('should transfer error if not-able to do a search-after request', function (done) {
var
firstSearchResult;

searchFilters = {sort: [{foo: 'asc'}]};

collection.search = function(filters, options, cb) {
cb(new Error('foobar search'));
};

firstSearchResult = new SearchResult(collection, 2, [firstDocument], {}, {size: 1}, searchFilters);

firstSearchResult.fetchNext(function(error) {
should(error).be.an.instanceOf(Error);
should(error.message).be.exactly('foobar search');
done();
});
});

it('should be able to do a scroll request', function (done) {
var
firstSearchResult;
Expand Down