Count API - count the number of documents in an index pattern

This commit is contained in:
Matthew Bargar 2016-03-14 17:13:32 -04:00
parent ecd13ad0e7
commit d12e2990be
6 changed files with 121 additions and 1 deletions

View file

@ -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({
@ -38,6 +39,7 @@ module.exports = function (kibana) {
init: function (server, options) {
ingest(server);
search(server);
}
});

View file

@ -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',
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));
}
);
}
});
}

View file

@ -0,0 +1,5 @@
import registerCount from './count/register_count';
export default function (server) {
registerCount(server);
}

View file

@ -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: {

View file

@ -0,0 +1,64 @@
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 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);
});
});
};
});

View file

@ -0,0 +1,23 @@
define(function (require) {
var bdd = require('intern!bdd');
var serverConfig = require('intern/dojo/node!../../../serverConfig');
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);
});
});