Попробуйте библиотеку алгоритмов графа. https://neo4j.com/docs/graph-algorithms/current/labs-algorithms/shortest-path/
//1.
MATCH (start:node {name: '1'}), (end:node {name: '4'})
CALL algo.shortestPath.stream(start, end, 'cost', {
nodeQuery:'MATCH (n:node) RETURN id(n) as id',
relationshipQuery:'MATCH (n:node)-[r:edge]->(m:node) WHERE r.Property1="P1" and r.Property2="P2" RETURN id(n) AS source, id(m) AS target, r.cost AS weight',
graph: 'cypher', duplicateRelationships: 'min'})
YIELD nodeId, cost
RETURN algo.asNode(nodeId).name AS name, cost;
//2.
MATCH (start:node {name: '1'}), (end:node {name: '4'})
CALL algo.shortestPath.stream(start, end, 'cost', {
nodeQuery:'MATCH (n:node) RETURN id(n) as id',
relationshipQuery:'MATCH (n:node)-[r:edge]->(m:node) WHERE r.Property1="P1" RETURN id(n) AS source, id(m) AS target, r.cost AS weight',
graph: 'cypher', duplicateRelationships: 'min'})
YIELD nodeId, cost
RETURN algo.asNode(nodeId).name AS name, cost;
//3.
MATCH (start:node {name: '1'}), (end:node {name: '4'})
CALL algo.shortestPath.stream(start, end, 'cost', {
nodeQuery:'MATCH (n:node) RETURN id(n) as id',
relationshipQuery:'MATCH (n:node)-[r:edge]->(m:node) WHERE r.Property2="P2" RETURN id(n) AS source, id(m) AS target, r.cost AS weight',
graph: 'cypher', duplicateRelationships: 'min'})
YIELD nodeId, cost
RETURN algo.asNode(nodeId).name AS name, cost;
//4.
MATCH (start:node {name: '1'}), (end:node {name: '4'})
CALL algo.shortestPath.stream(start, end, 'cost', {
nodeQuery:'MATCH (n:node) RETURN id(n) as id',
relationshipQuery:'MATCH (n:node)-[r:edge]->(m:node) WHERE r.Property1="P1" or r.Property2 = "P2" RETURN id(n) AS source, id(m) AS target, r.cost AS weight',
graph: 'cypher', duplicateRelationships: 'min'})
YIELD nodeId, cost
RETURN algo.asNode(nodeId).name AS name, cost;
//5.
MATCH (start:node {name: '1'}), (end:node {name: '4'})
CALL algo.shortestPath.stream(start, end, 'cost', {
nodeQuery:'MATCH (n:node) RETURN id(n) as id',
relationshipQuery:'MATCH (n:node)-[r:edge]->(m:node) WHERE coalesce(r.Property1, "P2")<>"P1" RETURN id(n) AS source, id(m) AS target, r.cost AS weight',
graph: 'cypher', duplicateRelationships: 'min'})
YIELD nodeId, cost
RETURN algo.asNode(nodeId).name AS name, cost;