Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[discover] respect the sample size settings when not sorting #4084

Merged
merged 1 commit into from
Jun 3, 2015
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
22 changes: 5 additions & 17 deletions src/kibana/components/courier/fetch/request/_segmented_handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,12 @@ define(function (require) {
_(SegmentedHandle).inherits(Events);
function SegmentedHandle(req) {
SegmentedHandle.Super.call(this);
this._req = req;
}

/**
* Set the sort direction for the request.
*
* @param {string} dir - one of 'asc' or 'desc'
*/
SegmentedHandle.prototype.setDirection = function (dir) {
switch (dir) {
case 'asc':
case 'desc':
return (this._req._direction = dir);
default:
throw new TypeError('unkown sort direction "' + dir + '"');
}
};
// export a couple methods from the request
this.setDirection = _.bindKey(req, 'setDirection');
this.setSize = _.bindKey(req, 'setSize');
}

return SegmentedHandle;
};
});
});
64 changes: 37 additions & 27 deletions src/kibana/components/courier/fetch/request/segmented.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ define(function (require) {

// segmented request specific state
this._initFn = initFn;
this._totalSize = false;
this._remainingSize = false;
this._desiredSize = false;
this._hitsReceived = 0;
this._direction = 'desc';
this._handle = new SegmentedHandle(this);

Expand Down Expand Up @@ -70,15 +70,8 @@ define(function (require) {
var index = self._active = self._queue.shift();

params.index = index;

// Only subtract from remaining size if dealing with the indexPattern's timefield
var timefield = self.source.get('index').timeFieldName;
if (_.keys(params.body.sort)[0] !== timefield) {
self._remainingSize = false;
}

if (self._remainingSize !== false) {
params.body.size = self._remainingSize;
if (self._desiredSize !== false) {
params.body.size = Math.max(self._desiredSize - self._hitsReceived, 0);
}

return params;
Expand Down Expand Up @@ -115,6 +108,35 @@ define(function (require) {
** SegmentedReq specific methods
*********/

/**
* Set the sort direction for the request.
*
* @param {string} dir - one of 'asc' or 'desc'
*/
SegmentedReq.prototype.setDirection = function (dir) {
switch (dir) {
case 'asc':
case 'desc':
return (this._direction = dir);
default:
throw new TypeError('unkown sort direction "' + dir + '"');
}
};

/**
* Set the sort total number of documents to
* emit
*
* Setting to false will not limit the documents,
* if a number is set the size of the request to es
* will be updated on each new request
*
* @param {number|false}
*/
SegmentedReq.prototype.setSize = function (totalSize) {
this._desiredSize = _.parseInt(totalSize) || false;
};

SegmentedReq.prototype._createQueue = function () {
var timeBounds = timefilter.getBounds();
var indexPattern = this.source.get('index');
Expand All @@ -135,18 +157,9 @@ define(function (require) {
hitCount: this._mergedResp.hits.hits.length
});
};
SegmentedReq.prototype._getFlattenedSource = function () {
var self = this;

return self.source._flatten()
.then(function (flatSource) {
var size = _.parseInt(_.deepGet(flatSource, 'body.size'));
if (_.isNumber(size)) {
self._totalSize = self._remainingSize = size;
}

return flatSource;
});
SegmentedReq.prototype._getFlattenedSource = function () {
return this.source._flatten();
};

SegmentedReq.prototype._consumeSegment = function (seg) {
Expand All @@ -161,10 +174,7 @@ define(function (require) {

this._mergeSegment(seg);
this.resp = _.omit(this._mergedResp, '_bucketIndex');

if (this._remainingSize !== false) {
this._remainingSize -= seg.hits.hits.length;
}
this._hitsReceived += seg.hits.hits.length;

if (firstHits) this._handle.emit('first', seg);
if (gotHits) this._handle.emit('segment', seg);
Expand Down Expand Up @@ -212,4 +222,4 @@ define(function (require) {

return SegmentedReq;
};
});
});
1 change: 1 addition & 0 deletions src/kibana/plugins/discover/controllers/discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ define(function (require) {
$scope.updateTime();

segmented.setDirection(sortBy === 'time' ? (sort[1] || 'desc') : 'desc');
segmented.setSize(sortBy === 'time' ? $scope.opts.sampleSize : false);

// triggered when the status updated
segmented.on('status', function (status) {
Expand Down