简体   繁体   中英

Gremlin get edges sharing the same vertex

My application has filters in English and I need to translate these filters into Gremlin query. Each filter consists of three parts:

  1. Type of vertex
  2. Label of outgoing edge from vertex in #1
  3. Name of incoming vertex from edge in #2

Any of the part can take the string "any", which signifies any type, label or name can be included in the result. Using the Modern toy graph as example, I have the following two filters:

  1. person -> created -> any
  2. person -> knows -> vadas

The result of the evaluation of the above two filters should be:

  1. marko -> created -> lop
  2. marko -> knows -> vadas

While the following two filters:

  1. person -> any -> josh
  2. person -> created -> lop

Should evaluate to the following edges:

  1. marko -> knows -> josh
  2. marko -> created -> lop

The query I come up with the closest result to the desired results above is:

g.E().and(outV().outE().has(label, "created"), outV().outE().has(label, "knows").inV().has("name", "vadas"), outV().has(label, "person"))

The problem with the above query is that it returns all three edges going out from marko, not just two desired edges. How can I improve my query to return only the two edges as described above?

This solution takes the approach of separating the filters from the traversals that return the results.

gremlin> Gremlin.version()
==>3.3.3
gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().
......1>     and(
......2>         outE('created'),
......3>         out('knows').has('name', 'vadas')
......4>     ).
......5>     union(
......6>         outE('created').inV(),
......7>         outE('knows').inV().has('name', 'vadas')
......8>     ).
......9>     path().by('name').by(label)
==>[marko,created,lop]
==>[marko,knows,vadas]
gremlin> g.V().
......1>     and(
......2>         out().has('name', 'josh'),
......3>         out('created').has('name', 'lop')
......4>     ).
......5>     union(
......6>         outE().inV().has('name', 'josh'),
......7>         outE('created').inV().has('name', 'lop')
......8>     ).
......9>     path().by('name').by(label)
==>[marko,knows,josh]
==>[marko,created,lop]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM