# Allow value variables in facet filters

**URL:** <https://discuss.dgraph.io/t/allow-value-variables-in-facet-filters/8751>\
**Category:** Dgraph\
**Tags:** kind:enhancement, popular, area:querylang, area:facets\
**Created:** [May 22, 2018, 10:07pm UTC](https://discuss.dgraph.io/t/allow-value-variables-in-facet-filters/8751 "2018-05-22T22:07:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [May 22, 2018, 10:07pm UTC](https://discuss.dgraph.io/t/allow-value-variables-in-facet-filters/8751/1 "2018-05-22T22:07:05Z")

</div>

**Moved from GitHub [dgraph/2406](https://github.com/dgraph-io/dgraph/issues/2406)**

_Posted by_ [djdoeslinux](https://github.com/djdoeslinux):

I would love to be able to interpolate value variables into @facet statements similarly to @filter statements. This would enable a new class of query dependent traversals for my use case and eliminate a lot of post-processing code in my application.

I will try to distill my current data model into a trivial, but relevant case tomorrow.

---

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [September 13, 2019, 10:34pm UTC](https://discuss.dgraph.io/t/allow-value-variables-in-facet-filters/8751/2 "2019-09-13T22:34:52Z")

</div>

[campoy](https://github.com/campoy) _commented_ :

Hi there, @djdoeslinux

What is exactly what you are requesting here? Do you mean having the facet name in a variable or the facet value that you want to compare the facet value with?

---

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [March 1, 2020, 2:31pm UTC](https://discuss.dgraph.io/t/allow-value-variables-in-facet-filters/8751/3 "2020-03-01T14:31:29Z")

</div>

[lucacanella](https://github.com/lucacanella) _commented_ :

I think I stumbled upon the same need.  
I created a fictional case that should describe what I’m asking, please see details below:

**Scenario**  
A classic person-rated-movie scenario, I want to find who gave the same rates to one or more movies I rated. Imagine following scenario:

```auto
(P1)-[Rated: 3].
                \
                 (Movie 1)-[Rated: 5]-(Me)
                /
(P2)-[Rated: 5]'

```

The matching pattern for my query should be: `(P2)-[Rated: 5]->(Movie 1)<-[Rated: 5]-(Me)`

**Example**  
Please find example schema, data and queries in the attachment.  
[dgraph-facets-filter-variable.txt](https://github.com/dgraph-io/dgraph/files/4271890/dgraph-facets-filter-variable.txt)

**What I tried to do**  
The query starts from node “(Me)”, then gets all rated movies with facets. Actual rates are an int facet for “rated” predicate, so for each movie I want to find all the people that rated that same movie given the value of the rate is the same that has been detected first. I tried to achieve it this way:

```auto
{
  scenario(func: type("Person")) @filter(eq(name,"Me")) @ignorereflex {
    name
    rated @facets(r as rate) {
      name
      ~rated @facets(eq(rate, val(r))) {
        name
      }
    }
  }
}

```

I obviously get this error:

```auto
Error Name: t
Message: line 9 column 24: variables are not allowed in facets filter.

```

I tried other ways, like treating ratings as nodes, but still could’nt get away with this.

Hardcoding a value, instead of using a variable with val, succedes:

```auto
{
  scenario(func: type("Person")) @filter(eq(name,"Me")) @ignorereflex {
    name
    rated @facets(rate) {
      name
      ~rated @facets(eq(rate, 5)) {
        name
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [March 1, 2020, 4:17pm UTC](https://discuss.dgraph.io/t/allow-value-variables-in-facet-filters/8751/4 "2020-03-01T16:17:17Z")

</div>

[MichelDiz](https://github.com/MichelDiz) _commented_ :

It seems reasonable to me. Value variables could come from other blocks and amplify the possibilities of querying.

```auto
{
  AB as var(func: anyofterms(name, "Alice Bob Charlie")) {
    value as math(5)
   }
  q(func: uid(AB)) {
    rated @facets @facets(eq(rating, val(value))) {
      uid
    }
  }
}

```

Update(2021): just fixed the query.

---

<div class="post-metadata">

**Author:** ![iamvibinvijay](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/iamvibinvijay/32/8609_2.png) [@iamvibinvijay](https://discuss.dgraph.io/u/iamvibinvijay)\
**Post date:** [July 22, 2021, 4:55pm UTC](https://discuss.dgraph.io/t/allow-value-variables-in-facet-filters/8751/5 "2021-07-22T16:55:01Z")

</div>

```auto
{
var(func: eq(userlist.publicId, "1010")){
userlist.userInterests @facets (priority){
id as skills.skillPublicId
}
}

```

```auto
{
my(func: eq(userlist.publicId, "1010")){
username: userlist.username
publicId: userlist.publicId
collaboration: userlist.collaboration @facets(skillPublicId: skillPublicId) @facets(eq(skillPublicId, val(id))){
username: userlist.username
publicId: userlist.publicId
}
}
}
}

```

I’m trying to find the userInterests for a given user and these userInterests in a variable named id are used to filter the facets. But I’m getting this error (please find the attached screenshots) This works if I hardcode a value.

Please help me, thanks!

 ![Screenshot (357)](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/c/ceadb579d5e0f5b3d91bad170e08f831715da884.png)
