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

如何为现有Gremlin查询添加入站与出站顶点信息?

问题:在Gremlin查询结果中包含匹配顶点的入站和出站顶点

我有一个满足特定筛选条件的Gremlin查询,目前运行正常。现在我希望在返回结果中包含匹配顶点的入站顶点和出站顶点,期望的返回数据结构如下:

{
  "id": "ba872479-90be-4478-b54b-00ea6540fb23",
  "label": "link",
  "type": "vertex",
  "properties": {
   "ListOfIncomingVerticesForThisObject": [],
   "ListOfOutgoingVerticesForThisObject": [],
    
    "state": [
      {
        "id": "71d5c4e5-e89f-424b-9894-af1177ea2294",
        "value": 1
      }
    ],
    "title": [
      {
        "id": "024fde3e-70ce-4d15-8ba1-92e488ffa36e",
        "value": "Some title"
      }
    ],
    "modified": [
      {
        "id": "6cb6e49f-16a8-4308-af41-8b68b14d3d97",
        "value": "2021-02-01"
      }
    ]
  }
}

当前我的查询语句如下(已省略部分信息):

g.V().
  hasLabel('link').
  has("created", between("2020-03-15", "2022-03-15")).
  has("path", TextP.containing("sometext")).
  has('type', within('some types')).
  has('domain', within('domain.com')).
  fold().as('files', 'count').
  select('files', 'count').by(range(local, 0, 1000)).by(count(local))

请问如何修改该查询,以在返回结果中加入入站和出站顶点信息?


解决方案

你需要在筛选出目标顶点后,为每个顶点整合入站/出站顶点信息,再保留原有的分页和计数逻辑。通过project步骤可以精准构造符合你需求的返回结构:

修改后的查询如下:

g.V().
  hasLabel('link').
  has("created", between("2020-03-15", "2022-03-15")).
  has("path", TextP.containing("sometext")).
  has('type', within('some types')).
  has('domain', within('domain.com')).
  project('id', 'label', 'type', 'properties').
    by(id()).
    by(label()).
    by(constant('vertex')).
    by(
      project('ListOfIncomingVerticesForThisObject', 'ListOfOutgoingVerticesForThisObject', 'state', 'title', 'modified').
        by(in().fold()). // 获取所有入站顶点,无匹配时返回空列表
        by(out().fold()). // 获取所有出站顶点,无匹配时返回空列表
        by(values('state').fold()).
        by(values('title').fold()).
        by(values('modified').fold())
    ).
  fold().as('files', 'count').
  select('files', 'count').by(range(local, 0, 1000)).by(count(local))

关键修改说明:

  1. 外层project:将顶点的核心标识(id、label、type)和自定义属性结构整合为统一输出
  2. 内层project:
    • in().fold()/out().fold():分别获取当前顶点的所有入站、出站邻接顶点,fold()确保无匹配时返回空数组,贴合你期望的结构
    • values('xxx').fold():提取顶点自身的state、title、modified属性,保持与原结构一致的格式
  3. 保留原有的fold()、as、select逻辑,确保分页和计数功能正常运行

如果需要筛选特定类型的边对应的顶点,可以给in()/out()添加边标签,比如只获取关联类型的入站顶点:in('关联').fold()。


内容的提问来源于stack exchange,提问作者imp.erial

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 01:58:11