Flattening to get unique nodes within n steps


(Adam Hopkins) #1

Hey everyone … I am new to dgraph, and having some trouble with trying to flatten a set.

Basically, I am trying to get a list of all the unique nodes in a query. With a set of bordering nodes, I want to know everything that is within n steps.

Here is a sample set:

{
    set {
        _:a <name> "NodeA" .
        _:b <name> "NodeB" .
        _:c <name> "NodeC" .
        _:d <name> "NodeD" .
        _:e <name> "NodeE" .
        _:f <name> "NodeF" .
        _:g <name> "NodeG" .
        _:h <name> "NodeH" .

        _:a <borders> _:b .
        _:b <borders> _:a .
        _:b <borders> _:c .
        _:b <borders> _:d .
        _:d <borders> _:b .
        _:d <borders> _:c .
        _:d <borders> _:e .
        _:c <borders> _:b .
        _:c <borders> _:d .
        _:c <borders> _:e .
        _:c <borders> _:f .
        _:c <borders> _:g .
        _:g <borders> _:h .
        _:g <borders> _:f .
        _:g <borders> _:c .
        _:f <borders> _:h .
        _:f <borders> _:g .
        _:f <borders> _:c .
        _:f <borders> _:d .
        _:f <borders> _:e .
        _:h <borders> _:g .
        _:h <borders> _:f .
        _:h <borders> _:f .
        _:h <borders> _:e .
        _:e <borders> _:h .
        _:e <borders> _:f .
        _:e <borders> _:c .
        _:e <borders> _:d .
        _:d <borders> _:e .
        _:d <borders> _:c .
        _:d <borders> _:b .
    }
}

The query that I built is:

{
  me(func: eq(name, "NodeA"))@recurse(depth: 4, loop: true)  @normalize {
    n: name
    borders {
      n: name
    }
  }
}

Results:

{
  "data": {
    "me": [
      {
        "n": [
          "NodeA",
          "NodeB",
          "NodeC",
          "NodeF"
        ]
      },
      {
        "n": [
          "NodeA",
          "NodeB",
          "NodeC",
          "NodeG"
        ]
      },
      {
        "n": [
          "NodeA",
          "NodeB",
          "NodeC",
          "NodeE"
        ]
      },
      {
        "n": [
          "NodeA",
          "NodeB",
          "NodeC",
          "NodeB"
        ]
      },
      {
        "n": [
          "NodeA",
          "NodeB",
          "NodeC",
          "NodeD"
        ]
      },
      {
        "n": [
          "NodeA",
          "NodeB",
          "NodeD",
          "NodeC"
        ]
      },
      {
        "n": [
          "NodeA",
          "NodeB",
          "NodeD",
          "NodeE"
        ]
      },
      {
        "n": [
          "NodeA",
          "NodeB",
          "NodeD",
          "NodeB"
        ]
      }
    ]
  },
  ...
}

Really, what I am hoping to get is:

{
    ...
      {
        "n": [
          "NodeA",
          "NodeB",
          "NodeC",
          "NodeD",
          "NodeE",
          "NodeF",
          "NodeG"
        ]
      }
    ...
}

So, it would appear I am going at this the wrong way. Given my set above, how can I traverse n levels and get a single list of unique nodes?


Storing and querying historical data
Recurse with calculated field
Friends of Friends n hops away
(Michel Conrado (Support Engineer)) #2

This serves?


{
  var(func: eq(name, "NodeA"))@recurse(depth: 140, loop: true) {
    G as borders
  }
  
  test(func: uid(G), orderasc: name) {
    n: name
  }
}
{
  "data": {
    "test": [
      {
        "n": "NodeA"
      },
      {
        "n": "NodeB"
      },
      {
        "n": "NodeC"
      },
      {
        "n": "NodeD"
      },
      {
        "n": "NodeE"
      },
      {
        "n": "NodeF"
      },
      {
        "n": "NodeG"
      },
      {
        "n": "NodeH"
      }
    ]
  },
  "extensions": {
    "server_latency": {
      "parsing_ns": 47074,
      "processing_ns": 14195219,
      "encoding_ns": 607901
    },
    "txn": {
      "start_ts": 1781,
      "lin_read": {
        "ids": {
          "1": 2640
        }
      }
    }
  }
}

Traverse Deeply Nested Graph Easily
(Adam Hopkins) #3

@MichelDiz awesome! That will work. Actually, it will work better than what I was asking about.

I think I need to spend some more time understanding how to use var. I tried to split the query similar to what you proposed, but it was simply not working.

Thank you.


(Michel Conrado (Support Engineer)) #4

Nice! glad it worked for you!

Cheers.