Search code examples
neo4jcypherneo4j-apoc

How to exclude/blacklist relationship types from apoc.path.expandConfig or similar?


So I am trying to find paths between a pair of nodes, of different types. The code is shown below. The graph is pretty large and have many different types of relationships, some of which I would like to exclude/blacklisted from the path, since they are extremely redundant/uninteresting.

From what I can see apoc.path.expandConfig does not allow for that natively. Is there are a way I can achieve this?

MATCH (sourceNode:SourceLabel {symbol: "<source node>"})
MATCH (targetNode:TargetLabel {name:"<target node>"})
CALL apoc.path.expandConfig(g, {
    labelFilter: "IntermediaryLabel1, IL2a|IL2b|IL2c, >TargetLabel",
    minLevel: 1,
    maxLevel: 3,
    terminatorNodes: [targetNode],
    uniqueness: "NODE_PATH",
    limit: 50
})
YIELD path
RETURN path, length(path) AS hops
ORDER BY hops

Solution

  • I've run into this but never sat down to solve it until today when I saw your question. If you have a LOT of relationships like I do, I feel your pain. Ideally they will eventually add a subtract for rels, but for now it is not hard to DIY.

    Steps

    1. get the list of all relationships
    2. subtract the ones you don't want
    3. format for use in the apoc call
    // get all relationships
    CALL db.relationshipTypes() yield relationshipType WITH collect(relationshipType) as rt
    // remove unwanted relationships
    WITH [n IN rt WHERE NOT n IN ['PERTURBS','CAUSES_SIDE_EFFECT','TREATS','REPURPOSED_INDICATION','SUBSET_OF','GENE_EC','GENE_GENE']] as rt2
    // format for use in apoc call
    WITH REDUCE(ms = "",word IN rt2 | ms+word+'|') as js
    // remove trailing pipe
    WITH LEFT(js, SIZE(js)-1) as relationshipWhiteList
    // your cypher here (and use the variable relationshipWhiteList)
    

    References used to piece this together