2016-02-28 78 views
0

我一直想弄明白這一點了幾天知道,但我不能拿出一個查詢,讓我得到正確的結果。任務的本質是我試圖檢索具有滿足多個約束的屬性的子圖的所有節點。我遇到的問題是一個節點可能有多個鏈接節點,當我嘗試應用標準來限制查詢必須返回哪些節點時,需要對節點集合而不是單個節點施加標準。通過篩選鏈接集合的多個字段來選擇父記錄

讓我通過一個例子更詳細地解釋問題。以下是公司和地點的示例模式。每家公司可以有多個地點。

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是針對值集合而不是單個記錄進行評估的,因此null不針對每個位置記錄的「非活動」字段應用,而是針對字段值集合應用null每個父記錄「非活動」。我嘗試了子查詢,設置函數,追加等,但我無法讓它工作。

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

回答

0

你可以用這個查詢嘗試:

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

UPDATE

我創造了這個圖表

enter image description here

您可以使用此查詢

select expand($f) 
let $a = (select from E where [email protected] = "company" and [email protected]="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) 

enter image description here

希望它能幫助。

+0

感謝您的回覆,但它似乎並不正確。一種情況是我需要檢索公司,如果他們有5類位置,他們的位置不活躍。如果我有一個位置類型爲5的公司,其中的非活動狀態爲null,而且一個位置類型爲5,並且非活動狀態設置爲Y,則上面的$ a子查詢將提取第二個位置並返回此公司,即使此公司也具有類型5是有效的。 –

+0

謝謝亞歷山德羅,這個作品。我不認爲這個查詢會擴展,但至少我可以得到我需要的。 –

0

嘗試此查詢:

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

希望它能幫助,

問候,

MICHELA

+0

感謝Michela的迴應,但這似乎也沒有給出預期的結果。如果我向type = 5的第二個公司添加第三個位置並且該位置處於活動狀態(不活動爲空),則查詢將在查詢的結果集中返回此公司,儘管事實上該公司具有類型= 5.你和亞歷山德羅使用集合來確定正確的結果爲我提供了一種嘗試解決這個問題的新方法。我有一個關於你的迴應的問題。? $ parent.current.name做什麼? –

+0

Hi @ChrisH基本上$父給出了父上下文,如果有的話。從子查詢遍歷時,您會發現這很有用。 $ current在迭代中給出當前記錄。要獲取嵌套查詢中的上層記錄,可以使用$ parent。$ current。 –