How to query Graph in recursive way? #2998
-
Given that I have a person table. And I insert thousands of record:
Then I make the relation like:
Since the length of knows is arbitrary. I could not know how many levels there shall be.
Can anyone share how to deal with this user case? |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 1 reply
-
No one? |
Beta Was this translation helpful? Give feedback.
-
Recursive queries are a requirement for my use case, is there no way to do them ? |
Beta Was this translation helpful? Give feedback.
-
There are only Record Links, which in my opinion are quite enough to work with. And they work, even if queried multiple times. Based on the CREATEs from the documentation you are able to do:
I dont see any use case for real recursive queries without having cursor support. Or you may provide one? |
Beta Was this translation helpful? Give feedback.
-
Leta say I have departments. And every department can have a sub-department, related in a MANAGED_BY relation. And it goes all the way from the lowest department and up to the CEO office in an hierarchical order.
I now want to draw the path from the lowest department to the CEO via MANAGED_BY. yet I don't know how many level would be. In one branch of the tree it's three, yet in another branch it's 20... How can you draw this path only by keeping surreal db busy, and not the server's CPU?
Get Outlook for Android<https://aka.ms/AAb9ysg>
…________________________________
From: dkr8toha ***@***.***>
Sent: Tuesday, May 14, 2024 12:36:16 AM
To: surrealdb/surrealdb ***@***.***>
Cc: Bokitza ***@***.***>; Comment ***@***.***>
Subject: Re: [surrealdb/surrealdb] How to query Graph in recursive way? (Discussion #2998)
There are only Record Links<https://surrealdb.com/docs/surrealdb/surrealql/datamodel/records>, which in my opinion are quite enough to work with. And they work, even if queried multiple times. Based on the CREATEs from the documentation you are able to do:
SELECT * FROM person WHERE name = "Tobie" FETCH friends
,friends.friends
,friends.friends.friends
,friends.friends.friends.friends
,friends.friends.friends.friends.friends
I dont see any use case for real recursive queries without having cursor support.
Or you may provide one?
—
Reply to this email directly, view it on GitHub<#2998 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AN3UDNWKW52RMILD6LV7F73ZCEW5BAVCNFSM6AAAAAA7RYXVHCVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TIMRWGIZTI>.
You are receiving this because you commented.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Currently this is the only possible way i think:
The following query will work:
but must be extended (currently supports a depth of 5)... will result in:
This works but well is a bit ugly :-) I agree that there should be some query operator to recursively collect and expand the item like:
something like this:
Ye i know, this is not the nested set you want to have but i personally hate the nesting ...... |
Beta Was this translation helpful? Give feedback.
-
should be
or
to define a maximum depth ..... |
Beta Was this translation helpful? Give feedback.
-
I have a similar use case, one table that can link back on itself. A really nice way IMO to write the query would be to introduce an
|
Beta Was this translation helpful? Give feedback.
-
Recursive queries have been added with version 2.1! 🚀 Here are the main pages in the documentation that have it: https://surrealdb.com/docs/surrealql/datamodel/idioms#recursive-paths https://surrealdb.com/docs/surrealql/statements/relate#recursive-graph-queries As well as Chapter 8 of the book. https://surrealdb.com/learn/book/chapter-08#longer-relational-queries |
Beta Was this translation helpful? Give feedback.
-
New graph traversal functionality is great! i read documentaion but still have some questions. Is there a way to sort and/or filter results on each itetation? can new graph traversal feature be used in delete or update queries? |
Beta Was this translation helpful? Give feedback.
-
Is there a way to select all columns with a star? person:tobie.{..4}.{ *, connections: ->knows->person.@ }; |
Beta Was this translation helpful? Give feedback.
Recursive queries have been added with version 2.1! 🚀 Here are the main pages in the documentation that have it:
https://surrealdb.com/docs/surrealql/datamodel/idioms#recursive-paths
https://surrealdb.com/docs/surrealql/statements/relate#recursive-graph-queries
As well as Chapter 8 of the book.
https://surrealdb.com/learn/book/chapter-08#longer-relational-queries