Skip to content

Commit

Permalink
Merge pull request #6534 from Bargs/countAPI
Browse files Browse the repository at this point in the history
Count API - count the number of documents in an index pattern
  • Loading branch information
Rashid Khan committed Mar 15, 2016
2 parents ecd13ad + 6b16b0b commit 866af9a
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/plugins/kibana/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ingest from './server/routes/api/ingest';
import search from './server/routes/api/search';

module.exports = function (kibana) {
return new kibana.Plugin({
Expand Down Expand Up @@ -38,6 +39,7 @@ module.exports = function (kibana) {

init: function (server, options) {
ingest(server);
search(server);
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import _ from 'lodash';
import handleESError from '../../../../lib/handle_es_error';

export default function registerCount(server) {
server.route({
path: '/api/kibana/{id}/_count',
method: ['POST', 'GET'],
handler: function (req, reply) {
const boundCallWithRequest = _.partial(server.plugins.elasticsearch.callWithRequest, req);

boundCallWithRequest('count', {
allowNoIndices: false,
index: req.params.id
})
.then(
function (res) {
reply({count: res.count});
},
function (error) {
reply(handleESError(error));
}
);
}
});
}
5 changes: 5 additions & 0 deletions src/plugins/kibana/server/routes/api/search/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import registerCount from './count/register_count';

export default function (server) {
registerCount(server);
}
3 changes: 2 additions & 1 deletion test/api_intern.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
define({
suites: [
'test/unit/api/ingest/index'
'test/unit/api/ingest/index',
'test/unit/api/search/index'
],
excludeInstrumentation: /(fixtures|node_modules)\//,
loaderOptions: {
Expand Down
72 changes: 72 additions & 0 deletions test/unit/api/search/_count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
define(function (require) {
var Promise = require('bluebird');
var _ = require('intern/dojo/node!lodash');
var expect = require('intern/dojo/node!expect.js');

return function (bdd, scenarioManager, request) {
bdd.describe('Count API', function postIngest() {

bdd.before(function () {
return scenarioManager.client.create({
index: 'foo-1',
type: 'bar',
id: '1',
body: {
foo: 'bar'
}
})
.then(function () {
return scenarioManager.client.create({
index: 'foo-2',
type: 'bar',
id: '2',
body: {
foo: 'bar'
}
});
})
.then(function () {
return scenarioManager.client.indices.refresh({
index: ['foo-1', 'foo-2']
});
});
});

bdd.after(function () {
return scenarioManager.reload('emptyKibana')
.then(function () {
scenarioManager.client.indices.delete({
index: 'foo*'
});
});
});

bdd.it('should return 200 with a document count for existing indices', function () {
return request.post('/kibana/foo-*/_count')
.expect(200)
.then(function (response) {
expect(response.body.count).to.be(2);
});
});

bdd.it('should support GET requests as well', function () {
return request.get('/kibana/foo-*/_count')
.expect(200)
.then(function (response) {
expect(response.body.count).to.be(2);
});
});

bdd.it('should return 404 if a pattern matches no indices', function () {
return request.post('/kibana/doesnotexist-*/_count')
.expect(404);
});

bdd.it('should return 404 if a concrete index does not exist', function () {
return request.post('/kibana/concrete/_count')
.expect(404);
});

});
};
});
23 changes: 23 additions & 0 deletions test/unit/api/search/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
define(function (require) {
var bdd = require('intern!bdd');
var serverConfig = require('intern/dojo/node!../../../server_config');
var ScenarioManager = require('intern/dojo/node!../../../fixtures/scenario_manager');
var request = require('intern/dojo/node!supertest-as-promised');
var url = require('intern/dojo/node!url');
var count = require('./_count');

bdd.describe('search API', function () {
var scenarioManager = new ScenarioManager(url.format(serverConfig.servers.elasticsearch));
request = request(url.format(serverConfig.servers.kibana) + '/api');

bdd.before(function () {
return scenarioManager.load('emptyKibana');
});

bdd.after(function () {
return scenarioManager.unload('emptyKibana');
});

count(bdd, scenarioManager, request);
});
});

0 comments on commit 866af9a

Please sign in to comment.