sunji
sunji

Reputation: 1

Cypher: Returning relationships between nodes that have another type of relationship between them

I'm new to Neo4j Community Edition 3.1.0 and I'm trying to create an efficient Cypher query that matches a relationship of a certain type (type A) between nodes ONLY if those nodes currently have a (type B) relationship between them. For instance, what I have right now is:

MATCH (n)-[:B]-(m)
WITH n,m
MATCH (n)-[r:A]-(m)
RETURN r

In my graph, there are significantly more (type B) relationships than (type A), so I'm worried that my query will be very inefficient since it is matching all the (type B) relationships first. Is my query correct; and if so, how can I make it more efficient?

Upvotes: 0

Views: 185

Answers (1)

RafaelCaballero
RafaelCaballero

Reputation: 1613

I think it is better to have only one query like:

MATCH (n)-[r:A]-(m)-[:B]-(n)
return r

In this way the query planner can consider relation A before relation B.

Upvotes: 0

Related Questions