diff --git a/src/plugins/kibana/index.js b/src/plugins/kibana/index.js index b1ea675fd1057..88ab65eb3d38a 100644 --- a/src/plugins/kibana/index.js +++ b/src/plugins/kibana/index.js @@ -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); } }); diff --git a/src/plugins/kibana/server/routes/api/search/count/register_count.js b/src/plugins/kibana/server/routes/api/search/count/register_count.js new file mode 100644 index 0000000000000..8e542afe70cf0 --- /dev/null +++ b/src/plugins/kibana/server/routes/api/search/count/register_count.js @@ -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)); + } + ); + } + }); +} diff --git a/src/plugins/kibana/server/routes/api/search/index.js b/src/plugins/kibana/server/routes/api/search/index.js new file mode 100644 index 0000000000000..2dfea0d380d4f --- /dev/null +++ b/src/plugins/kibana/server/routes/api/search/index.js @@ -0,0 +1,5 @@ +import registerCount from './count/register_count'; + +export default function (server) { + registerCount(server); +} diff --git a/test/api_intern.js b/test/api_intern.js index c8b9b9aee4040..c30303e96c593 100644 --- a/test/api_intern.js +++ b/test/api_intern.js @@ -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: { diff --git a/test/unit/api/search/_count.js b/test/unit/api/search/_count.js new file mode 100644 index 0000000000000..239a936cfd1ed --- /dev/null +++ b/test/unit/api/search/_count.js @@ -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); + }); + + }); + }; +}); diff --git a/test/unit/api/search/index.js b/test/unit/api/search/index.js new file mode 100644 index 0000000000000..85fd81bc12253 --- /dev/null +++ b/test/unit/api/search/index.js @@ -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); + }); +});