【发布时间】:2021-09-30 14:17:30
【问题描述】:
我第一次使用 nodejs,所以请提供一些帮助。 我在 localhost 上运行了 elasticsearch(http://localhost:9200) 和 kibana(http://localhost:5601)。 在弹性搜索中,我有正在访问的索引出勤率。 我想制作 nodejs api,它将从这个 elasticsearch 服务器获取数据并将其显示在浏览器上。 我使用了以下代码:
var http = require('http');
const { Client } = require('@elastic/elasticsearch')
const client = new Client({ node: 'http://localhost:9200' })
var events = require('events');
var url = require('url');
var eventEmitter = new events.EventEmitter();
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(req.url);
eventEmitter.emit(req.url);
eventEmitter.on('/connection/', connect_new);
res.end();
}).listen(8080);
var connect_new=async function run () {
await client.search({
index: 'attendance',
body:{
"aggs": {
"3": {
"terms": {
"field": "timevar.keyword",
"order": {
"_count": "desc"
},
"size": 8
},
"aggs": {
"4": {
"terms": {
"field": "type.keyword",
"order": {
"_count": "desc"
},
"size": 5
}
}
}
}
},
"size": 0,
"fields": [
{
"field": "@timestamp",
"format": "date_time"
},
{
"field": "date",
"format": "date_time"
},
{
"field": "in_timestamp",
"format": "date_time"
}
],
"script_fields": {},
"stored_fields": [
"*"
],
"runtime_mappings": {},
"_source": {
"excludes": []
},
"query": {
"bool": {
"must": [],
"filter": [
{
"match_all": {}
},
{
"range": {
"in_timestamp": {
"gte": "2021-07-13T07:18:27.397Z",
"lte": "2021-07-13T08:22:57.064Z",
"format": "strict_date_optional_time"
}
}
}
],
"should": [],
"must_not": []
}
}
}}).then(function(resp) {
//resp.writeHead(200, {'Content-Type': 'text/html'});
//resp.write('time interval:- ' + resp.body.aggregations['3']["buckets"][0]["key"]);
//resp.write('total students:- ' + resp.body.aggregations['3']["buckets"][0]["doc_count"]);
console.log(resp);
console.log('time interval:- ' + resp.body.aggregations['3']["buckets"][0]["key"]);
console.log('total students:- ' + resp.body.aggregations['3']["buckets"][0]["doc_count"]);
console.log('time interval:- ' + resp.body.aggregations['3']["buckets"][1]["key"]);
console.log('total students:- ' + resp.body.aggregations['3']["buckets"][1]["doc_count"]);
//res.end();
}, function(err) {
console.trace(err.message);
});
}
它在命令行中为我提供以下输出:
{
body: {
took: 4,
timed_out: false,
_shards: { total: 1, successful: 1, skipped: 0, failed: 0 },
hits: { total: [Object], max_score: null, hits: [] },
aggregations: { '3': [Object] }
},
statusCode: 200,
headers: {
warning: '299 Elasticsearch-7.13.3-5d21bea28db1e89ecc1f66311ebdec9dc3aa7d64 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.13/security-minimal-setup.html to enable security."',
'content-type': 'application/json; charset=UTF-8',
'content-length': '827'
},
meta: {
context: null,
request: { params: [Object], options: {}, id: 2 },
name: 'elasticsearch-js',
connection: {
url: 'http://localhost:9200/',
id: 'http://localhost:9200/',
headers: {},
deadCount: 0,
resurrectTimeout: 0,
_openRequests: 0,
status: 'alive',
roles: [Object]
},
attempts: 0,
aborted: false
}
}
time interval:- INTERVAL_01-02
total students:- 54
time interval:- INTERVAL_12-01
total students:- 13
但我希望在 Web 浏览器中输出这个 JSON。现在我只得到低于输出:
/connection/
请给我一个可能的解决方案。任何建议表示赞赏。
【问题讨论】:
-
IANAL,但 the elastic licence 说 您不得将软件作为托管或托管服务提供给第三方...
标签: node.js elasticsearch