0

Suppose I want to query the Neptune graph with "group-by" on one property (or more), and I want to get back the list of vertices too.

Let's say, I want to group-by on ("city", "age") and want to get the list of vertices too:

[
  {"city": "SFO", "age": 29, "persons": [v[1], ...]},
  {"city": "SFO", "age": 30, "persons": [v[10], v[13], ...]}, 
  ...
]

Or, get back the vertex with its properties (as valueMap):

[
  {"city": "SFO", "age": 29, "persons": [[id:1,label:person,name:[marko],age:[29],city:[SFO]], ...]},
  ...
]

AFAIK, Neptune doesn't support lambda nor variable assignments. is there a way to do this with one traversal and no lambdas?

Update: I'm able to get the vertices, but without their properties (with valueMap).

Query:

g.V().hasLabel("person").group().
   by(values("city", "age").fold()).
   by(fold().
     match(__.as("p").unfold().values("city").as("city"),
           __.as("p").unfold().values("age").as("age"),
           __.as("p").fold().unfold().as("persons")).
     select("city", "age", "persons")).
   select(values).
   next()

Output:

==>[city:SFO,age:29,persons:[v[1]]]
==>[city:SFO,age:27,persons:[v[2],v[23]]]
...
mux
  • 455
  • 5
  • 10

1 Answers1

1

If I understand it correctly, then ...

g.V().hasLabel("person").
  group().
    by(values("city", "age").fold())

... or ...

g.V().hasLabel("person").
  group().
    by(valueMap("city", "age").by(unfold()))

... already gives you what you need, it's just about reformating the result. To merge the maps in keys and values together, you can do something like this:

g.V().hasLabel("person").
  group().
    by(valueMap("city", "age").by(unfold())).
  unfold().
  map(union(select(keys), 
            project("persons").
              by(values)).
      unfold().
      group().
        by(keys).
        by(select(values)))

Executing this on the modern toy graph (city replaced with name) will yield the following result:

gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin>     g.V().hasLabel("person").
......1>       group().
......2>         by(valueMap("name", "age").by(unfold())).
......3>       unfold().
......4>       map(union(select(keys), 
......5>                 project("persons").
......6>                   by(values)).
......7>           unfold().
......8>           group().
......9>             by(keys).
.....10>             by(select(values)))
==>[persons:[v[2]],name:vadas,age:27]
==>[persons:[v[4]],name:josh,age:32]
==>[persons:[v[1]],name:marko,age:29]
==>[persons:[v[6]],name:peter,age:35]
Daniel Kuppitz
  • 10,846
  • 1
  • 25
  • 34
  • You are right. I'm reformatting the output because I'm not able to represent the original group-by traversal as a valid JSON (map keys are list type). Thanks for your help. – mux Apr 22 '19 at 12:10