elasticsearch - 如何使用 Vega-Lite 仅绘制最新时间的文档?
问题描述
我有一个带有时间字段的索引 - “时间”,对于每个“时间”值,都有多个文档。我只想展示最近一次的结果。我该怎么做?有没有办法计算所有文档的时间字段的最大值,然后根据这个值应用过滤器?
这是代码示例:
{
$schema: https://vega.github.io/schema/vega-lite/v2.json
data: {
data: table
url: {
%context%: true
%timefield%: Time
index: data*
body: {
size: 10000
_source: ["X", "Y"]
}
}
format: {property: "hits.hits"}
}
mark: {type: "square", filled: true, size: 800 }
encoding: {
x: {
field: "_source.X"
type: quantitative
axis: {title: "X"}
}
y: {
field: "_source.Y"
type: quantitative
axis: {title: "Y"}
}
}
}
我尝试了这段代码,但它仍然无法正常工作。我猜我的语法不正确?
{
"$schema": "https://vega.github.io/schema/vega-lite/v2.json",
"data": {
"data": "table",
"url": {
"%context%": true,
"%timefield%": "Time",
"index": "data*",
"body": {
"size": 10000,
"aggs": {
"time_field": {
"terms": {"field": "Time", "size": 1, "order": {"_key": "desc"}},
"aggs": {
"x_Field": {"terms": {"field": "X", "size": 10000}},
"y_Field": {"terms": {"field": "Y", "size": 10000}}
}
}
}
}
},
"format": {"property": "aggregations.category.buckets[0].x_Field.buckets"}
"format": {"property": "aggregations.category.buckets[0].y_Field.buckets"}
},
"mark": {"type": "square", "filled": true, "size": 800},
"encoding": {
"x": {"field": "x_Field", "type": "quantitative", "axis": {"title": "X"}},
"y": {"field": "y_Field", "type": "quantitative", "axis": {"title": "Y"}}
}
}
解决方案
您可以按大小为 1 的降序对时间字段进行排序,这将为您提供属于最新时间的文档,然后您需要根据 elasticsearch 输出更改格式。下面是弹性搜索的示例代码:
"body": {
"size": 10000,
"aggs": {
"time_field": {
"terms": {
"field": "Time",
"size": 1,
"order": {
"_key": "desc"
}
},
"aggs": {
"x_Field": {
"terms": {
"field": "x",
"size": 10000
}
}
}
}
}
}
编辑:使用以下格式后,您可以在 key 中获取展平的 x 字段数据。
"format": {
"property": "aggregations.category.buckets[0].x_Field.buckets"
}
推荐阅读
- flutter - 未调用有状态小部件处置方法
- c# - C# CS2066 错误。无法将浮点数转换为整数
- ios - iphone是否只需要在ios中建立档案?
- java - Checkmarx - 如何验证和清理 HttpServletRequest .getInputStream 以通过 checkmarx 扫描
- reactjs - 如何从函数返回输入标签?
- math - 如何有效地将基数为 2 的大整数转换为基数为 10 的表示?
- node.js - 如何在 Windows 10 中为节点安装 Talib
- java - 在插入和更新时使用 utc 时间自动更新上次更新的字段
- android - 这里映射路线,考虑目的地必须在街道的哪一侧
- go - go2go.playground - 预期类型,找到“类型”(还有 1 个错误)