# Why do the variables in these synonym(I think) queries return two different results?

**URL:** https://discuss.dgraph.io/t/why-do-the-variables-in-these-synonym-i-think-queries-return-two-different-results/3776
**Category:** Dgraph
**Tags:** example
**Created:** [December 17, 2018, 2:33pm UTC](https://discuss.dgraph.io/t/why-do-the-variables-in-these-synonym-i-think-queries-return-two-different-results/3776 "2018-12-17T14:33:27Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Matt](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/matt/32/1397_2.png) [@Matt](https://discuss.dgraph.io/u/Matt)
#### Post date: [December 17, 2018, 2:33pm UTC](https://discuss.dgraph.io/t/why-do-the-variables-in-these-synonym-i-think-queries-return-two-different-results/3776/1 "2018-12-17T14:33:27Z")

</div>

Hello,  
I have two versions of a query, and I feel like they should return the same result. They don’t – and I hope someone can explain why.

query version 1:

```auto
    {
        var(func: eq(xids, "test@email.com")) {
            A as ~audit.entity {
                expand(_all_)
            }
        } 
        
        res(func: eq(xids, "test@email.com")) {
            xids
            phones @facets(confidence) {
                phone.number
                phone.ext
                count(~audit.attribute @filter(uid(A)))
            }
        }
    }

```

result:

```auto
{'res': [{'phones': [{'count(~audit.attribute)': 3,
                      'phone.ext': 456,
                      'phone.number': '123-555-5555',
                      'phones|confidence': 85}],
          'xids': ['03D42343FMTDFM', 'test@email.com']}]}

```

query version 2:

```auto
    {
        res(func: eq(xids, "test@email.com")) {
            xids
            A as ~audit.entity
            phones @facets(confidence) {
                phone.number
                phone.ext
                count(~audit.attribute @filter(uid(A)))
            }
        }
    }

```

result:

```auto
{'res': [{'phones': [{'count(~audit.attribute)': 0,
                      'phone.ext': 456,
                      'phone.number': '123-555-5555',
                      'phones|confidence': 85}],
          'xids': ['03D42343FMTDFM', 'test@email.com']}]}

```

Notice `'count(~audit.attribute)'` in the first result is 3, but in the second it’s 0. They use the exact same node selection statement, shouldn’t they return the same node list for `~audit.entity`?

The goal of this query is as follows:  
“Get me all of B that points to A then the count of C that points to B and also points to A”

I’m very much new to dgraph, so if there’s a way to do this more efficiently, I’d love to hear it. But either way, I’m interested in why these two queries give different results.

Thanks!

---

<div class="post-metadata">

### Author: ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)
#### Post date: [December 17, 2018, 2:43pm UTC](https://discuss.dgraph.io/t/why-do-the-variables-in-these-synonym-i-think-queries-return-two-different-results/3776/2 "2018-12-17T14:43:27Z")

</div>

I believe it is cus you have not expanded audit.entity - Try:

```auto
{
        res(func: eq(xids, "test@email.com")) {
            xids
            A as ~audit.entity {
                expand(_all_)
            }
            phones @facets(confidence) {
                phone.number
                phone.ext
                count(~audit.attribute @filter(uid(A)))
            }
        }
    }

```

---

<div class="post-metadata">

### Author: ![Matt](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/matt/32/1397_2.png) [@Matt](https://discuss.dgraph.io/u/Matt)
#### Post date: [December 17, 2018, 2:55pm UTC](https://discuss.dgraph.io/t/why-do-the-variables-in-these-synonym-i-think-queries-return-two-different-results/3776/3 "2018-12-17T14:55:24Z")

</div>

Hi, when I try that, it shows me the result of that predicate, but the count is still 0

```auto
{'res': [{'phones': [{'count(~audit.attribute)': 0,
                      'phone.ext': 456,
                      'phone.number': '123-555-5555',
                      'phones|confidence': 85}],
          'xids': ['03D42343FMTDFM', 'test@email.com'],
          '~audit.entity': [{'created_at': '2018-12-13T12:00:00Z'},
                            {'created_at': '2018-12-13T12:00:00Z'},
                            {'created_at': '2018-12-13T12:00:00Z'}]}]}

```

Which makes it even more strange, because I can now clearly see there are 3 nodes to count…

I also tested it the other way. I tried this version where I use the var block but don’t expand it

```auto
    {
        var(func: eq(xids, "test@email.com")) {
            A as ~audit.entity
        } 
        
        res(func: eq(xids, "test@email.com")) {
            xids
            phones @facets(confidence) {
                phone.number
                phone.ext
                count(~audit.attribute @filter(uid(A)))
            }
        }
    }

```

The result is exactly the same, with the proper count:

```auto
{'res': [{'phones': [{'count(~audit.attribute)': 3,
                      'phone.ext': 456,
                      'phone.number': '123-555-5555',
                      'phones|confidence': 85}],
          'xids': ['03D42343FMTDFM', 'test@email.com']}]}

```

---

<div class="post-metadata">

### Author: ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)
#### Post date: [December 17, 2018, 3:25pm UTC](https://discuss.dgraph.io/t/why-do-the-variables-in-these-synonym-i-think-queries-return-two-different-results/3776/4 "2018-12-17T15:25:22Z")

</div>

Wait, audit.entity is related to “`test@email.com`” or phones of “`test@email.com`” - This seems diff levels.

---

<div class="post-metadata">

### Author: ![Matt](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/matt/32/1397_2.png) [@Matt](https://discuss.dgraph.io/u/Matt)
#### Post date: [December 17, 2018, 3:27pm UTC](https://discuss.dgraph.io/t/why-do-the-variables-in-these-synonym-i-think-queries-return-two-different-results/3776/5 "2018-12-17T15:27:09Z")

</div>

Ok, so i’m adding a better example of the issue in one query and with much more context:

```auto
    {
        tmp(func: eq(xids, "test@email.com")) {
            uids_in_B: B as ~audit.entity {
                uid
            }
        } 

        res(func: eq(xids, "test@email.com")) {
            uid
            ids
            uids_in_A: A as ~audit.entity {
                uid
            }
            phones @facets(confidence) {
                uid
                phone.number
                phone.ext
                count_of_A_with_filter: count(~audit.attribute @filter(uid(A)))
                count_of_B_with_filter: count(~audit.attribute @filter(uid(B)))
                count_no_filter: count(~audit.attribute)
                uids_in_count_before_filter: ~audit.attribute {
                    uid
                }
            }
        }
    }

```

Result:

```auto
{'res': [{'phones': [{'count_no_filter': 4,
                      'count_of_A_with_filter': 0,
                      'count_of_B_with_filter': 3,
                      'phone.ext': 456,
                      'phone.number': '123-555-5555',
                      'phones|confidence': 85,
                      'uid': '0xc4f0',
                      'uids_in_count_before_filter': [{'uid': '0xc4f1'},
                                                      {'uid': '0xc4f2'},
                                                      {'uid': '0xc4f5'},
                                                      {'uid': '0xc4f6'}]}],
          'uid': '0xc4ef',
          'uids_in_A': [{'uid': '0xc4f1'},
                        {'uid': '0xc4f2'},
                        {'uid': '0xc4f5'}]}],
 'tmp': [{'uids_in_B': [{'uid': '0xc4f1'},
                        {'uid': '0xc4f2'},
                        {'uid': '0xc4f5'}]}]}

```

So the question is, why is `count_of_A_with_filter` different than `count_of_B_with_filter`

---

<div class="post-metadata">

### Author: ![Matt](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/matt/32/1397_2.png) [@Matt](https://discuss.dgraph.io/u/Matt)
#### Post date: [December 17, 2018, 3:40pm UTC](https://discuss.dgraph.io/t/why-do-the-variables-in-these-synonym-i-think-queries-return-two-different-results/3776/6 "2018-12-17T15:40:24Z")

</div>

Yes, it is different levels, but those levels “point” to the same set of nodes, which is what I’m attempting to count. Sorry, i’m not very familiar with the terminology yet.

A -(phones)-\> B -(~audit.attribute)-\> C -(audit.entity)-\> A

I’m trying to get the count of C, that have edges with B and A, as a predicate of B

---

<div class="post-metadata">

### Author: ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)
#### Post date: [December 17, 2018, 3:45pm UTC](https://discuss.dgraph.io/t/why-do-the-variables-in-these-synonym-i-think-queries-return-two-different-results/3776/7 "2018-12-17T15:45:08Z")

</div>

Okay, It seems should work indeed. But I’m not sure. I’ll ask @gus for solve this doubt; But I feel it don’t for some reason.

---

<div class="post-metadata">

### Author: ![gus](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/gus/32/1259_2.png) [@gus](https://discuss.dgraph.io/u/gus)
#### Post date: [December 17, 2018, 6:35pm UTC](https://discuss.dgraph.io/t/why-do-the-variables-in-these-synonym-i-think-queries-return-two-different-results/3776/8 "2018-12-17T18:35:50Z")

</div>

This looks like it could be a bug. Which version are you using? Do mind testing this using master? Thanks

---

<div class="post-metadata">

### Author: ![Matt](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/matt/32/1397_2.png) [@Matt](https://discuss.dgraph.io/u/Matt)
#### Post date: [December 17, 2018, 7:29pm UTC](https://discuss.dgraph.io/t/why-do-the-variables-in-these-synonym-i-think-queries-return-two-different-results/3776/9 "2018-12-17T19:29:02Z")

</div>

I was running 1.0.8 (i’m using docker-compose, which pulled latest at the time), but I have now run it using master and I get the same result.
