# Getting the current depth in recurse query

**URL:** <https://discuss.dgraph.io/t/getting-the-current-depth-in-recurse-query/6157>\
**Category:** Dgraph\
**Created:** [March 19, 2020, 5:39pm UTC](https://discuss.dgraph.io/t/getting-the-current-depth-in-recurse-query/6157 "2020-03-19T17:39:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![karthik\_cru](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@karthik\_cru](https://discuss.dgraph.io/u/karthik_cru)\
**Post date:** [March 19, 2020, 5:39pm UTC](https://discuss.dgraph.io/t/getting-the-current-depth-in-recurse-query/6157/1 "2020-03-19T17:39:35Z")

</div>

I am trying to get the hops that are needed to reach from node a to node b in a recursive fashion

I know that this is possible to achieve using the shortest path function, but what would be even better is to get the current depth inside a recurse function.

```auto
{
  tree(func: eq(name, leaf)) @recurse {
    name as uid
    ~next {}
    #current_depth
  }
  findtree(func: uid(name)) @filter(eq(dgraph.type, Tree)) {
    name
  }
}

```

By this way I will be getting the depth at which the Tree is found as well. This tree in this case is root node, but it can be some intermediate node as well based on filters.

Is there a better way of getting the depth or steps from the leaf to the tree in this case.

---

<div class="post-metadata">

**Author:** ![karthik\_cru](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@karthik\_cru](https://discuss.dgraph.io/u/karthik_cru)\
**Post date:** [March 19, 2020, 5:40pm UTC](https://discuss.dgraph.io/t/getting-the-current-depth-in-recurse-query/6157/2 "2020-03-19T17:40:55Z")

</div>

There are different kinds of nodes before reaching tree in this case, so using a second function to filter them, else a filter could be added on ~next

---

<div class="post-metadata">

**Author:** ![karthik\_cru](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@karthik\_cru](https://discuss.dgraph.io/u/karthik_cru)\
**Post date:** [March 26, 2020, 6:30am UTC](https://discuss.dgraph.io/t/getting-the-current-depth-in-recurse-query/6157/3 "2020-03-26T06:30:27Z")

</div>

Just let me know if this even possible? Is there an alternate way to do the same ? I tried writing a function for shortest path, but it accepts only single uids, not lists, what if I want to get the shortest distance between multiple node pairs

Distance between [a,b,c] \* [x,y,z]. Distance or shortest path between the cross product of the lists

---

<div class="post-metadata">

**Author:** ![karthik\_cru](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@karthik\_cru](https://discuss.dgraph.io/u/karthik_cru)\
**Post date:** [March 26, 2020, 7:13am UTC](https://discuss.dgraph.io/t/getting-the-current-depth-in-recurse-query/6157/4 "2020-03-26T07:13:25Z")

</div>

I saw a similar question getting closed without a response, idle for 30 days, I will keep this ticking for 45 days, then leave it alone .

---

<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:** [March 26, 2020, 6:18pm UTC](https://discuss.dgraph.io/t/getting-the-current-depth-in-recurse-query/6157/5 "2020-03-26T18:18:35Z")

</div>

> [@karthik\_cru](#):
>
> but what would be even better is to get the current depth inside a recurse function.

I didn’t get what you are doing. By “get the current depth” do you mean that the query returns a depth value?

You can try

```auto
findtree(func: uid(name)) @filter(eq(dgraph.type, Tree)) {
count(uid)
}

```

BTW, you don’t need to use {} in the query body.

---

<div class="post-metadata">

**Author:** ![karthik\_cru](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@karthik\_cru](https://discuss.dgraph.io/u/karthik_cru)\
**Post date:** [March 26, 2020, 9:54pm UTC](https://discuss.dgraph.io/t/getting-the-current-depth-in-recurse-query/6157/6 "2020-03-26T21:54:56Z")

</div>

By current depth I meant the total number of hops (distance) needed to reach a particular parent node.

Which is basically the depth value in the recurse query. Internally depth value will be used in the recursive function. Just wanted to know of that value is available as a variable in Graphql

---

<div class="post-metadata">

**Author:** ![karthik\_cru](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@karthik\_cru](https://discuss.dgraph.io/u/karthik_cru)\
**Post date:** [March 26, 2020, 10:01pm UTC](https://discuss.dgraph.io/t/getting-the-current-depth-in-recurse-query/6157/7 "2020-03-26T22:01:57Z")

</div>

An equivalent cypher query would look like this.

```auto
MATCH p=(d:Tree)-[*]->(c:Code) 
WHERE c.value IN {Codes} WITH d, length(p) as distance 
RETURN ID(d) as id, d.name as name, distance

```

It would be great if I can find an equivalent query in Dgraph.

The thing which I wanted to know is how to get the distance value as got in the cypher query

---

<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:** [March 26, 2020, 10:36pm UTC](https://discuss.dgraph.io/t/getting-the-current-depth-in-recurse-query/6157/8 "2020-03-26T22:36:29Z")

</div>

I am not an expert on Cypher. But are you sure this is a recurse query?

It seems to me to be a query that says “Match any Tree entity that has any edge for Code entity”. This does not seem to me to be a recursive query itself in the sense of traverse. I don’t know how Cypher does recurse query tho.

Consider this query below:

```auto
{
  var(func: uid(T)){
    g as count(uid)
  }
  stats(){
    F as TotalTraversed : sum(val(g))
    Hops: math(F-1)
  }
  q(func: eq(name, "Root")) @recurse {
    T as uid
    name
    next
  }
}

```

You should get a response like so:

```auto
{
    "stats": [
      {
        "TotalTraversed": 5
      },
      {
        "Hops": 4
      }
    ]
...
 }

```

## This is the Sample

```auto
{
   "set": [
      {
         "name": "Root",
         "next": [
            {
               "name": "level_1",
               "next": [
                  {
                     "name": "level_2",
                     "next": [
                        {
                           "name": "level_3",
                           "next": [
                              {
                                 "name": "level_4"
                              }
                           ]
                        }
                     ]
                  }
               ]
            }
         ]
      }
   ]
}

```

---

<div class="post-metadata">

**Author:** ![karthik\_cru](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@karthik\_cru](https://discuss.dgraph.io/u/karthik_cru)\
**Post date:** [March 27, 2020, 3:59am UTC](https://discuss.dgraph.io/t/getting-the-current-depth-in-recurse-query/6157/9 "2020-03-27T03:59:25Z")

</div>

This helps , will try it out and let you know
