1
votes

I am using neo4j to store data with nodes having 1 of 2 labels :Person and Organization. All nodes have a property :name

All the relationships are labeled LinkedTo and have a property :score. There might be multiple relations between one pair of Person and Organization nodes

I have used path queries to search paths between these nodes like:

MATCH (n:Person) WHERE n.name =~ "(?i)person1"
MATCH (m:Organization) WHERE m.name =~ "(?i)organization1"
WITH m,n
MATCH p = (m)-[*1..4]-(n)
RETURN p ORDER BY length(p) LIMIT 10

This returns all paths (upto 10)

Now I want to find specific paths, with all relations involved having a score=1. Not sure how to achieve this, I started with MATCH p = (m)-[f*1..4]-(n) but it got a deprecation warning. So after some googling and trials and errors, I came up with this:

MATCH (n:Person) WHERE n.name =~ "(?i)person1"
MATCH (m:Organization) WHERE m.name =~ "(?i)organization1"
WITH m,n
MATCH p = (m)-[*1..4]-(n)
WITH filter(x IN relationships(p) WHERE x.score=1) AS f
ORDER BY length(p)
UNWIND f AS ff
MATCH (a)-[ff]-(b)
RETURN a,b,ff LIMIT 10

But this is not correct and not clean and is giving me relationships and nodes that are not needed in the path.

This might be a basic cypher query but I am just a beginner and need help with this. :)

1

1 Answers

1
votes

From what I have understand you are searching this query :

MATCH p = (m:Organization)-[rels*1..4]-(n:Person) 
WHERE 
    n.name =~ "(?i)person1" AND
    m.name =~ "(?i)organization1" AND
    all(r IN rels WHERE r.score=1)
RETURN p
ORDER BY length(p)

Due to all(r IN rels WHERE r.score=1), Neo4j will just expand relationships in the path that have an attribute score set to 1.

Also you should note that you are using a regex condition for the node n & m, and this operator can't use indexes ! If your goal is to have a case-insensitive search, I advise you to create a sanitize field (ex _name) to store the name in lower-case, and to replace your regex with a CONTAINS or STARTS WITH