通过过滤链接集合的多个字段来选择父记录

Selecting parent records by filtering multiple fields of collection of links

几天来我一直在努力弄清楚这个问题,但我想不出一个能给我正确结果的查询。该任务的本质是我试图检索一个图形的所有节点,这些节点的子节点的属性满足多个约束。我遇到的问题是一个节点可能有多个链接节点,当我尝试应用标准来限制查询必须返回哪些节点时,需要对节点集而不是单个节点施加标准。

让我通过一个例子更详细地解释这个问题。以下是公司和地点的示例架构。每个公司可以有多个地点。

create class company extends V;
create property company.name STRING;

create class location extends V;
create property location.name STRING;
create property location.type INTEGER;
create property location.inactive STRING;

现在让我创建一些记录来说明我遇到的问题。

create vertex company set name = 'Company1';
create vertex location set name = 'Location1', type = 5;
create vertex location set name = 'Location2', type = 7;
create edge from (select from company where name = 'Company1') to (select from location where name in ['Location1', 'Location2']);

create vertex company set name = 'Company2';
create vertex location set name = 'Location3', type = 6;
create vertex location set name = 'Location4', type = 5, inactive = 'Y';
create edge from (select from company where name = 'Company2') to (select from location where name in ['Location3','Location4']);

我想检索没有类型 5 位置或类型 5 位置处于非活动状态(非活动 = 'Y')的所有公司。我最初尝试的查询如下所示。它不起作用,因为 $loc.type 是针对值集合而不是单个记录进行评估的,因此 is null 不适用于每个位置记录的单个字段 'inactive' 而是针对集合每个父记录的字段 'inactive' 的值。我尝试了子查询、set 函数、append 等等,但我无法让它工作。

select from company let loc = out() where $loc.type not contains 5 or ($loc.type contains 5 and $loc.type is null)

试试这个查询:

select expand($d) from company
let $a=(select from company where out().type <> 5 and name contains $parent.current.name),
$b=(select from company where out().type contains 5 and name=$parent.current.name),
$c=(select from company where out().inactive contains "Y" and name=$parent.current.name),
$d=unionall($a,intersect($b,$c))

希望对您有所帮助,

此致,

米歇拉

您可以尝试使用此查询:

select expand($c)
let $a = ( select expand(out) from E where out.@class = "company" and in.@class="location" and in.type = 5 and in.inactive = "Y" ),
    $b = ( select from company where 5 not in out("E").type ),
    $c = unionAll($a,$b)

更新

我创建了这张图

您可以使用这个查询

select expand($f)
let $a = ( select from E where out.@class = "company" and in.@class="location" ),
    $b = ( select expand(out) from $a where in.type = 5 and in.inactive = "Y"),
    $c = ( select expand(out) from $a where in.type = 5 and in.inactive is null),
    $d = difference($b,$c),
    $e =  ( select from company where 5 not in out("E").type ),
    $f = unionAll($d,$e)

希望对您有所帮助。