Skip to content

Instantly share code, notes, and snippets.

@gabonator
Created July 26, 2023 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabonator/64cf3a849d5a6f556da0ac96d147d02c to your computer and use it in GitHub Desktop.
Save gabonator/64cf3a849d5a6f556da0ac96d147d02c to your computer and use it in GitHub Desktop.
elastic search test
// Create sample elastic search database for kibana
// To add new endpoint into kibana:
// Menu (top left) -> Stack management -> Index patterns (Kibana) ->
// Create index pattern (top right) -> "test_endpoint3" -> Timestamp field -> Create index pattern
// To check recent data:
// Menu (top left) -> Discover -> "test_endpoint3"
// To allow elastic search listening on 0.0.0.0 interface add:
// -Djava.net.preferIPv4Stack=true
// to
// /etc/elasticsearch/jvm.options
// Check opened ports with:
// ss -tln
var request = require("request");
data = {
"mappings": {
"properties": {
"a_all": { "type": "integer" },
"a_closed": { "type": "integer" },
"a_opened": { "type": "integer" },
"b_total": { "type": "integer" },
"b_skip": { "type": "integer" },
"b_pass": { "type": "integer" },
"b_fail": { "type": "integer" },
"c_total": { "type": "integer" },
"c_skip": { "type": "integer" },
"c_pass": { "type": "integer" },
"c_fail": { "type": "integer" },
"d_total": { "type": "integer" },
"d_skip": { "type": "integer" },
"d_pass": { "type": "integer" },
"d_fail": { "type": "integer" },
"tag": {"type": "keyword"},
"temp": {"type": "float"},
"timestamp": {"type": "date"},
}
}
};
data_modify = {
"properties": {
"temp": {"type": "integer"},
}
}
function createEndpoint()
{
request({
url: "http://server.com:9200/test_endpoint3",
method: "PUT",
json: data
}, handleResponse);
}
function modifyEndpoint()
{
request({
url: "http://server.com:9200/test_endpoint3/_mapping",
method: "PUT",
json: data_modify
}, handleResponse);
}
function deleteEndpoint()
{
request({
url: "http://server.com:9200/test_endpoint3",
method: "DELETE",
}, handleResponse);
}
function handleResponse(error, data)
{
var onError = msg => {throw msg};
error && onError(error);
data || onError("no data");
data.statusCode == 200 || data.statusCode == 201 || onError("Wrong response: "+JSON.stringify(data));
console.log(data.body);
}
function pushData()
{
var data = {
"a_all": 2400+Math.floor(Math.random()*100),
"a_closed": 1955+Math.floor(Math.random()*100),
"a_opened": 458+Math.floor(Math.random()*100),
"b_total": Math.floor(Math.random()*100),
"b_skip": Math.floor(Math.random()*100),
"b_pass": Math.floor(Math.random()*100),
"b_fail": Math.floor(Math.random()*100),
"c_total": Math.floor(Math.random()*100),
"c_skip": Math.floor(Math.random()*100),
"c_pass": Math.floor(Math.random()*100),
"c_fail": Math.floor(Math.random()*100),
"d_total": Math.floor(Math.random()*100),
"d_skip": Math.floor(Math.random()*100),
"d_pass": Math.floor(Math.random()*100),
"d_fail": Math.floor(Math.random()*100),
"timestamp": new Date().getTime(),
}
request({
url: "http://server:9200/test_endpoint3/_doc",
method: "POST",
json: data
}, handleResponse);
}
function readData()
{
request({
url: "http://server:9200/test_endpoint3/_doc/szn4aIkBzImSLbIfSCb1",
method: "GET"
}, handleResponse);
}
//deleteEndpoint();
createEndpoint();
//modifyEndpoint();
pushData();
//readData();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment