1
votes

I have this test graph in arangodb

nodes:

[  { "_key": "A", "name": "A", "sector": "a"},
{ "_key": "B", "name": "B", "sector": "a"},
{ "_key": "C1", "name": "C1", "sector": "c"},
{ "_key": "C2", "name": "C2", "sector": "c"},
{ "_key": "C3", "name": "C3", "sector": "c"},
{ "_key": "C4", "name": "C4", "sector": "c"},
{ "_key": "D1", "name": "D1", "sector": "d"},
{ "_key": "D2", "name": "D2", "sector": "d"},
{ "_key": "E1", "name": "E1", "sector": "e"},
{ "_key": "E2", "name": "E2", "sector": "e"},
{ "_key": "E3", "name": "E3", "sector": "e"}]

edges:

[{ "_from": "V/A","_to": "V/D1", "cat": [{"c":1,"s":3}] },
{ "_from": "V/A","_to": "V/D2", "cat": [{"c":1,"s":1}] },
{ "_from": "V/B","_to": "V/D2", "cat": [{"c":2,"s":1}] },
{ "_from": "V/D1","_to": "V/E1", "cat": [{"c":1,"s":8}] }, 
{ "_from": "V/D1","_to": "V/E2", "cat": [{"c":1,"s":4}] },
{ "_from": "V/D2","_to": "V/E2", "cat": [{"c":1,"s":3},{"c":2,"s":4}] },
{ "_from": "V/D2","_to": "V/E3", "cat": [{"c":2,"s":4}] },
{ "_from": "V/C1","_to": "V/B", "cat": [{"c":2,"s":5}] },
{ "_from": "V/C1","_to": "V/A", "cat": [{"c":1,"s":6}] },
{ "_from": "V/C2","_to": "V/A", "cat": [{"c":1,"s":2}] },
{ "_from": "V/C3","_to": "V/A", "cat": [{"c":1,"s":1}] },
{ "_from": "V/C4","_to": "V/A", "cat": [{"c":1,"s":1}] },
{ "_from": "V/C4","_to": "V/B", "cat": [{"c":2,"s":2}] } ]

It is simplified part of much bigger graph (almost one thousand nodes, several thousand edges). Notice in this example that each edge has an property "cat" as an array of category objects. Actually, in the real set of data, each edge is part of one or more networks. There are 22 networks/categories. In this working example there are only two, 1 and 2. Each edge is part of one category, excepts D2->E3 being the only one here as member of both categories.

Problem: I have to traverse the graph by filtering/selecting edges of a chosen category(network in the real data) and its associated vertices, starting from a given vertex. Of course, avoiding loops and duplicated vertices or edges.

Example: starting from B and choosing category 2, I need to return this set: v: [B,D2,E2,E3,C1,C4] and e: [{B->D2, D2->E2, D2->E3, C1->B, C4->B]

In AQL I tried various filters starting from:

FOR v, e, p IN 0..3 any "nodes/D2" edges OPTIONS {bfs: true, uniqueVertices: 'global'}
  //Here, the filter for cat 2  ?
return p

Nothing worked (sure, I am newbie in Arango).

Question 1: How to construct the filter ?

Question 2: How to format the results like in the example above? More exactly (the order of objects in each array doesn't matter):

[
  nodes: [{name:"B",sector:"a"}, {name:"D2",sector:"d"}, {name:"E2",sector:"e"}, ...]
  edges: [{source: "B", target: "D2", s:1}, {source: "D2", target: "E2", s:4}, ...]
]

Thanks for help.

1

1 Answers

0
votes

1) In order to filter the entry "c":2 in cat, for all edges on the path (p.edges[*]) it has to be checked if the c attribute (.c) of the cat array (.cat[*]) includes the value [2].

Therefore we use the IN operator when accessing the sub-attribute array p.edges[*].cat[*].c.

FILTER [2] IN p.edges[*].cat[*].c

2) The return format can also be adjusted by accessing sub-attributes like:

return {'nodes':{'name':v.name,'sector':v.sector},'edges':{'source':e._from,'target':e._to, 's': e.cat[*].s}}

The adjusted query is:

FOR v, e, p IN 0..3 any "V/D2" edges
FILTER [2] IN p.edges[*].cat[*].c
return {'nodes':{'name':v.name,'sector':v.sector},'edges':{'source':e._from,'target':e._to, 's': e.cat[*].s}}