2
votes

I am trying to query a graph to return all of the paths with a set of specified relationships.

The graph I have contains the following nodes: Person

The relationships which connect two people are: knows, married

So an example of some data are:

c:Person-[:knows]->b:Person
  a:Person-[:married]->c:Person
  d:Person-[knows]-> a:Person

In my query I would like to be able to find all paths that contain both relationships 'knows' and 'married'; however, I don't care about the ordering of such relationships in the paths. For example, my query should return the following paths:

1) a:Person-[:married]->c:Person-[:knows]->b:Person
2) d:Person-[:knows]->a:Person-[:married]->c:Person

I tried the following query

MATCH p=(a)-[:KNOWS|MARRIED*1..3]-(b)
  RETURN p

However, it returned the paths only having knows relationship or the paths only having married relationship, but not both.

Is there any way of finding the paths I want? Many thanks!

2

2 Answers

0
votes

You can try this

MATCH p=(a)-[rel*]-(b)
WHERE type(rel)='KNOWS' OR type(rel)='MARRIED'
RETURN p

It will provide you all paths

0
votes
MATCH p = (a:Person)-[rels:KNOWS|MARRIED*]->(b:Person)
WITH p, EXTRACT(r IN rels | TYPE(r)) AS types
WHERE 'KNOWS' IN types AND 'MARRIED' IN types
RETURN p