-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6534 from Bargs/countAPI
Count API - count the number of documents in an index pattern
- Loading branch information
Showing
6 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/plugins/kibana/server/routes/api/search/count/register_count.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
}); | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |