问题
I am trying to build a cypher query for two scenarios :
- Tests having depth more than 2
- Specific test having depth more than 2
As in Image you can see tests 1, 2, 3 are somewhat related through depth more than 2. The cypher which i ran was :
MATCH p=()<-[r:TEST_FOR*..10]-() RETURN p LIMIT 50
Now when i Change my cypher to below then i get no records/results or nodes.
1) MATCH p=()<-[r:TEST_FOR*2..5]-() RETURN p LIMIT 50
2) MATCH p=()<-[r:TEST_FOR*2]-() RETURN p LIMIT 50
3) MATCH p=(d:Disease)<-[r:TEST_FOR*]-(t:Tests) WHERE t.testname = 'Alkaline Phosphatase (ALP)' RETURN p
4) MATCH p=()<-[r:TEST_FOR*..10]-(t:Tests {testname:'Alkaline Phosphatase (ALP)'}) RETURN p LIMIT 50
When I run Query 3 and 4 above, I get same results, i.e 1 test with 5 diseases, but it does not extend out further for that specific test.
But if you see the image the test is connected to two other tests ! My structure is as follows :
- Tests (testname)
- Disease (diseasename, did)
- Linknode (parentdieaseid, testname)
I had used below query to create Relationship "TEST_FOR"
match(d:Disease), (l:Linknode) where d.did = l.parentdiseaseid
with d, l.testname as name
match(t:Test {testname:name}) create (d)<-[:TEST_FOR]-(t);
回答1:
Direction is the problem here. Each of your 4 queries above uses a directed variable-length matching pattern, meaning that every relationship traversed must use the direction indicated (incoming). However, that won't work once you hit :Tests nodes, since they only have outgoing relationships to :Disease nodes.
The easy fix is to omit the direction, so the matching pattern will traverse :TEST_FOR relationships regardless of direction. For example:
MATCH p=()-[r:TEST_FOR*2..5]-() RETURN p LIMIT 50
Note that the reason your original query
MATCH p=()<-[r:TEST_FOR*..10]-() RETURN p LIMIT 50
was returning the full graph is because it was never traversing deeper than a single hop from each :Disease node (since it would never be able to traverse an incoming :TEST_FOR relationship from a :Tests node), but it was starting from every :Disease node, so naturally that hit every single :Tests node as well. You would have gotten the same graph with this:
MATCH p=()<-[r:TEST_FOR]-() RETURN p LIMIT 50
来源:https://stackoverflow.com/questions/51001233/neo4j-variable-depth-not-working