Project_H
Project_H

Reputation: 59

Neo4j wildcard search on existing relationships?

How do I create relationships between nodes only where specific relationships specified by pattern do not already exist?

The closest I can get is:

MATCH (d1:Document),(d2:Document), (d1)-[r]-()
WHERE d1.acct  = d2.acct 
AND d1.doc_type = 'ID' AND d2.doc_type = 'PA' 
AND d1.DD_sd = d2.DD_sd
AND NOT TYPE (r) =~ ':IDRelated*.' 
CREATE (d1) -[:IDRelated_SameDD]-> (d2)

but that would not be as accurate as listing out the specific relationships to exclude.

I would like something like:

MATCH (d1:Document),(d2:Document)
WHERE d1.acct  = d2.acct 
AND d1.doc_type = 'ID' AND d2.doc_type = 'PA' 
AND d1.DD_sd = d2.DD_sd
AND NOT ( (d1) -[':IDRelated*.']- () ) 
CREATE (d1) -[:IDRelated_SameDD]-> (d2)

where I can use a regular expression (or STARTS WITH) to specify a pattern to exclude multiple relationships matching that pattern.

Thanks!

Upvotes: 1

Views: 732

Answers (1)

InverseFalcon
InverseFalcon

Reputation: 30397

At the moment there's no such way to do this inline in a pattern.

However, you may want to PROFILE your query, and compare it against a query where you save your match to d2 until you finish filtering for the correct d1 matches. Something like this:

MATCH (d1:Document)-[r]-()
WHERE d1.doc_type = 'ID' 
WITH d1, COLLECT(r) as rels
WHERE NONE(r in rels WHERE type(r) STARTS WITH 'IDRelated')
MATCH (d2:Document)
WHERE d1.acct  = d2.acct 
AND d1.DD_sd = d2.DD_sd
AND d2.doc_type = 'PA'
CREATE (d1) -[:IDRelated_SameDD]-> (d2)

Upvotes: 4

Related Questions