简体   繁体   中英

Find path in Neo4j with directed edges

This is my first attempt at Neo4j, please excuse me if I am missing something very trivial. Here is my problem:


Consider the graph as created in the following Neo4j console example: http://console.neo4j.org/?id=y13kbv

We have following nodes in this example:

(Person {memberId, memberName, membershipDate})

(Email {value, badFlag})

(AccountNumber {value, badFlag})

We could potentially have more nodes capturing features related to a Person like creditCard, billAddress, shipAddress, etc. All of these nodes will be the same as Email and AccountNumber nodes: (creditCard {value, badFlag}), (billAddress {value, badFlag}),etc.

With the graph populated as seen in the Neo4j console example, assume that we add one more Person to the graph as follows:

(p7:Person {memberId:'18' , memberName:'John', membershipDate:'12/2/2015'}),
(email6:Email {value: 'john@gmail.com', badFlag:'false'}),
(a2)-[b13:BELONGS_TO]->(p7),
(email6)-[b14:BELONGS_TO]->(p7)

When we add this new person to the system, the use case is that we have to check if there exists a path from features of the new Person ("email6" and "a2" nodes) to any other node in the system where the "badFlag=true", in this case node (a1 {value:1234, badFlag:true}).

Here, the resultant path would be (email6)-[BELONGS_TO]->(p7)<-[BELONGS_TO]-(a2)-[BELONGS_TO]->(p6)<-[BELONGS_TO]-(email5)-[BELONGS_TO]->(p5)<-[BELONGS_TO]-(a1:{badFlag:true})

I tried something like this:

MATCH (newEmail:Email{value:'john@gmail.com'})-[:BELONGS_TO]->(p7)-[*]-(badPerson)<-[:BELONGS_TO]-(badFeature{badFlag:'true'}) RETURN badPerson, badFeature;

which seems to work when there is only one level of chaining, but it doesn't work when the path could be longer like in the case of Neo4j console example.

I need help with the Cypher query that will help me solve this problem. I will eventually be doing this operation using Neo4j's Java API using my application. What could be the right way to go about doing this using Java API?

You had a typo in you query. PART_OF should be BELONGS_TO . This should work for you:

MATCH (newEmail:Email {value:'john@gmail.com'})-[:BELONGS_TO]->(p7)-[*]-(badPerson)<-[:BELONGS_TO]-(badFeature {badFlag:'true'})
RETURN badPerson, badFeature;

Aside: You seem to use string values for all properties. I'd replace the string values 'true' and 'false' with the boolean values true and false . Likewise, values that are always numeric should just use integer or float values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM