0
votes

New to cypher query

g.V().has('Entity1','id',within(id1)).in('Entity2').
where(__.out('Entity3').where(__.out('Entity4').has('name',within(name))))

how to convert the above gremlin to cypher and return adjacent Entity2 invertex.

Here condition is out('Entity3') should be out of Entity2 out('Entity4') should be out of Entity3 and name in the provided list of values

Return adjacent vertex of inEntity2

2

2 Answers

3
votes

Straight answer:

MATCH (m:Entity1 )<-[:Entity2]-(n) 
WHERE (n)-[:Entity3]->()-[:Entity4]->({name: "ABC"}) 
       AND m.id in ["id1"]
RETURN n

# Assuming id is a property here. 

# If id is the actual ID of the node

MATCH (m:Entity1 )<-[:Entity2]-(n) 
WHERE (n)-[:Entity3]->()-[:Entity4]->({name: "ABC"}) 
       AND ID(m) in ["id1"]
RETURN n

I tried to create the graph for you use-case using this query:

CREATE (a:Entity2)-[:Entity2]->(b:Entity1 {id:"id1"}), 
    (a)-[:Entity3]->(:Entity3)-[:Entity4]->(:Entity4 {name:"ABC"})

the graph looks like this:

sample graph

However, I think while writing your gremlin traversal you had the intention of specifying the label of the vertex rather than label of the edge. That is why in the query I wrote to create the graph, the relationship and the vertex, relationship is pointing to have same label.

If that is your intention then your cypher query would look like.

MATCH (:Entity1 {id:"id1"})<--(n:Entity2) 
WHERE (n)-->(:Entity3)-->(:Entity4 {name: "ABC"})
RETURN n

0
votes

I'm not 100% sure what you are looking for as the Gremlin above seems incomplete compared to the description but I think what you are looking for is something like this:

MATCH (e1:Entity1)<-[:Entity2]-(e2)-[:Entity3]->(e3)-[:Entity4]->(e4 {code: 'LHR'})
WHERE e1 IN (id1)
RETURN e2