问题
I am currently working on a program using graph db - neo4j and I need to realize the following function.
- I have two types of nodes, type A means stage, type N means let the user do some selections.
- First we have node A1, which has several (2-5) type N children, N1, N2, N3, ...
- Nodes A1 also have child nodes A2, A3, ...
- In java, after arriving at A1, I will ask the users to do some selections according to Ni, then go to a type A child based on a function of the selections. For example, if N1=true, N2=true, N3=false, I go to A2, otherwise, I goto A3.
BTW, I will meet this situation many many times in my program. Do you guys have any idea how to implement it efficiently.
Thanks in advance.
回答1:
Suggestion for Setup
(Ax)-[:TRUE ]->(Nx)-[:TRUE ]->(Ax+1)
(Ax)-[:FALSE]->(Nx)-[:FALSE]->(Ax+1)
Suggestion for Query
MATCH (a:A {id:1}),
(a)-[:TRUE]-> (n)-[:FALSE]->(a2),
(a)-[:FALSE]->(n2)-[:TRUE]->(a2),
(a)-[:TRUE]-> (n)-[:FALSE]->(a2)
RETURN a2;
回答2:
Thanks to @Michael Hunger, I think I find an acceptable solution, although it seems a lot of work to do.
Specifically, I expand all the pathes from A1 through Ni and link the only path when all Ni=true to A2* and all the other paths to A3 as the following graph:
来源:https://stackoverflow.com/questions/35694532/neo4j-conditional-relationship