J L
J L

Reputation: 420

How can Cypher Impose a Maximum Number of Hops Only Counting a Specific Type of Node?

I know that in Neo4J, Cypher can be used to filter results based on a maximum number of hops between two nodes, like this:

MATCH (a:Word)-[relationships*..3]-(b:Word)
RETURN a, relationships, b
LIMIT 5

This will return nodes (a and b) that are both of type Word, and that are with 4 total hops of each other (through all node types, and all relationship types).

I'm wondering whether Cypher can be made to only count specific types of nodes when it's counting to that maximum of 3 hops in the above example.

For example, in this chain of nodes:

(a:Word) ---> (b:Definition) ---> (c:Word) ---> (d:Definition) ---> (e:Definition) ---> (f:Word) ---> (g:Definition) ---> (h:Word)

There are 7 total hops between nodes a and h. However, there are only 3 Word hops between them.

Is it possible for Cypher to impose a maximum number of hops in this way?

Upvotes: 1

Views: 3803

Answers (1)

stdob--
stdob--

Reputation: 29167

You can use a filter to count label of nodes. For example:

MATCH path = (a:Word)-[relationships*..10]-(b:Word)
WHERE SIZE( FILTER(n IN NODES(path) WHERE 'Word' IN LABELS(n)) ) > 3
RETURN a, relationships, b
LIMIT 5

Upvotes: 1

Related Questions