0

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.

Ali
  • 1,759
  • 2
  • 32
  • 69

2 Answers2

5

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]]]
Daniel Kuppitz
  • 10,846
  • 1
  • 25
  • 34
  • what a perfect answer! but problem is that when a user follows many users all of that users will load on memory – pinokio Apr 17 '16 at 10:58
  • @pinokio probably using limit() could be a solution in this situation. – Ali Apr 17 '16 at 12:50
  • @Daniel Kuppitz How can I get certain data (e.g. username, surname etc.) of user/projects? – trojek Oct 09 '17 at 18:18
  • 1
    You would choose a different result structure: `g.V().has("person", "age", gt(30)).as("u").valueMap("name","age").as("user").select("u").out("created").valueMap("name","lang").group().by(select("user"))` – Daniel Kuppitz Oct 10 '17 at 15:00
0

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.

pinokio
  • 97
  • 4