简体   繁体   English

层次关系的图遍历

[英]Graph traversal for hierarchical relations

I have graph represents the parent child hierarchy.我有图表示父子层次结构。 Also it holds the relationship between objects.它还包含对象之间的关系。 一种

I have above as graph.我有上面的图表。 Where Orange are my parent-child hierarchy and Green are my relations.其中橙色是我的父子层次结构,绿色是我的关系。 So if i want to get relation between E and F , i will get the relation which is in between B and C (as they are parents of E and F).因此,如果我想获得EF之间的关系,我将获得BC之间的关系(因为它们是 E 和 F 的父母)。 This relation finding can go up to top most parents.这种关系发现可以达到大多数父母的最高水平。

I can find the parents of a node using Gremlin query like我可以使用 Gremlin 查询找到节点的父节点,例如

g.V().has('name', 'D').repeat(out('parent')).emit().values('name')

This query will return me B and A.这个查询将返回我 B 和 A。

Q. On similar lines does Gremlin or any other graph query language supports the relation inheritance ?问:在类似的方面,Gremlin 或任何其他图形查询语言是否支持关系继承? How Gremlin query should be formed ? Gremlin 查询应该如何形成?

Note : Graph can be very huge containing many unique nodes and many unique relations.注意:图可以非常庞大,包含许多独特的节点和许多独特的关系。 I want to get the inherited relations in quick time so that i wont have to pre-calculate and cached it or make duplicates for quick reference.我想快速获取继承的关系,这样我就不必预先计算和缓存它或复制以供快速参考。

I assume that by inheritance you mean the ability to traverse from grandparent to parent to child to grand child ,etc... Arango support traversals and has the ability to traverse these type of relationships very fast.我认为继承是指从祖父母到父母再到孩子再到孙子等的能力...... Arango 支持遍历并且能够非常快速地遍历这些类型的关系。 For example, to duplicate your example above of starting at node D and getting node B and A you could do something like :例如,要复制上面从节点 D 开始并获取节点 B 和 A 的示例,您可以执行以下操作:

// Find all nodes that are named d
let dNodes = (FOR test in test2
            FILTER test.name == 'd'
            RETURN test)

//Traverse outbound relationships starting at the dNodes and return up to 2 nodes up the hierarchy
FOR node in dNodes
   FOR v,e IN 1..2 OUTBOUND node
   testEdge
RETURN v

In terms of performance, I have traversed irregular hierarchies with thousands of nodes without performance issues and without having to cache anything.在性能方面,我遍历了具有数千个节点的不规则层次结构,没有性能问题,也无需缓存任何内容。 Keep in mind however that there is no magic here and a bad data model will cause trouble no matter the db engine.但是请记住,这里没有魔法,无论数据库引擎如何,错误的数据模型都会引起麻烦。

There is some performance information here if you want to review and play with it here如果您想在这里查看和使用它,这里有一些性能信息

Traversing multiple edges (relationship types) is very similar to our earlier example.遍历多条边(关系类型)与我们之前的示例非常相似。 To find the path from E to F using hierarchy (orange) edges and relationship (green) edges, we can do :要使用层次(橙色)边和关系(绿色)边找到从 E 到 F 的路径,我们可以这样做:

// Find all nodes that are named E
let eNodes = (FOR test in test3
            FILTER test.name == 'E'
            RETURN test
            )

// Start in node E and go upto three steps
// Traverse the hierarchy edges in any direction (so that we can find parents and child nodes)
// Traverse the relatedto (green) edges in the outbound direction only
// Filter the traversal to items that end in vertice F and return the path (E<-B->C->F)
FOR node in eNodes
   FOR v,e,p IN 1..3 ANY node
   parentOf, OUTBOUND relatedTo 
   FILTER v.name == 'F'
RETURN p

Or if we just want the shortest path between E and F we can do for example:或者,如果我们只想要 E 和 F 之间的最短路径,我们可以这样做:

let eNodes = (FOR test in test3
            FILTER test.name == 'E'
            RETURN test
            )
//Find shortest path between node E and F and return the path (E<-B->C->F)            
FOR node in eNodes
    FOR v, e IN ANY SHORTEST_PATH
      node TO 'test3/F'                 
      parentOf, OUTBOUND relatedTo
RETURN e

Note that I just used the id of the "F" record in the code above, but we could have searched fo the record using the name just like we did for the "E" record.请注意,我在上面的代码中只使用了“F”记录的 id,但我们可以使用名称搜索记录,就像我们对“E”记录所做的那样。

Also note we created the edge data for our example as directed edges in the DB: parentOf edges were created from parent to child (ex: A to B) and for green relationships edges we created them alphabetically (ex: B to C).另请注意,我们将示例的边数据创建为 DB 中的有向边:parentOf 边是从父到子创建的(例如:A 到 B),对于绿色关系边,我们按字母顺序创建它们(例如:B 到 C)。

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

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