0
votes

I am wondering how is possible to have a gremlin query which returns results in a nested format. Suppose there is property graph as follows:

USER and PAGE vertices with some properties such as AGE for USER vertex;

FOLLOW edge between USER and PAGE;

I am looking for a single efficient query which gives all Users with age greater than 20 years and all of the followed pages by those users. I can do that using a simple loop from the application side and per each iteration use a simple traversal query. Unfortunately, such solution is not efficient for me, since it will generate lots of queries and network latency could be huge in this case.

2

2 Answers

5
votes

Not sure what your definition of "efficient" is, but keep in mind that this is a typical OLAP use-case and you shouldn't expect fast OLTP realtime responses.

That said, the query should be as simple as:

g.V().has("USER", "AGE", gt(20)).as("user").
  map(out("FOLLOW").fold()).as("pages").
  select("user", "pages")

A small example using the modern sample graph:

gremlin> g = TinkerFactory.createModern().traversal().withComputer()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], graphcomputer]
gremlin> g.V().has("person", "age", gt(30)).as("user").
           map(out("created").fold()).as("projects").
           select("user","projects")
==>[user:v[6], projects:[v[3]]]
==>[user:v[4], projects:[v[5], v[3]]]
0
votes

this is very easy:

g.V().label('user').has('age',gt(20))
.match(__.as('user').out('follows').as('page'))
.select('user','page')

just attention when you are using this query in gremlin, gremlin gives you null pointer exception you can use it in code and check if 'page' exist get that.