cancel
Showing results forΒ
Did you mean:Β

## Variable Length Relationship - intermediate nodes

Graph Voyager

Hi,

Have a question about being able to constrain the nodes included in a variable length match.

``````Match p= (a:owner)-[:Has]->(b:document)-[:Cites*1..]->(d:document)<-[:Has]-(a)
``````

In the path within the variable length relationship [:Cites], I would like to limit the nodes to also satisfy (a)-[:Has]-(intermediate node). The goal is to limit all document nodes to those that also satisfy a relationship of [:Has] with node (a:owner).
Is there a way to add that constraint to the match?
Andy

1 ACCEPTED SOLUTION
Ninja

Just as a note, you don't need the final 'has' relationship with the new constraint, since you are requiring each document node to have a 'has' relationship to 'a'. Thus, the following should provide the same result:

match p = (a:owner)-[:Has]->(:document)-[:Cites*1..]->(:document)
with p, a, [i in nodes(p) where i:document] as docNodes
where all (i in docNodes where exists((a)-[:Has]->(i)))
return p

It seems that all that is occurring here, is finding a trail of document citations among a single author's papers.

5 REPLIES 5
Node Clone

Hi Andy, it is possible to access the nodes in path p with nodes(p). That function returns a list of nodes.
However, albeit without knowing what you are trying to achieve, your approach seems overly complicated to me and this filtering will most likely not be fast.

They pattern seems to say:

1. Find owner nodes, each represented by the variable 'a' `(a:owner)`
2. Traverse from every 'a' `:document` node via the `;Has` relationship to `:document` 'b' that `:Cities` r `:document` 'd' `-[:Has]->(b:document)-[:Cites*1..]->(d:document)`
3. Where `:document` 'd' is also connected to the node represented by 'a' via a `:Has` relationship.
` `
Which I translate to: "Find every document that 'a' has, that is citied by another document that 'a' has"
` `
Is that what you're actually looking for?
Ninja

I agree with others that maybe we can come up with a more efficient approach if we know more about your requirements. In the meantime, the following query should do what you want. Sorry I don't have data to test it out.

match p = (a:owner)-[:Has]->(:document)-[:Cites*1..]->(:document)<-[:Has]-(a)
with p, a, [i in nodes(p) where i:document] as docNodes
where all (i in docNodes where exists((a)-[:Has]->(i)))
return p

Ninja

Just as a note, you don't need the final 'has' relationship with the new constraint, since you are requiring each document node to have a 'has' relationship to 'a'. Thus, the following should provide the same result:

match p = (a:owner)-[:Has]->(:document)-[:Cites*1..]->(:document)
with p, a, [i in nodes(p) where i:document] as docNodes
where all (i in docNodes where exists((a)-[:Has]->(i)))
return p

It seems that all that is occurring here, is finding a trail of document citations among a single author's papers.

Graph Voyager

Hi,

You are correct in that I am looking for the document trail and in the data set there are links from child to grandparent or even great grandparent. My goal is be able to create a view that isolates the distinct chain of direct parent0child relationships. I am tracking the specific evolution of the document or really the concepts within the document and need to isolate each step. For example what new phrases or terms were added with each generation. I also want to reduce the visual clutter in a representation of the path to the user so they can understand the changes. For my data set this analysis of the path is on a slowly growing data set and only needs to be done once for each document added so I am nodding the result to a property. In y case I add an 'inherited" boolean property to relationships that are also described by the natural evolution of the document.

An alternative approach I have also used is to create a temporary relationship that captures only documents by that specific author and craft a query based on that and then delete that relationship.

Andy

Nodes 2022

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.

Neo4j Resources