Merging maps in Gremlin

你说的曾经没有我的故事 提交于 2020-06-15 04:35:13

问题


I'm trying to write a single query that satisfies two requirements in the response object:

  1. Must include all properties of a vertex, without specifying individual fields in the query.

  2. Must flatten results at the same level on the object.

As separate queries, I can do:

1. valueMap query

g.V(1)
.valueMap().by(unfold()).fold()

Response:

{
  "property1": "value1",
  "property2": "value2"
}

2. project query

g.V(1)
.project("projectedProperty")
.by(out("X").valueMap().by(unfold()).fold())

Response:

{
  "projectedProperty": "value",
}

Combined query

I've attempted to use a union to combine these results:

g.V(1)
.union(
  valueMap().by(unfold()).fold(), // Query 1
  project("projectedField").by(out("X").valueMap().by(unfold()).fold()) // Query 2
).fold()

Response:

{
  [
    {
      "property1": "value1",
      "property2": "value2"
    },
    {
      "projectedProperty": "value"
    }
  ]
}

This union approach relies on me putting a fold at the end, which in turn doesn't merge/flatten the two maps properly. Expected response is:

{
  "property1": "value1",
  "property2": "value2",
  "projectedProperty": "value"
}

Is there a better way to accomplish this task of merging/flattening two maps in the response object?


回答1:


The typical pattern is to unfold() your maps to entries (keys/values) then group() them back together as one:

gremlin> g.V().has('person','name','marko').
......1>   union(project('count').by(outE().count()), elementMap()).
......2>   unfold().
......3>   group().by(keys).by(select(values))
==>[count:3,name:marko,label:person,id:1,age:29]


来源:https://stackoverflow.com/questions/60307292/merging-maps-in-gremlin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!