gremlinamazon-neptunetinkerpop3graph-traversalgremlinpython

Using Project + valueMap in combination with where


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


Solution

  • 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]]