Skip to content

Commit 5e62636

Browse files
committed
Add endpoint to get field mapping for given index.
1 parent 11f25c4 commit 5e62636

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ This server exposes the following REST API's:
215215

216216
Returns metadata from elasticsearch related to elasalert's state. `:type` should be one of: elastalert_status, elastalert, elastalert_error, or silence. See [docs about the elastalert metadata index](https://elastalert.readthedocs.io/en/latest/elastalert_status.html).
217217

218+
- **GET `/mapping/:index`**
219+
220+
Returns field mapping from elasticsearch for a given index.
221+
218222
- **[WIP] GET `/config`**
219223

220224
Gets the ElastAlert configuration from `config.yaml` in `elastalertPath` (from the config).

src/handlers/mapping/get.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { getClient } from '../../common/elasticsearch_client';
2+
3+
var client = getClient();
4+
5+
export default function metadataHandler(request, response) {
6+
/**
7+
* @type {ElastalertServer}
8+
*/
9+
10+
client.indices.getMapping({
11+
index: request.params.index
12+
}).then(function(resp) {
13+
response.send(resp);
14+
}, function(err) {
15+
response.send({
16+
error: err
17+
});
18+
});
19+
20+
}

src/handlers/metadata/get.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,29 @@ import { getConfig, getClient } from '../../common/elasticsearch_client';
33
var config = getConfig();
44
var client = getClient();
55

6+
function getQueryString(request) {
7+
if (request.params.type === 'elastalert_error') {
8+
return '*:*';
9+
}
10+
else {
11+
return `rule_name:${request.query.rule_name || '*'}`;
12+
}
13+
}
14+
615
export default function metadataHandler(request, response) {
716
/**
817
* @type {ElastalertServer}
918
*/
10-
1119
client.search({
1220
index: config.writeback_index,
1321
type: request.params.type,
1422
body: {
1523
from : request.query.from || 0,
16-
size : request.query.size || 10,
24+
size : request.query.size || 100,
1725
query: {
18-
match_all: {}
26+
query_string: {
27+
query: getQueryString(request)
28+
}
1929
},
2030
sort: [{ '@timestamp': { order: 'desc' } }]
2131
}

src/routes/routes.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import testPostHandler from '../handlers/test/post';
1515
import configGetHandler from '../handlers/config/get';
1616
import configPostHandler from '../handlers/config/post';
1717
import metadataHandler from '../handlers/metadata/get';
18+
import mappingHandler from '../handlers/mapping/get';
1819

1920
/**
2021
* A server route.
@@ -80,6 +81,11 @@ let routes = [
8081
path: 'metadata/:type',
8182
method: ['GET'],
8283
handler: [metadataHandler]
84+
},
85+
{
86+
path: 'mapping/:index',
87+
method: ['GET'],
88+
handler: [mappingHandler]
8389
}
8490
];
8591

0 commit comments

Comments
 (0)