Search code examples
gremlintinkerpoptinkerpop3gremlin-servertinkergraph

Tinkerpop: select vertex which do not have path to vertex having a property


In Tinkerpop, I want to select vertices which are not directly connected to vertices having property foo equal to bar

For ex:

Vertex user1 = graph.addVertex("vid","one");
Vertex user2 = graph.addVertex("vid","two");
Vertex user3 = graph.addVertex("vid","three");

Vertex tag1 = graph.addVertex("tagKey", "tagKey1");
Vertex tag2 = graph.addVertex("tagKey", "tagKey2");
Vertex tag3 = graph.addVertex("tagKey", "tagKey3");

user1.addEdge("user_tag", tag1);
user2.addEdge("user_tag", tag2);
user2.addEdge("user_tag", tag3);

In above test case I want to select all user vertices which are not connected to tag vertices with tagKey with value of tagKey2. Output should be 2 vertices user3 , user 1


Solution

  • Query to fetch the Vertex those are not connected to tags.

    g.V().hasLabel("Vertex").
        filter(
            not(outE().hasLabel('connected'))
            ).
        properties()
    

    Query to add the Vertex data:

    g.addV('Vertex').as('1').property(single, 'name', 'One').
      addV('Vertex').as('2').property(single, 'name', 'Two').
      addV('Vertex').as('3').property(single, 'name', 'Three').
      addV('Vertex').as('4').property(single, 'name', 'Four').
      addV('Tag').as('5').property(single, 'name', 'Key1').
      addV('Tag').as('6').property(single, 'name', 'Key2').
      addV('Tag').as('7').property(single, 'name', 'Key3').
      addE('connected').from('1').to('5').
      addE('connected').from('2').to('6').
      addE('connected').from('4').to('7')
    

    Gremlify link: https://gremlify.com/f1muf12xhdv/2