# Friends of Friends n hops away

**URL:** <https://discuss.dgraph.io/t/friends-of-friends-n-hops-away/2471>\
**Category:** Dgraph\
**Created:** [April 16, 2018, 9:31pm UTC](https://discuss.dgraph.io/t/friends-of-friends-n-hops-away/2471 "2018-04-16T21:31:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![stankadalekova](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@stankadalekova](https://discuss.dgraph.io/u/stankadalekova)\
**Post date:** [April 16, 2018, 9:31pm UTC](https://discuss.dgraph.io/t/friends-of-friends-n-hops-away/2471/1 "2018-04-16T21:31:56Z")

</div>

Hi,

is there a way to take all nodes n hops away. For example

```auto
 {
  set {
   _:a <name> "a" .
   _:b <name> "b" .
   _:c <name> "c" .
   _:d <name> "d" .
   _:e <name> "e" .

   _:a <child> _:b .
   _:b <child> _:c .
   _:c <child> _:d .
   _:d <child> _:e .
   
   _:a <type> "astnode" .
   _:b <type> "astnode" .
   _:c <type> "astnode" .
   _:d <type> "astnode" .
   _:e <type> "astnode" .

   _:a <kind> "file" .
   _:b <kind> "class" .
   _:c <kind> "func" .
   _:d <kind> "if" .
   _:e <kind> "var" .
  }
}

```

For example 2 hops from c we have a and e.  
How can I acheive this? Is it with a recursive query? Can you show me how?  
I have gone trough some posts, for example [Recursive Queries](http://discuss.hypermode.com/t/recursive-queries/1306)  
But I cannot get a working example(recursive query does not compile), can you please help?

Thanks,  
Stanka

---

<div class="post-metadata">

**Author:** ![pawan](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/pawan/32/1946_2.png) [@pawan](https://discuss.dgraph.io/u/pawan)\
**Post date:** [April 17, 2018, 6:08am UTC](https://discuss.dgraph.io/t/friends-of-friends-n-hops-away/2471/2 "2018-04-17T06:08:57Z")

</div>

For two hops, you can just do this. Note, edges are not bidirectional in Dgraph so this won’t give `a`, only `e`.

```auto
{
  me(func: uid(c)) {
    child {
      child {
        name
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![stankadalekova](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@stankadalekova](https://discuss.dgraph.io/u/stankadalekova)\
**Post date:** [April 17, 2018, 9:21pm UTC](https://discuss.dgraph.io/t/friends-of-friends-n-hops-away/2471/3 "2018-04-17T21:21:58Z")

</div>

When I execute the upper statement I got  
“Some variables are used but not defined Defined: Used:[c]”

---

<div class="post-metadata">

**Author:** ![pawan](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/pawan/32/1946_2.png) [@pawan](https://discuss.dgraph.io/u/pawan)\
**Post date:** [April 17, 2018, 11:46pm UTC](https://discuss.dgraph.io/t/friends-of-friends-n-hops-away/2471/4 "2018-04-17T23:46:32Z")

</div>

You have to use the unique id for `c`. Sorry, I didn’t make that clear. Another way is to search by name.

```auto
{
  me(func: eq(name, "c")) {
    child {
      child {
        name
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![stankadalekova](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@stankadalekova](https://discuss.dgraph.io/u/stankadalekova)\
**Post date:** [April 20, 2018, 5:09pm UTC](https://discuss.dgraph.io/t/friends-of-friends-n-hops-away/2471/5 "2018-04-20T17:09:28Z")

</div>

Thank you very much, this works as expected! I have tree more questions:  
1)Can I get C parent easily? For example: get all c direct descendants and ascendants, i.e. can I go back?  
2)Is there a special property for node type? For example if I have two types of nodes, one for movies and one for directors, how to take all movies?  
3)Can I take first 10 nodes, BUT without filter

---

<div class="post-metadata">

**Author:** ![stankadalekova](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@stankadalekova](https://discuss.dgraph.io/u/stankadalekova)\
**Post date:** [April 24, 2018, 10:50am UTC](https://discuss.dgraph.io/t/friends-of-friends-n-hops-away/2471/6 "2018-04-24T10:50:25Z")

</div>

Should I post these questions as a separete post? Thank you, Stanka.

---

<div class="post-metadata">

**Author:** ![pawan](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/pawan/32/1946_2.png) [@pawan](https://discuss.dgraph.io/u/pawan)\
**Post date:** [April 26, 2018, 12:03am UTC](https://discuss.dgraph.io/t/friends-of-friends-n-hops-away/2471/7 "2018-04-26T00:03:41Z")

</div>

All these questions could have been answered by doing a little more research as they have been asked before.

> [@stankadalekova](#):
>
> 1. Can I get C parent easily? For example: get all c direct descendants and ascendants, i.e. can I go back?

Sure, look at reverse edges.

> [@stankadalekova](#):
>
> 1. Is there a special property for node type? For example if I have two types of nodes, one for movies and one for directors, how to take all movies?

Dgraph doesn’t assign any labels to nodes by default. you have to associate a property for the type of nodes. Then you can do a `has` query. See [Get started with Dgraph](https://docs.dgraph.io/howto/#giving-nodes-a-type)

> [@stankadalekova](#):
>
> 1. Can I take first 10 nodes, BUT without filter

You have to apply a function at root to filter the initial set of nodes. Then you can take the first 10 or first n.

---

<div class="post-metadata">

**Author:** ![srinathganesh](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/srinathganesh/32/896_2.png) [@srinathganesh](https://discuss.dgraph.io/u/srinathganesh)\
**Post date:** [June 28, 2018, 5:50am UTC](https://discuss.dgraph.io/t/friends-of-friends-n-hops-away/2471/8 "2018-06-28T05:50:18Z")

</div>

> [@pawan](#):
>
> edges are not bidirectional in Dgraph

so is there any way to ‘search’ bi-directionally?

---

<div class="post-metadata">

**Author:** ![ahopkins](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/ahopkins/32/1195_2.png) [@ahopkins](https://discuss.dgraph.io/u/ahopkins)\
**Post date:** [July 31, 2018, 5:25pm UTC](https://discuss.dgraph.io/t/friends-of-friends-n-hops-away/2471/9 "2018-07-31T17:25:49Z")

</div>

For anyone that ends up here looking for a similar answer, take a look at my related question here: [Flattening to get unique nodes within n steps](http://discuss.hypermode.com/t/flattening-to-get-unique-nodes-within-n-steps/2969)
