Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arango db generate breadcrumbs while traversing graph

Tags:

arangodb

I searched all over the arangodb project website, but came up with no solution. If I traverse simple tree like graph like continents/countries/capitals how can I get breadcrumbs for current item? Hope that makes sense

like image 248
Marius Avatar asked Nov 30 '13 16:11

Marius


1 Answers

I'll start with your example using the arangosh and Gremlin.JS. Later I'm going to use AQL with is available in all language drivers.

arangosh [_system]> var Graph = require("org/arangodb/graph").Graph;

First the different locations (regions, cities):

arangosh [_system]> v1 = g.addVertex("Earth");
arangosh [_system]> v2 = g.addVertex("Europe");
arangosh [_system]> v3 = g.addVertex("Germany");
arangosh [_system]> v4 = g.addVertex("Berlin");
arangosh [_system]> v5 = g.addVertex("Spain");
arangosh [_system]> v6 = g.addVertex("Barcelona");

and their relation:

arangosh [_system]> g.addEdge(v1, v2);
arangosh [_system]> g.addEdge(v2, v3);
arangosh [_system]> g.addEdge(v3, v4);
arangosh [_system]> g.addEdge(v2, v5);
arangosh [_system]> g.addEdge(v5, v6);

So the graph is now your tree:

arangosh [_system]> v1.outbound().getInVertex()
[ Vertex("Europe") ]

arangosh [_system]> v1.outbound().getInVertex().outbound().getInVertex()
[  [  Vertex("Germany"),  Vertex("Spain") ] ]

arangosh [_system]> v1.outbound().getInVertex().outbound().getInVertex().outbound().getInVertex()
[  [  [  Vertex("Berlin")  ],  [  Vertex("Barcelona")  ]  ]  ]

In order to get the breadcrumb from say Berlin, you can compute a path from Berlin to Earth:

arangosh [_system]> v4.pathTo(v1);
[
  [
    "Berlin",
    "Germany",
    "Europe",
    "Earth"
  ]
]

Using AQL you can use the traversal to traverse from Berlin back to the root:

arangosh [_system]> db._query('FOR p in TRAVERSAL(locations, part, @start, "inbound", {}) RETURN p.vertex._key', { start: v4._id } ).toArray()
[
  "Berlin",
  "Germany",
  "Europe",
  "Earth"
]

I hope that example gives you any idea how to create the breadcrumb.

Cheers Frank

like image 154
fceller Avatar answered Oct 31 '22 23:10

fceller