Search code examples
gremlinjanusgraph

Gremlin query to find path of vertices for the common value of vertex but different vertex


I am trying to write a query to find all path between 2 given vertices

will give an example to understand my question, here's graph like that, I'm using JanusGraph

Vertex_1(id=1, value='Bob', label='user')
Vertex_2(id=2, value='Tom', label='user')
Vertex_3(id=3, value='Tom', label='contact')
Vertex_4(id=4, value='Jamie', label='user')
Edge_1(Vertex_1 -> Vertex_2)
Edge_2(Vertex_3 -> Vertex_4)

Can I find the path from Vertex_1 to Vertex_4 since Vertex_2 and Vertex_3 have same value, Can Vertex_2 and Vertex_3 be considered as a single vertex? Thanks

I have tried the following Gremlin query, but it doesn't work:

g.V().has('value','Bob').repeat(timeLimit(10000).bothE().otherV()).limit(5000).simplePath()).emit().times(3).dedup().has('value','Jamie').limit(1).path().by(id)

Solution

  • In a graph, a path represents a set of nodes and edges that connect the start and end elements together. In the example, you have you 4 distinct elements with edges between 1-2 and 3-4 so there is no "path" between them in the logical sense. You

    paths

    It is possible to use the value of one node as a filter for another node to achieve something like this for your use case:

    g.V().has('value', 'Bob').out().as('a').
      V().where(eq('a')).by('value').
      where(neq('a')).out().path().
        by(valueMap())
    

    However, you would need to do this for each edge traversal, and it would get unwieldy quickly, and the performance would likely be poor.

    A better method would be to either connect 2 and 3 via a shared edge (e.g. sameAs) or perform some entity resolution on the data ingest to combine those entities into one.