# Shortest path query problem

**URL:** <https://discuss.dgraph.io/t/shortest-path-query-problem/10875>\
**Category:** Dgraph\
**Tags:** kind:question\
**Created:** [October 10, 2020, 8:32pm UTC](https://discuss.dgraph.io/t/shortest-path-query-problem/10875 "2020-10-10T20:32:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![greatvovan](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/greatvovan/32/5068_2.png) [@greatvovan](https://discuss.dgraph.io/u/greatvovan)\
**Post date:** [October 10, 2020, 8:32pm UTC](https://discuss.dgraph.io/t/shortest-path-query-problem/10875/1 "2020-10-10T20:32:38Z")

</div>

I am new to Dgraph and now exploring the tour. I created types as described on step 3 and loaded the data as in this lesson: [Load Data | Intro | Dgraph Tour](https://dgraph.io/tour/intro/4)

I am trying to query shortest path between nodes as described in the [article](http://discuss.hypermode.com/t/k-shortest-path-query-returns-path-that-doesnt-have-the-to-node/9927/16):

```auto
{
 path as shortest(from: 0xb, to: 0x2) {
  friend
 }
 path(func: uid(path)) {
   name
 }
}

```

With some node pairs it works as expected, e.g. from: 0xb (“Michael”), to: 0x1 (“Sarah”), but with others it gives weird output. Examples are:

1. from: 0xb (“Amit”), to: 0x2 (“Hyung Sin”):  
Empty `path` array:

```auto
{
  "data": {
    "path": [],
    "_path_": [
      {
        "friend": {
          "friend": {
            "uid": "0x2"
          },
          "uid": "0x5"
        },
        "uid": "0xb",
        "_weight_": 2
      }
    ]
  }

```

1. from: 0x4 (“Michael”), to: 0xb (“Amit”)  
Only one node in `path` (source only):

```auto
{
  "data": {
    "path": [
      {
        "name": "Michael"
      }
    ],
    "_path_": [
      {
        "friend": {
          "uid": "0xb"
        },
        "uid": "0x4",
        "_weight_": 1
      }
    ]
  }

```

1. from: 0x4 (“Michael”), to: 0xb (“Amit”)  
Only one node in `path` (destination only):

```auto
{
  "data": {
    "path": [
      {
        "name": "Michael"
      }
    ],
    "_path_": [
      {
        "friend": {
          "uid": "0x4"
        },
        "uid": "0xb",
        "_weight_": 1
      }
    ]
  }

```

The `_path_` content appears to be correct though.

It seems that only specific nodes can be shown in `path` (“Michael”, “Sarah” and “Catalina”) while others cannot, but I do not understand what is wrong with these nodes and how one category is different from another.

I am launching Dgraph in Docker, tested on image `dgraph/dgraph`, tags `latest`, `master`, `v1.2.7` (all the same).

Is it a bug or I misunderstand something?

---

<div class="post-metadata">

**Author:** ![greatvovan](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/greatvovan/32/5068_2.png) [@greatvovan](https://discuss.dgraph.io/u/greatvovan)\
**Post date:** [October 10, 2020, 8:33pm UTC](https://discuss.dgraph.io/t/shortest-path-query-problem/10875/2 "2020-10-10T20:33:26Z")

</div>

[This issue](http://discuss.hypermode.com/t/k-shortest-path-query-returns-path-that-doesnt-have-the-to-node/9927/16) seems to be relevant as it describes a specific case of my issue, but the thread suggests that it was fixed…

---

<div class="post-metadata">

**Author:** ![Anurag](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/anurag/32/2599_2.png) [@Anurag](https://discuss.dgraph.io/u/Anurag)\
**Post date:** [October 11, 2020, 12:54pm UTC](https://discuss.dgraph.io/t/shortest-path-query-problem/10875/3 "2020-10-11T12:54:35Z")

</div>

> [@greatvovan](#):
>
> but I do not understand what is wrong with these nodes and how one category is different from another.

Setting data like below could assign a different/non-deterministic uid to nodes:

```auto
{
  set {
    _:michael <name> "Michael" .
    _:michael <dgraph.type> "Person" .
    _:michael <age> "39" .
    _:michael <friend> _:amit .

    _:amit <name> "Amit"@en .
    _:amit <dgraph.type> "Person" .
    _:amit <age> "35" .
    _:amit <friend> _:michael .
    _:amit <friend> _:sang .
  }
}

```

Therefore do not hardcode the uids in your shortest path query, instead you could follow this:

```auto
{ 
  var(func: eq(name, "Michael")){
   SOURCE as uid
  }
  var(func: eq(name, "Amit")){
   TARGET as uid
  }  

  path as shortest(from: uid(SOURCE), to: uid(TARGET)) {
    friend
  }
  
   path(func: uid(path)) {
    name
   } 
}

```

It is possible that the hardcoded uids you provide in shortest queries are assigned to some other node. You can check that by:

```auto
{
    query(func: uid("0xb")) {
    name
   } 
}

```

---

<div class="post-metadata">

**Author:** ![greatvovan](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/greatvovan/32/5068_2.png) [@greatvovan](https://discuss.dgraph.io/u/greatvovan)\
**Post date:** [October 11, 2020, 9:32pm UTC](https://discuss.dgraph.io/t/shortest-path-query-problem/10875/4 "2020-10-11T21:32:13Z")

</div>

Thanks for your suggestion Anurag, this improves readability greatly!

The UIDs are correct, do you have any thoughts on the matter of the issue? Is it a bug?

---

<div class="post-metadata">

**Author:** ![rajas](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/rajas/32/4393_2.png) [@rajas](https://discuss.dgraph.io/u/rajas)\
**Post date:** [October 12, 2020, 4:22am UTC](https://discuss.dgraph.io/t/shortest-path-query-problem/10875/5 "2020-10-12T04:22:50Z")

</div>

This is not a bug.  
In the dataset, `name` field is not provided for all nodes of type `Person`.  
The path query works well for Michael and Sarah because `name` field is defined for them.

For other entities like Amit, Hyung Sin, Luke, Artyom `name@en` field is defined using a facet.  
To make the query work for them, you can either use the following shortest path query or add `name` field to all other entities.

```auto
{
 path as shortest(from: 0xb, to: 0x2) {
  friend
 }
 path(func: uid(path)) {
   name
   name@en
 }
}

```

The path query tries to see if `name` field is defined for the nodes on path. If it is not defined, it does not return any field for that node.

---

<div class="post-metadata">

**Author:** ![greatvovan](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/greatvovan/32/5068_2.png) [@greatvovan](https://discuss.dgraph.io/u/greatvovan)\
**Post date:** [October 12, 2020, 9:23pm UTC](https://discuss.dgraph.io/t/shortest-path-query-problem/10875/6 "2020-10-12T21:23:40Z")

</div>

Oh… that absolutely makes sense!

Thanks for the explanation @rajas!
