ESRestclient 分组按时间查询折线图
时间: 2023-02-25 08:01:49 浏览: 94
vue实现折线图 可按时间查询
5星 · 资源好评率100%
To query data from Elasticsearch and display it in a line chart grouped by time, you can use the Elasticsearch REST client to query your data and then use a charting library like Chart.js to display the data in a line chart.
Here's a basic example in JavaScript using the Elasticsearch REST client and Chart.js:
```
var axios = require('axios');
var Chart = require('chart.js');
var query = {
size: 0,
query: {
bool: {
filter: {
range: {
"@timestamp": {
gte: "2022-01-01",
lte: "2022-12-31"
}
}
}
}
},
aggs: {
date_histogram: {
date_histogram: {
field: "@timestamp",
interval: "month"
},
aggs: {
avg_value: {
avg: {
field: "value"
}
}
}
}
}
};
axios.post('http://your-elasticsearch-url/your-index/_search', query)
.then(function (response) {
var data = response.data.aggregations.date_histogram.buckets.map(function (bucket) {
return {
x: bucket.key_as_string,
y: bucket.avg_value.value
};
});
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
type: 'line',
data: {
datasets: [{
label: 'Avg Value',
data: data,
borderColor: 'rgb(75, 192, 192)',
fill: false
}]
},
options: {
scales: {
xAxes: [{
type: 'time',
time: {
unit: 'month'
}
}]
}
}
});
});
```
This example uses Axios to make a POST request to the Elasticsearch API, passing in a query that aggregates the data by month, calculates the average value for each month, and returns the results. The response data is then mapped to the format required by Chart.js and used to create a line chart.
阅读全文