My application has filters in English and I need to translate these filters into Gremlin query. Each filter consists of three parts:
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:
The result of the evaluation of the above two filters should be:
While the following two filters:
Should evaluate to the following edges:
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.