You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Elasticsearch索引构建Vega力导向图的技术问询

Vega 对接 Elasticsearch 实现企业关系可视化

核心思路

要对接你的Elasticsearch索引,需要在Vega中配置ES数据源,关联companies和relationship两个索引的数据,再通过Vega的编码规则实现节点样式、标签和链接的需求。以下是完整可复用的配置方案,适配你的索引结构和需求。


完整Vega配置示例

{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "width": 800,
  "height": 600,
  "padding": 5,
  "signals": [
    {
      "name": "nodeSize", "value": 20,
      "bind": {"input": "range", "min": 10, "max": 50, "step": 1}
    }
  ],
  "data": [
    // 1. 从Elasticsearch获取企业节点数据
    {
      "name": "companies",
      "url": {
        "index": "companies",
        "body": {
          "query": {"match_all": {}},
          "_source": ["name", "group", "unique_id"]
        }
      },
      "format": {"type": "json", "property": "hits.hits"},
      "transform": [
        {
          "type": "formula",
          "as": "id",
          "expr": "datum._source.unique_id"
        },
        {
          "type": "formula",
          "as": "name",
          "expr": "datum._source.name"
        },
        {
          "type": "formula",
          "as": "group",
          "expr": "datum._source.group"
        }
      ]
    },
    // 2. 从Elasticsearch获取关系链接数据
    {
      "name": "relationships",
      "url": {
        "index": "relationship",
        "body": {
          "query": {"match_all": {}},
          "_source": ["source", "target", "relationship", "group_id"]
        }
      },
      "format": {"type": "json", "property": "hits.hits"},
      "transform": [
        {
          "type": "formula",
          "as": "source",
          "expr": "datum._source.source"
        },
        {
          "type": "formula",
          "as": "target",
          "expr": "datum._source.target"
        },
        {
          "type": "formula",
          "as": "rel_label",
          "expr": "datum._source.relationship"
        },
        {
          "type": "formula",
          "as": "group_id",
          "expr": "datum._source.group_id"
        }
      ]
    },
    // 3. 关联节点与链接,补充节点的group_id(从关系数据匹配)
    {
      "name": "nodes",
      "source": "companies",
      "transform": [
        {
          "type": "lookup",
          "from": "relationships",
          "key": "source",
          "fields": ["id"],
          "values": ["group_id"],
          "as": ["group_id"]
        },
        // 处理无关联的节点,默认group_id设为0
        {
          "type": "formula",
          "as": "group_id",
          "expr": "isValid(datum.group_id) ? datum.group_id : 0"
        }
      ]
    },
    // 4. 整理链接数据,确保source/target对应节点id
    {
      "name": "links",
      "source": "relationships"
    }
  ],
  "scales": [
    // 节点配色:按group_id映射不同颜色
    {
      "name": "colorScale",
      "type": "ordinal",
      "domain": {"data": "nodes", "field": "group_id"},
      "range": ["#ff7f0e", "#1f77b4", "#2ca02c", "#d62728", "#9467bd"]
    },
    // 节点形状:按company的group字段映射
    {
      "name": "shapeScale",
      "type": "ordinal",
      "domain": ["holding company", "technology & services", "streaming"],
      "range": ["circle", "triangle-up", "square"]
    }
  ],
  "marks": [
    // 绘制关系链接
    {
      "type": "line",
      "from": {"data": "links"},
      "encode": {
        "enter": {
          "source": {"data": "nodes", "field": "id", "index": {"data": "links", "field": "source"}},
          "target": {"data": "nodes", "field": "id", "index": {"data": "links", "field": "target"}},
          "stroke": {"scale": "colorScale", "field": "group_id"},
          "strokeWidth": {"value": 2}
        }
      }
    },
    // 绘制链接标签
    {
      "type": "text",
      "from": {"data": "links"},
      "encode": {
        "enter": {
          "text": {"field": "rel_label"},
          "fontSize": {"value": 12},
          "fill": {"value": "#333"},
          "align": {"value": "center"},
          "baseline": {"value": "middle"}
        },
        "update": {
          "x": {"signal": "(path('links', datum).x1 + path('links', datum).x2)/2"},
          "y": {"signal": "(path('links', datum).y1 + path('links', datum).y2)/2"}
        }
      }
    },
    // 绘制节点
    {
      "type": "symbol",
      "from": {"data": "nodes"},
      "encode": {
        "enter": {
          "size": {"signal": "nodeSize * nodeSize"},
          "shape": {"scale": "shapeScale", "field": "group"},
          "fill": {"scale": "colorScale", "field": "group_id"},
          "stroke": {"value": "#fff"},
          "strokeWidth": {"value": 1.5}
        },
        "update": {
          "x": {"field": "x"},
          "y": {"field": "y"}
        }
      }
    },
    // 绘制节点标签
    {
      "type": "text",
      "from": {"data": "nodes"},
      "encode": {
        "enter": {
          "text": {"field": "name"},
          "fontSize": {"value": 13},
          "fill": {"value": "#000"},
          "align": {"value": "center"},
          "baseline": {"value": "bottom"},
          "dx": {"value": 0},
          "dy": {"value": -8}
        },
        "update": {
          "x": {"field": "x"},
          "y": {"field": "y"}
        }
      }
    }
  ],
  // 力导向布局,自动计算节点位置
  "layout": {
    "type": "force",
    "link": {"source": "source", "target": "target", "distance": 150},
    "node": {"size": {"signal": "nodeSize"}},
    "iterations": 300
  }
}

关键配置说明

  1. Elasticsearch数据源配置

    • 每个索引对应一个data块,通过url.index指定索引名,body定义ES查询规则(这里用match_all获取全量数据,可根据需求添加过滤条件)
    • format.property: "hits.hits"用于解析ES返回的嵌套结构,提取实际数据
    • 用transform.formula把ES返回的_source字段映射为Vega可用的字段
  2. 数据关联处理

    • 通过lookup转换,把relationship中的group_id关联到对应的节点,确保节点能按group_id配色
    • 处理无关联节点的默认group_id,避免渲染错误
  3. 样式与交互配置

    • colorScale:按group_id分配不同颜色,可自行修改range中的色值
    • shapeScale:严格匹配你的group字段值,映射对应的节点形状
    • 力导向布局force自动计算节点位置,可调整distance控制节点间距

常见问题排查

  • 若出现数据加载错误:检查ES实例的跨域配置、索引名拼写、字段权限
  • 若节点/链接不显示:确认source/target与节点id的整数类型一致,无类型不匹配问题
  • 若标签重叠:调整layout.force.distance或节点大小,开启Vega的标签避让配置(可添加transform: [{"type": "label"}]到文本标记)

内容的提问来源于stack exchange,提问作者Ranger Rick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 20:13:26