cancel
Showing results for 
Search instead for 
Did you mean: 

Variable length with intermediate node

Hi

I have a dataset that contains information about projects and project dependencies in package managers. Meaning that a specific version of a project has dependencies to other projects. The data model consists of project, which has a relation to versions, which has a dependencies to multiple projects.
(project) - [has_version] -> (version) -> [depends_on] - (project)

What I'm trying to do initially is to return a graph of a dependency tree for some specific project with a given name and version. So given a project name and a version it should find all dependencies for that project and continuously find all the dependencies for the dependency projects until the chain stops.

My problem is that all the examples of variable length doesn't have an intermediate node (versions). Like this <MATCH path = (e)<-[:ManagedBy*]-(e)
RETURN e, path/>

Is there any way to use variable length with an intermediate node?

Thanks in advance!

2 REPLIES 2

Joel
Ninja
Ninja

You can specify variable lengths on each relationship in the query if you want, so perhaps your query might look something like this (sorry for changing the case, and adding labels to the node to restrict the search, force of habit...)

// find the center node first, then find paths to left and right of it
MATCH (version:Version  {id:'2.1.1'})
MATCH path = (project1:Project)-[HAS_VERSION*]->(version)->[DEPENDS_ON*]-(project2:Project)
RETURN path

Notes

  • if you are only interested in paths, you can delete project1 and project2 variables from the cypher, since we aren't using them (in my example)
  • I would recommend bounding the path depths e.g. *1..2 (min and max hop) and I wonder if you'd want to hop through other relationship types? (the HAS_VERSION* relationship will find this type of path, a-HAS_VERSION->b-HAS_VERSION->c )

ameyasoft
Graph Maven
Try this:

MATCH (a:project)-[:has_version]->(b:version) 
WHERE a.project = "A" and b.version = 2
WITH id(a) as ID, id(b) as ID1

MATCH (c) where id(c) in [ID, ID2]
CALL apoc.path.spanningTree(c,{maxLevel:3, limit:100}) YIELD path
RETURN path]

You can select the maxLevel  and also you can add label filters, relationship filters.
Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.