0
votes

I want to make a cypher query that do below tasks:

  1. there is a given start node, and I want to get all related nodes in 2 hops
  2. sort queried nodes by hops asc, and limit it with given number
  3. and get all relations between result of 1.

I tried tons of queries, and I made below query for step 1, 2

MATCH path=((start {eid:12018})-[r:REAL_CALL*1..2]-(end))
WITH start, end, path
ORDER BY length(path) ASC
RETURN start, collect(distinct end)[..10]

But when I try to get relationships in queried path with below query, it returns all relationships in the path :

MATCH path=((start {eid:12018})-[r:REAL_CALL*1..2]-(end))
WITH start, end, path
ORDER BY length(path) ASC
RETURN start, collect(distinct end)[..10], relationships(path)

I think I have to match again with result of first match instead of get relationships from path directly, but all of my attempts have failed.

How can I get all relationships between queried nodes? Any helps appreciate, thanks a lot.

1

1 Answers

1
votes

[EDITED]

Something like this may work for you:

MATCH (start {eid:12018})-[rels:REAL_CALL*..2]-(end)
RETURN start, end, COLLECT(rels) AS rels_collection
ORDER BY
  REDUCE(s = 2, rs in rels_collection | CASE WHEN SIZE(rs) < s THEN SIZE(rs) ELSE s END)
LIMIT 10;

The COLLECT aggregation function will generate a collection (of relationship collections) for each distinct start/end pair. The LIMIT clause limits the returned results to the first 10 start/end pairs, based on the ORDER BY clause. The ORDER BY clause uses REDCUE to calculate the minimum size of each path to a given end node.