1
votes

In continuation of a discussion related to a linkedlist serving as a workable array where each hop depth represents an index that can be ordered:

CYPHER store order of node relationships of the same label when I create

Cypher Linked LIst: how to unshift and replace by index

Cypher LinkedList Match by index but "Don't know how to compare that." instead

I have recently been trying to capture a report on each array that belong to a parent node. This report includes a unique relationshiptype and a count of how many elements are in the linked list array:

[
   {
      reltype    : "123A_RelationshipTitleOne",
      depthcount : 5
   }, {
      reltype    : "123A_RelationshipTitleTwo",
      depthcount : 9
   }, {
      reltype    : "123A_RelationshipTitleThree",
      depthcount : 42
   }
]

Cypher -Tally report on the depth of each relationship type stemming from parent node

We are close to a solution but the query time is very slow and the count amount is double its expected value.

On stackoverflow we have documented queries for array-like features of a linkedlist including unshift at the beginning, insert at index, replace at index, and push at the end of the "array"

I'm hoping for help on also the array length feature

I am very grateful for help you can offer.

2

2 Answers

0
votes

Something like

MATCH p = (a:Head {id:234})-[:NEXT*..20]->(b:End)
RETURN max(length(path))
0
votes

This may be what you are looking for:

MATCH (n { id: '123A' })
OPTIONAL MATCH p=(n)-[r*]->(c)
WHERE (type(r[0]) STARTS WITH '123A') AND NOT (c)-->()
RETURN n, COLLECT({ id: type(r[0]), depth: length(p)}) AS leafreport;

The NOT (c)-->() test is for ensuring that the query only collects "complete paths", not partial paths. This presumes that in this use case every linked list ends with a node that has no outgoing relationships.

Here is a console that shows the results for some sample data.

Notes:

  1. If n is not found, then no rows are returned.
  2. If n is found, but there are no initial relationships whose types start with "123A", then leafreport will be [{id:null, depth:null}].
  3. The x STARTS WITH '123A' syntax was introduced in neo4j 2.3. In prior versions, you'd have to use a less readable regexp: x =~ "123A.*".