# Paths throw several nodes

**URL:** <https://discuss.dgraph.io/t/paths-throw-several-nodes/6271>\
**Category:** Dgraph\
**Created:** [April 7, 2020, 7:19am UTC](https://discuss.dgraph.io/t/paths-throw-several-nodes/6271 "2020-04-07T07:19:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![g.v.pol](https://avatars.discourse-cdn.com/v4/letter/g/6bbea6/32.png) [@g.v.pol](https://discuss.dgraph.io/u/g.v.pol)\
**Post date:** [April 7, 2020, 7:19am UTC](https://discuss.dgraph.io/t/paths-throw-several-nodes/6271/1 "2020-04-07T07:19:52Z")

</div>

Hello! I’m working on a project where I need to find the route between two nodes, but this path must contains several nodes. For example:

```auto
{
  set {
    _:1 <name> "test1" .
    _:1 <next> _:2 .
    _:1 <next> _:5 .

    _:2 <name> "test2" .
    _:2 <next> _:3 .
    _:2 <next> _:4 .
    _2 <next> _:5 .

    _:3 <name> "test3" .
    _:3 <next> _:5 .
    
    _:4 <name> "test4" .
    _:4 <next> _:5 .

    _:5 <name> "test5" .

    _:6 <name> "test6" .
  }
}

 function(from "test1", to "test5", contains = ["test2", "test4"]) = [["test1" ,"test2" , "test4", "test5"], ["test1" ,"test2", "test3" , "test4", "test5"] ]

 function(from "test1", to "test5", contains = ["test2"]) = [["test1" ,"test2", "test5"], ["test1" ,"test2" , "test3", "test5"], ["test1" ,"test2" , "test4", "test5"], ["test1" ,"test2", "test3" , "test4", "test5"] ]

 function(from "test1", to "test5", contains = []) = [["test1", "test5"], ["test1" ,"test2", "test5"], ["test1" ,"test2" , "test3", "test5"], ["test1" ,"test2" , "test4", "test5"], ["test1" ,"test2", "test3" , "test4", "test5"] ]

 function(from "test1", to "test5", contains = ["test6"]) = []

```

So I’m wondering is dgraph a good solution? And if it is how would you do that? I watched on function “K Shortest Path Query”. It gives me the shortest path between a source ( `from` ) node and destination ( `to` ) node. But I can’t influence on this way.

---

<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:** [April 7, 2020, 7:10pm UTC](https://discuss.dgraph.io/t/paths-throw-several-nodes/6271/2 "2020-04-07T19:10:21Z")

</div>

You can use Recurse query [https://dgraph.io/docs/query-language/#recurse-query](https://dgraph.io/docs/query-language/#recurse-query)

and apply rules like

```auto
{
	me(func: somefunc..) @recurse(...your params here...) {
		performance.actor @filter(do some func as a kind of "constraint" to the several nodes) 
        #This filter will be applied recursively on this edge
}

```

If your need is too complex to a single block with recurse, you should do it piece by piece (block by block)

---

<div class="post-metadata">

**Author:** ![g.v.pol](https://avatars.discourse-cdn.com/v4/letter/g/6bbea6/32.png) [@g.v.pol](https://discuss.dgraph.io/u/g.v.pol)\
**Post date:** [April 8, 2020, 8:58am UTC](https://discuss.dgraph.io/t/paths-throw-several-nodes/6271/3 "2020-04-08T08:58:10Z")

</div>

Hello! Thanks for answer. I understand how I have to use Recurse query if I now all function for filter. I mean if I now all restriction on next step in path, it is ok. But what about this situation?

function(from “test1”, to “test5”, contains = [“test2”]) = [[“test1” ,“test2”, “test5”], [“test1” ,“test2” , “test3”, “test5”], [“test1” ,“test2” , “test4”, “test5”], [“test1” ,“test2”, “test3” , “test4”, “test5”] ]

I now only one node which includes on path. What should the filter be like?

Excuse me for my English.
