0

I have a very simple graph in which I am returning (recursively) everyone, directly/indirectly, in my graph from a certain person that is older than 5. I am returning the person's properties but I also want to return a "derived relationship" based of a set of criteria, such as "closest female". Is there a better way to do this other than checking the path on each emitted vertex?

Queries:

Get everyone directly/indirectly related to 'A' over 5:

    g.V('a').
      repeat(out()).
      until(has('age',lte(5))).emit().
      project('props').
        by(elementMap()) 

Get everyone directly/indirectly related to 'A' over 5 and their closest female relative:

    g.V('a').
      repeat(out()).
      until(has('age', lte(5))).emit().as('x').
      project('props','closestFemale').
        by(elementMap()).
        by(coalesce(path().unfold().where(neq('x')).
           where(has('gender', 'F')).tail(), constant('n/a')))

Diagram image:
https://i.stack.imgur.com/HOTNg.png

Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38
user3726393
  • 265
  • 1
  • 2
  • 11
  • I like the query logics as is; even the greatest gremlin guru ever did not refrain from using path() steps after a repeat() construct, see the second query in https://tinkerpop.apache.org/docs/current/recipes/#_small_graph_traversals. However, semantically I would expect 'closestFemale' to include implicit sister relations, which your query does not address. – HadoopMarc Oct 15 '22 at 10:38
  • As @HadoopMarc mentioned, using `path` is something that is very common. If you have some steps that build a sample graph (using `addV` and `addE`) it will be easier to potentially suggest alternate approaches (if appropriate). If your current query is performing acceptably, and giving you the results you need, I would not worry too much about improving it. – Kelvin Lawrence Oct 26 '22 at 23:49

0 Answers0