0

I have the following graph: https://gremlify.com/vxkibjtoz3b if u can't open the link the graph is lloking the following: Graph

You can see there 2 queries. The first one: g.V().out("r").as("a").V().out("rl_pre").out("rl").as("b").where("a", eq("b")).by("attr").select("a", "b")

gives me all vertices which share the same "attr" value. In the second query: g.V().out("r").as("a").V().hasLabel("start").project("lower_pre", "lower"). by(__.out("rl_pre").valueMap("lower_pre_prop")) by(__.out("rl_pre").out("rl").as("b").where("a", eq("b")).by("attr").valueMap("attr"))

I tried to combine the first query with "project" and "valueMap" to get some values of former attributes. Unfortunately the second query isn't working. Does anyone of know who to get properties of former Vertices + the properties in the graph traversal end vertex in combination with the "where" clause?

Expected output

I expect to get all vertex pairs ("upper and lower) which share the same property value for ("attr") + I want to have the "start_attribute" value from the start vertex

Kind regards Stefan

2
  • The first query is going to potentially fan out unexpectedly and be expensive as V (all vertices) will be applied to the results of g.V().out('r'). You may want to fold first to flatten all of the traversals. Can you sat a bit more about the exact output you are looking to produce? Commented Mar 14, 2023 at 14:51
  • Thx for your reply... I edit my article and added the section: Expected output. Hope thie helps :) Commented Mar 14, 2023 at 15:31

1 Answer 1

1

Taking the Gremlify data and pretty printing it a little gives us:

g.addV('wrong_node').as('1').property(single, 'attr', 0).
  addV('start').as('2').
  addV('upper').as('3').property(single, 'attr', 1).
  addV('lower').as('4').property(single, 'attr', 1).
  addV('lower_pre').as('5').property(single, 'lower_pre_prop', 666).
  addV('lower_wrong').as('6').property(single, 'attr', 2).
  addV('upper_2').as('7').property(single, 'attr', 1).
  addE('r').from('2').to('7').
  addE('r').from('2').to('3').
  addE('r').from('2').to('1').
  addE('rl_pre').from('2').to('5').
  addE('rl').from('5').to('4').
  addE('rl').from('5').to('6') 

Using that sample graph, the query can be reduced to:

gremlin> g.V().hasLabel('start').as('s').
......1>   out('r').as('a').
......2>   V().hasLabel('start').
......3>   out('rl_pre').
......4>   out('rl').
......5>   where(eq('a')).
......6>     by('attr').as('b').
......7>   select('s','a','b').
......8>     by().
......9>     by(valueMap(true,'attr')).
.....10>     by(valueMap(true,'attr'))  

which produces (I did not see a "start_attribute" property so left that part out)

==>[s:v[2],a:[id:11,label:upper_2,attr:[1]],b:[id:5,label:lower,attr:[1]]]
==>[s:v[2],a:[id:3,label:upper,attr:[1]],b:[id:5,label:lower,attr:[1]]]   

The valueMap step can be replaced by elementMap in this case which gives a slightly simpler query and more readable output.

gremlin> g.V().hasLabel('start').as('s').
......1>   out('r').as('a').
......2>   V().hasLabel('start').
......3>   out('rl_pre').
......4>   out('rl').
......5>   where(eq('a')).
......6>     by('attr').as('b').
......7>   select('s','a','b').
......8>     by().
......9>     by(elementMap('attr')).
.....10>     by(elementMap('attr'))  

==>[s:v[2],a:[id:11,label:upper_2,attr:1],b:[id:5,label:lower,attr:1]]
==>[s:v[2],a:[id:3,label:upper,attr:1],b:[id:5,label:lower,attr:1]]   
Sign up to request clarification or add additional context in comments.

2 Comments

Super cool and detailed answer. Thank you very much :) Did u like the idea of sharing the graph via gremlify?
Gremlify is helpful yes, but I also like to have the graph creation steps in the post itself in case one day the graph in Gremlify is deleted etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.