a-lustin
(Anton Lustin)
December 22, 2020, 3:06pm
1
Hello!
I’ve a question about how i can filter nodes by facets? Example:
Shema
<dgraph.graphql.schema>: string .
<director>: [uid] @reverse .
<fee>: [float] .
<name>: string @index(fulltext, term, trigram) .
type <dgraph.graphql> {
dgraph.graphql.schema
}
type <movie> {
name
fee
}
type <person> {
name
director
}
Mutation
upsert {
query {
var(func: eq(name, "movie1")) {
m1 as uid
}
var(func: eq(name, "movie2")) {
m2 as uid
}
var(func: eq(name, "person1")) {
p1 as uid
}
var(func: eq(name, "person2")) {
p2 as uid
}
}
mutation {
set {
uid(m1) <dgraph.type> "movie" .
uid(m1) <name> "movie1" .
uid(m1) <fee> "10" (type="BoxOffice") .
uid(m1) <fee> "5" (type="DVD") .
uid(m1) <fee> "2" (type="BlueRay") .
uid(m2) <dgraph.type> "movie" .
uid(m2) <name> "movie2" .
uid(m2) <fee> "15" (type="BoxOffice") .
uid(m2) <fee> "3" (type="DVD") .
uid(p1) <dgraph.type> "person" .
uid(p1) <name> "person1" .
uid(p1) <director> uid(m1) .
uid(p2) <dgraph.type> "person" .
uid(p2) <name> "person2" .
uid(p2) <director> uid(m2) .
}
}
}
How can I get movies with fee greater then 4 and type DVD?
How can I get persons, which is an directors of movies with fee greater then 10 in box office?
MichelDiz
(Michel Diz)
December 22, 2020, 4:05pm
2
Something like
{
q(func: type(movie)) @filter(gt(fee, 4)) @cascade {
name
fee @facets(eq(type, "DVD"))
}
}
{
Dir as var(func: type(Person)) @filter(has(director)) @cascade {
director @filter(gt(fee, 10)) {
fee @facets(eq(type, "BoxOffice"))
}
}
q(func: uid(Dir)) {
name
}
}
a-lustin
(Anton Lustin)
December 24, 2020, 1:28pm
3
@MichelDiz thank you for answer. But it didnt helped for me
Result on my dataset:
{
"data": {
"q": [
{
"name": "movie1",
"fee|type": {
"0": "DVD"
},
"fee": [
5
]
},
{
"name": "movie2",
"fee|type": {
"0": "DVD"
},
"fee": [
3
]
}
]
}
}
MichelDiz
(Michel Diz)
December 24, 2020, 1:35pm
4
Odd, pls share the query too.
Your dataset is a bit off. This above, won’t work. Each edge holds a unique value and its facets. When you repeat the edge in a mutation, only the last (in this case the uid(m1) <fee> "2" (type="BlueRay")
) will be recorded and the others discarted.
The only way to make this works is doing extra edges.
uid(m1) <fee-1> "10" (type="BoxOffice") .
uid(m1) <fee-2> "5" (type="DVD") .
uid(m1) <fee-3> "2" (type="BlueRay") .
a-lustin
(Anton Lustin)
December 24, 2020, 1:37pm
5
Request:
{
q(func: type(movie)) {
name
fee @facets
}
}
Response:
{
"data": {
"q": [
{
"name": "movie1",
"fee|type": {
"0": "BlueRay",
"1": "BoxOffice",
"2": "DVD"
},
"fee": [
2,
10,
5
]
},
{
"name": "movie2",
"fee|type": {
"0": "BoxOffice",
"1": "DVD"
},
"fee": [
15,
3
]
}
]
}
}
MichelDiz
(Michel Diz)
December 24, 2020, 1:41pm
6
Oh, it is a list… hum, yeah I’m not sure if it will work as inequality was created with edges in mind. Not list type values. I’ll check this.
But, in part the filtering should work. Just the result data won’t be as expected, but for sure they are filtered.
MichelDiz
(Michel Diz)
December 24, 2020, 1:48pm
7
Odd, in my end works fine.
{
Dir as varg(func: type(person)) @filter(has(director)) @cascade {
uid
director @filter(gt(fee, 10))
{
uid
fee @facets(eq(type, "BoxOffice")) @facets
}
}
q(func: uid(Dir)) {
name
}
}
Result
{
"data": {
"varg": [
{
"uid": "0x4e23",
"director": [
{
"uid": "0x4e21",
"fee|type": {
"0": "BoxOffice"
},
"fee": [
"10"
]
}
]
},
{
"uid": "0x4e24",
"director": [
{
"uid": "0x4e22",
"fee|type": {
"0": "BoxOffice"
},
"fee": [
"15"
]
}
]
}
],
"q": [
{
"name": "person1"
},
{
"name": "person2"
}
]
}
}