CONTAINS with boundary across multiple words in Neo4j

Viewed 47

I'm trying to do a CONTAINS with boundaries across MULTIPLE words in neo4j. Any advice?

EQUIVALENT OF:

MATCH (a)-[:REL]->(b)
WHERE b.whole CONTAINS a.part
RETURN a.part, b.whole

Would return:
"Peter","Peter Andrew Morrison"
"Andrew","Peter Andrew Morrison"
"Sarah Morgan","Sarah Morgan Wilkerson"

Would not return
"Pete","Peter Andrew Morrison"
"Pete Andrew","Peter Andrew Morrison"
"Sara","Sarah Morgan Wilkerson"

Is regex the way to go? If so, how do you add the a.part in regex? I tried

WHERE b.whole=~'.*'+a.part+'.*'

but that returns an error.

Or should I splice b.whole into a list and do b.part in list?

WITH b, a, split(b.whole, ' ') as list 
WHERE a.part in list

But this only allows one word at a time and I need multiple word matches... any thoughts?
TIA!!!

1 Answers

The keyword "contains" will also work for strings inside a string and does not need to be the whole word. Try this cypher below and it returns true.

RETURN "Peter Andrew Morrison" CONTAINS "Pete" 
true

The problem is there is no such node "a" where a.part equals "Pete". Copy below cypher and run it in your neo4j. You will see 0 row.

MATCH (a)-[:REL]->(b) 
WHERE a.part = "Pete" 
RETURN a

Thus, if node "a" for Pete does not exists or/and is not connected to b then you will not get "Pete","Peter Andrew Morrison" in the result.

Related