elasticsearch - Vega-lite 数据转换为非嵌套对象
问题描述
数据来自 elasticsearch url,格式如下:
{
"took": 44,
"timed_out": false,
"hits": {
"total": 11,
"max_score": 0,
"hits": [
{
"_index": "dataindex",
"_type": "span",
"_id": "tKVUs3kBhoeKMUMeIwCv",
"_score": 0,
"_source": {
"fieldA": 272.2,
"fieldB": 73,
"fieldX": "event 1"
}
},
{
"_index": "dataindex",
"_type": "span",
"_id": "iuVetHkBhoeKMUMe4O92",
"_score": 0,
"_source": {
"fieldA": 305.2,
"fieldB": 80,
"fieldX": "event 2"
}
},
{
"_index": "dataindex",
"_type": "span",
"_id": "Yt-QwXkBhoeKMUMex3tp",
"_score": 0,
"_source": {
"fieldA": 281.8,
"fieldB": 73,
"fieldX": "event 3"
}
}
]
}
}
我希望制作一个散点图矩阵。下面的数据数组hits.hits
可以通过format.property
config来访问。
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"repeat": {
"row": [
"_source.fieldA",
"_source.fieldB"
],
"column": [
"_source.fieldB",
"_source.fieldA"
]
},
"spec": {
"data": {
"url": "url/to/elastic/query",
"format": {"property": "hits.hits", "type": "json"}
},
"mark": "point",
"encoding": {
"x": {
"field": {"repeat": "column"},
"type": "quantitative"
},
"y": {
"field": {"repeat": "row"},
"type": "quantitative"
},
"color": {
"field": "_source.fieldX",
"type": "nominal"
},
"shape": {
"field": "_source.fieldX",
"type": "nominal"
}
}
}
}
但是仍然有一个额外的_source
级别需要指向任何地方(重复、颜色、形状),而且它在轴和图例标题中也是如此:
有没有可以摆脱这个_source
级别的转换类型?因此,进入encoding
阶段的数据可以像来源一样简单
[
{
"fieldA": 272.2,
"fieldB": 73,
"fieldX": "event 1"
},{
"fieldA": 305.2,
"fieldB": 80,
"fieldX": "event 2"
}
]
或者一种在重复矩阵中动态重命名轴的方法?
解决方案
有一种方法,您必须提供一次字段,它将在单层而不是嵌套上。calculate
如下所示或在编辑器中执行转换:
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"transform": [
{"calculate": "datum._source.fieldA", "as": "fieldA"},
{"calculate": "datum._source.fieldB", "as": "fieldB"},
{"calculate": "datum._source.fieldX", "as": "fieldX"}
],
"repeat": {
"row": ["fieldA", "fieldB"],
"column": ["fieldB", "fieldA"]
},
"spec": {
"data": {
"values": {
"took": 44,
"timed_out": false,
"hits": {
"total": 11,
"max_score": 0,
"hits": [
{
"_index": "dataindex",
"_type": "span",
"_id": "tKVUs3kBhoeKMUMeIwCv",
"_score": 0,
"_source": {"fieldA": 272.2, "fieldB": 73, "fieldX": "event 1"}
},
{
"_index": "dataindex",
"_type": "span",
"_id": "iuVetHkBhoeKMUMe4O92",
"_score": 0,
"_source": {"fieldA": 305.2, "fieldB": 80, "fieldX": "event 2"}
},
{
"_index": "dataindex",
"_type": "span",
"_id": "Yt-QwXkBhoeKMUMex3tp",
"_score": 0,
"_source": {"fieldA": 281.8, "fieldB": 73, "fieldX": "event 3"}
}
]
}
},
"format": {"property": "hits.hits", "type": "json"}
},
"mark": "point",
"encoding": {
"x": {"field": {"repeat": "column"}, "type": "quantitative"},
"y": {"field": {"repeat": "row"}, "type": "quantitative"},
"color": {"field": "fieldX", "type": "nominal"},
"shape": {"field": "fieldX", "type": "nominal"}
}
}
}
推荐阅读
- reactjs - 具有道具大小的二维数组仅更新一维
- python - 无法使用 Python SQlite3 启用扩展
- python - 正则表达式:定义一个特定的组并避免每次都重复
- python - 在北极立体投影上用cartopy绘制等纬度线
- python - 返回所有出现多次且具有不同值的键?
- sql - 我可以在 Firebird POSITION 函数中使用通配符吗
- python - 安装 anaconda 是否与已安装的 Python3 和 R 冲突?
- python - “ NotImplementedError: sched 尚未在 Skulpt 第 4 行实现” Python 2.7
- discord.js - 如何检查机器人是否具有管理 webhook 的权限?
- python-3.x - Discord.py 区分两个不同的服务器