Hi.
I simply can’t figure out how to return a list sorted on nested values and hope someone can explain how it can be done.
I know https://dgraph.io/docs/tips/#sort-edge-by-nested-node-values but the example doesn’t explain it a way I understand and can translate it into my solution.
So I have create a simple schema, with some sample data. See below.
The structure is simple. A RootLevel with n number of leafs.
In my query I do a filtering to ensure I only return 1 leaf pr. RootLevel. (@Filtering in the example is not correct but it works for the demo purpose)
The result I want to return should contain RootLevel and one Leaf node and be sorted based on Leaf.identifier and the order should be:
1 ident.
2 ident.
3 indet.
4 indet.
The returned order with my query is
1, 4, 3, 2 The uid order.
Hope someone can explain my the pattern to use, so I can get it to sort as expected.
Thanks in advance.
Lasse Nedergaard
#Schema
RootLevel.attributes: [uid] .
Leaf.identifier: string @index(exact) .
Leaf.note: string @index(fulltext) .
Leaf.parent: uid .
type Leaf {
Leaf.identifier
Leaf.note
}
type RootLevel {
RootLevel.attributes
}
#Demo data.
{"set":[
{"uid":"_:objatt","Leaf.identifier":"1 ident.","Leaf.note":"My note","dgraph.type":["Leaf"]},
{"uid":"_:rootobj","dgraph.type":["RootLevel"],"RootLevel.attributes":[{"uid":"_:objatt"}]},
{"uid":"_:objatt","Leaf.parent":{"uid":"_:rootobj"}}
]
}
{"set":[
{"uid":"_:objatt","Leaf.identifier":"4 ident.","Leaf.note":"My note","dgraph.type":["Leaf"]},
{"uid":"_:rootobj","dgraph.type":["RootLevel"],"RootLevel.attributes":[{"uid":"_:objatt"}]},
{"uid":"_:objatt","Leaf.parent":{"uid":"_:rootobj"}}
]
}
{"set":[
{"uid":"_:objatt","Leaf.identifier":"3 ident.","Leaf.note":"My note","dgraph.type":["Leaf"]},
{"uid":"_:rootobj","dgraph.type":["RootLevel"],"RootLevel.attributes":[{"uid":"_:objatt"}]},
{"uid":"_:objatt","Leaf.parent":{"uid":"_:rootobj"}}
]
}
{"set":[
{"uid":"_:objatt","Leaf.identifier":"2 ident.","Leaf.note":"My note","dgraph.type":["Leaf"]},
{"uid":"_:objatt2","Leaf.identifier":"1 ident.","Leaf.note":"not matching note","dgraph.type":["Leaf"]},
{"uid":"_:rootobj","dgraph.type":["RootLevel"],"RootLevel.attributes":[{"uid":"_:objatt"}, {"uid":"_:objatt2"}]},
{"uid":"_:objatt","Leaf.parent":{"uid":"_:rootobj"}},
{"uid":"_:objatt2","Leaf.parent":{"uid":"_:rootobj"}}
]
}
#Query without sorting
{
var(func: type(Leaf)) @filter(eq(Leaf.note, "My note"))
{
rootUid as Leaf.parent
}
results(func: uid(rootUid), first:10, offset:0) @cascade(RootLevel.attributes) {
uid
RootLevel.attributes {
uid
expand(_all_)
}
}
}