首页 > 解决方案 > 在 gremlin 中合并地图列表

问题描述

我有这样的关系:

person --likes--> subject

这是我的查询:

g.V().
  hasLabel('person').
  has('name', 'Joe').
  outE('likes').
  range(0, 2).
  union(identity(), inV().hasLabel('subject')).
  valueMap('rating', 'name').

此时,我得到如下所示的结果:

 [
      {
        "rating": 3.236155563
      },
      {
        "rating": 3.162886797
      },
      {
        "name": "math"
      },
      {
        "name": "history"
      }
]

我想得到这样的东西:

 [
      {
        "rating": 3.236155563,
        "name": "math"
      },
      {
        "rating": 3.162886797,
        "name": "history"
      },
]

我尝试对结果进行分组——这给了我想要的结构——但由于相同的键,我只能得到一组结果。

标签: gremlin

解决方案


当您发布代码以创建图表时,它总是会有所帮助,因此我们可以为您提供经过测试的答案。像这样

g.addV('person').property('name', 'P1').as('p1').
  addV('subject').property('name', 'Math').as('math').
  addV('subject').property('name', 'History').as('history').
  addV('subject').property('name', 'Geography').as('geography').
  addE('likes').from('p1').to('math').property('rating', 1.2).
  addE('likes').from('p1').to('history').property('rating', 2.3).
  addE('likes').from('p1').to('geography').property('rating', 3.4)

我相信您正在尝试编写从某个人开始的遍历,沿着前两个“喜欢”边缘走,并获取他喜欢的主题的名称以及相应“喜欢”边缘的评分。

g.V().has('person', 'name', 'P1').
  outE('likes').
  range(0, 2).
  project('SubjectName', 'Rating').
    by(inV().values('name')).
    by(values('rating'))

推荐阅读