简体   繁体   English

遍历Arangodb图,边缘进行数组过滤

[英]Traverse Arangodb graph with array filtering on edges

I have this test graph in arangodb 我在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. 请注意,在此示例中,每个边缘都有一个属性“ cat”作为类别对象的数组。 Actually, in the real set of data, each edge is part of one or more networks. 实际上,在实际数据集中,每个边缘都是一个或多个网络的一部分。 There are 22 networks/categories. 有22个网络/类别。 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. 在此工作示例中,只有两个1和2。每个边都是一个类别的一部分,但D2-> E3是这两个类别中唯一的一个。

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] 示例:从B开始并选择类别2,我需要返回以下集合:v:[B,D2,E2,E3,C1,C4]和e:[{B-> D2,D2-> E2,D2-> E3,C1-> B,C4-> B]

In AQL I tried various filters starting from: 在AQL中,我尝试了以下各种过滤器:

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). 什么都没做(当然,我是Arango的新手)。

Question 1: How to construct the filter ? 问题1:如何构造过滤器?

Question 2: How to format the results like in the example above? 问题2:如何像上面的示例一样格式化结果? 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) 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] . 1)为了在cat过滤条目"c":2 ,对于路径( p.edges[*] )上的所有边,必须检查cat数组( .cat[*] )的c属性( .c.cat[*] )包含值[2]

Therefore we use the IN operator when accessing the sub-attribute array p.edges[*].cat[*].c . 因此,在访问子属性数组p.edges[*].cat[*].c时,我们使用IN运算符。

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

2) The return format can also be adjusted by accessing sub-attributes like: 2)还可以通过访问子属性来调整return格式,例如:

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}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM