Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Pause, resolving how to handle anonymous content

searching using 'or' on entities

Node Clone


I am a Neo4j newbie. 

I have a Neo4j graph with 3 entities: Person, FirstName, Postcode - they are related to each other as follows: 

Person -> [:HasFirstName] -> FirstName 

Person -> [:HasPostcode] -> Postcode

I have a person in the database: 
Tom with Postcode: LA2 0RN

I have this query: 

"MATCH(p:Person), (pc:Postcode), (f:FirstName)
WHERE (p)-[:HAS_POSTCODE] -> (pc{value:'LA2 0RN'}) OR (p)-[:HAS_FIRST_NAME]->(f:FirstName {value: "Jerry"})
return DISTINCT p, pc" 

It does not return any records even if there is a match for Tom based on his postcode. How do I execute an OR statement so that I can return Tom based on his postcode. 


I believe it is due there not existing a path between Person Tom and FirstName "Jerry", so the value of 'f' never get set, so the Cartesian product in your match has not results. The following should work:

optional match (pc:Postcode{value:'LA2 0RN'})
optional match (f:FirstName{value: "Jerry"})
    exists((p)-[:HAS_POSTCODE] -> (pc)) 
    OR exists((p)-[:HAS_FIRST_NAME]->(f))
return DISTINCT p, pc

Just a note, the data model seems a little strange.  Typically, postal code and first name would be a property on the Person node. Is there a reason you have made this nodes instead?  

Node Clone

Thanks for your response. We did have them as properties but when the graph was scaled its performance degraded (despite using indexes etc). We are trying a new model where we have extracted the properties that we search against into entities. 

That is interesting that it degraded. With your new model, I am afraid you will get super nodes with all the common postal codes and first names people will have. Even if it finds the postal code or first name node quickly, it will have to traverse every relationship to collect the nodes on the other end. It's going to repeat this for each property extracted to a node. If you and all these conditions, it will have to find the intersection of all these nodes. It will be interesting to see how it performs. 

Our assumption is that it should reduce/filter the set of results if we extract values that we are searching against into entities. Preliminary findings confirm this however we will continue to test the hypotheses and keep you posted. Thank you for your assistance.