0
votes

I have the graph model below and am trying to write a Cypher query that finds the shortest path from a Person to a Skill that includes at least one instance of the relationship HAS_SKILL in the path.

In the model

  • a Person is connected to another Person via the IS_CONNECTED_TO relationship
  • a Person is connected to a Skill via HAS_SKILL
  • a Skill is connected to another Skill via the IS_RELATED_TO relationship

enter image description here

I have come up with the following query, which works but unwraps all the relationships in the path to check that the HAS_SKILL relationship is one of the relationships.

match (person:Person {id: "48"}), (skill:Skill {id: '10667'}),
path = shortestPath((p)-[*..30]-(s))
WHERE ANY(r in relationships(path) where type(r) = 'HAS_SKILL')
return path;

Is there a more effecient way to do this?

I have played around with relationship pattern matching in (p)-[*..30]-(s) but I couldn't get it work in the same way that ANY in the where clause works?

This is using neo4j 4.1.0.

1
Is it possible for a path between a Person and a Skill NOT TO contain a relationship of type HAS_SKILL ?? From your graph schema, it seems it's not?stellasia
that's correct here, but its only a subset of the full model. the full model does allow for alternate paths to Skills not via HAS_SKILLuser783836

1 Answers

0
votes

If the center of universe is HAS_SKILL, I'd suggest looking into anchoring on that relationship, and then path find from the relationships start/end nodes. Something like this

match (p:Person)-[:HAS_SKILL]->(s:Skill)

Then perhaps determine the shortest path from the specific person to those p, then take just the s skill's those shortest paths p point to and then shortest path find from those s to the specific skill.

I don't know if there is more we could suggest without knowing more about the actual model and challenge.