Gremlin 查询所有传出边都与给定列表列表相关的节点

Gremlin query for nodes that all outgoing edges are related to a given list list

我有一个图表模式: ( Person )-- Adopted --> ( Cat )<-- Parent_Of --( Cat )

给定一个猫的列表,我想找到他们收养的所有猫都来自列表中的人,或者是列表中的猫的儿子。

我们可以假设每个人都收养了至少一只猫。

例如: 帕特收养了菲利克斯和辛巴。 辛巴是萨拉比的儿子。

如果我查询 Felix 和 Sarabi,我想查找 Pat。

如果我查询 Felix 和 Simba,我想查找 Pat.

如果我只查询 Felix,我不想查找 Pat。

我对 Felix 和 Sarabi 的查询是:

g.V().hasLabel('Person').where( 
    out('Adopted').not(
        or(
            hasLabel('Cat').has('name','Felix'),
            hasLabel('Cat').has('name','Sarabi'),
            __.in('Parent_Of').hasLabel('Cat').has('name','Felix'),
            __.in('Parent_Of').hasLabel('Cat').has('name','Sarabi'),
        )
    ).count().is(eq(0))
).properties('name')

这是正确的,但不直观且效率很低,尤其是在查询多只猫时,每只猫有很多 children.

如果这对某人有帮助,我找到的解决方案是:

g.V().or(
    hasLabel('Cat').has('name','Felix'),
    hasLabel('Cat').has('name','Sarabi'),
).union( identity() , out('Parent_Of') ).aggregate('cats').fold() \
.V().hasLabel('Person').where( 
    out('Adopted').where( without( 'cats' ) ).count().is(eq(0)) 
).properties('name')