Reputation: 59
I am a little stuck querying the contents of arrays, here is the basics of the graph
(:A)-[:Q]-(:B)-[:R]-(:C)
The initial part of the query is matching other data properties of [:R] I can do easily and return a total count of matching nodes (c) using
MATCH (a:A)-[:Q]-(b:B)-[r:R]-(c:C)
WHERE a.ID = currentID AND r.x = 1 AND r.y =3 AND r.z = 2<br/>
RETURN COUNT(c)
But a property of [:R] is an array containing IDs of other (:C) nodes, what I would like is to return (:C) descending order of occurrence.
Example result of query would be
(:A)-[:Q]-(:B)-[1:R {ids: [2,3,4]} ]-(:C {ID = 1})
(:A)-[:Q]-(:B)-[:R {ids: [7,3,4]} ]-(:C {ID = 2})
(:A)-[:Q]-(:B)-[:R {ids: [1,4]} ]-(:C {ID = 3})
(:A)-[:Q]-(:B)-[:R {ids: [1]} ]-(:C {ID = 4})……
and return this
(:C {ID = 4}) = 3
(:C {ID = 1}) = 2
(:C {ID = 3}) = 2
(:C {ID = 2}) = 1
(:C {ID = 7}) = 1
Is this at all possible or can I just get a list of the IDs and the count, then tell the application to retrieve the data using the IDs??
4 = 3
1 = 2
3 = 2
2 = 1
7 = 1
If this is not possible, do I need to remodel the graph and add an extra relationship between C’s,
(:A)-[:Q]-(b:B)-[:R]-(c1:C)-[:R2 {property = b.ID}]-(c2:C)
If so I would extend the original MATCH query to something like
MATCH (b:B)-[r:R1]-(c1:C)-[q:R2]-(c2:C)
WHERE r.x = 1 AND r.y =3 AND r.z = 2 and q.property = b.ID;
From here how would I list c2 in order of occurrence?
Upvotes: 0
Views: 69
Reputation: 1820
I don't understand why you build a this data model. Graph DB is very good for relationship between node. You should create relationship instead of create many another node property
Upvotes: 1
Reputation: 59
Ok shortly after posting this found UNWIND, and this is a working solution
MATCH (b:B)-[r:R]->(c:C)
WHERE a.ID = currentID AND r.x = 1 AND r.y =3 AND r.z = 2
UNWIND r.ids AS ids
WITH ids ORDER BY ids
MATCH (d:C) WHERE d.ID = ids
RETURN DISTINCT(d), COUNT(d)
ORDER BY count(d) DESC
Upvotes: 0