首页 > 解决方案 > 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.propertyconfig来访问。

{
  "$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"
}
]

或者一种在重复矩阵中动态重命名轴的方法?

标签: elasticsearchvega-lite

解决方案


有一种方法,您必须提供一次字段,它将在单层而不是嵌套上。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"}
    }
  }
}

推荐阅读