Does Dgraph support sub-graph query?
For example,I only get part of the information in the graph.There are a lot of nodes I don’t need in the result of my query. I just need to get the nodes I need.
Graph like this
But there are so many nodes, I just want a subgraph like below
and this
How can I do that?
Hi @Soultrans,
As I understand looking at the diagram, you want to see only those nodes from level 2 which have at least one outgoing edge.
If that is the case you can use filter
in the query.
Suppose every node has predicate name
and the outgoing edges are represented by isconnectedto
. You can use something like this:
{
find_subgraph(func: uid(<root_uid>)) {
name
isconnectedto @filter(gt(count(isconnectedto), 0)){
name
isconnectedto {
name
}
}
}
}
Note that <root_uid> will have to be replaced by uid of node1.
Simple Example using filter: https://dgraph.io/docs/tutorial-3/
I’m sorry,maybe I didn’t make it clear.
For example,I get a diagram,but I just want an infographic with blue nodes.
{
all(func: eq(department, "principal","sdirector","aleader","teacherA","student")) @recurse(depth: 3) {
department
subordinate_departments
}
}
The results of my query are shown below
I want something like this:
Or two pictures like this:
If you know your start and end IDs you can try using shortest()
But I don’t know that
Now, what I’m going to get is the relationship between the incoming word information
I just realized that the path is quite arbitrary, so shortest
won’t work. What is the blue dots? Are they of different types?
I don’t know how to explain it, it looks like the node of the query, I’m not sure.
For example:
{
all(func: eq(department, “principal”,“sdirector”,“aleader”,“teacherA”,“student”)) @recurse(depth: 3) {
department
subordinate_departments
}
}
The predicates of my query are blue in the query result, “principal” is node1
Looks like you want a Connected Components thing.
perhaps you can add this:
{
all(func: eq(department, “principal”,“sdirector”,“aleader”,“teacherA”,“student”)) @recurse(depth: 3) {
department
subordinate_departments @filter(has(subordinate_departments))
}
}
this is a variant of @rajas’ answer above
This is a little off from what I expected
All I want is the information that contains the blue nodes.They are part of the whole.
Like this,I just want the red part
Sorry, I meant:
{
all(func: eq(department, “principal”,“sdirector”,“aleader”,“teacherA”,“student”)) @recurse(depth: 3) {
department
subordinate_departments {
department @filter(has(subordinate_departments))
}
}
}
can you also share what the result would be like?
That doesn’t seem right.
Ah, then my DQL-fu ain’t strong enough
When using recurse, you don’t have to specify the nested ones. Just any existing edge/pred.
So the query looks like this:
{
all(func: eq(department, “principal”,“sdirector”,“aleader”,“teacherA”,“student”)) @recurse(depth: 3) {
department
subordinate_departments
department @filter(has(subordinate_departments))
}
}
Also, to make this query even more performative I would do like:
{
A as var(func: eq(department, “principal”))
B as var(func: eq(department, “sdirector”))
C as var(func: eq(department, “aleader”))
D as var(func: eq(department, “teacherA”))
E as var(func: eq(department, “student”))
all(func: uid(A, B, C, D, E)) @recurse(depth: 3) {
department
subordinate_departments
department @filter(has(subordinate_departments))
}
}