0

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 Answer 1

1

[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.

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you for your answer. Your query works fine, but limit has some problem. 'end' nodes are limited by 10 correctly with my query, but when I tested your query, it only displays 8 nodes. I think limit clause counts not only nodes but also relationships. Do you know how can I apply limit clause only for number of nodes?
If I understand what you want, my updated answer should work.
Thank you for your kind explanation. Now I understand limit / order by clauses much better than before with your explanation. I'll try it. Thanks a lot!
As a result of your query, it limits end nodes correctly, but there is ordering problem. Actually I want to get nodes within 1 hop first in limit value, after then get 2 hop nodes. For example, if there are 10 nodes - 6 nodes in distance 1, 4 nodes in distance 2, and limit number is 8, then I want to aggregate 6 nodes in distance 1 first, and after then aggregate 2 nodes in distance 2. So I used length(path) ASC because of this requirement. Could you give me an advice to handle it?
OK, nice problem. I have updated my answer yet again, The COLLECT function seems to mess up the ordering, so the new query no longer orders before doing the COLLECT, but afterwards.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.