2017-03-02 21 views
0

我已經得到了包括代表在版本的路徑連接在一起的文檔版本節點的圖中發現。這些路徑可以通過代表文檔版本更改方式的另一種關係進行關聯。圖的問題之一是用於創建它的源代碼並不真正乾淨,這就是爲什麼我要編寫一個查詢來添加一個關係,以便在圖中具有乾淨的版本路徑。暗號:連接兩個路徑的結局

我卡上的部分如下: 都說我有兩個不同的版本時間節點的兩條路徑。這些路徑通過第二種類型的一個或多個關係連接在一起,指示文檔的一個端口到新系統。我想要一個查詢,使最後一個滿足舊路徑中的一些條件,並將它連接到第一個滿足新路徑中的其他條件的查詢。

例如在下面的圖表,我會想(D)連接到(2)因爲(1)不能滿足我的組條件:

(A)-[:Version]->(B)-[:Version]->(C)-[:Version]->(D) 
       |    | 
       Ported   Ported 
       |    | 
(1)-[:Version]->(2)-[:Version]->(3) 

我想出了不同的查詢,但他們都在某些情況下失敗:

因爲有時舊文件,其中移植和拆分成多個文檔,這意味着不同的道路,但我的查詢選擇一個「老」的一個從而忽視了一些路徑只有一個新的「新」節點這一次失敗。

//match all the 'port' and 'ported' relations between old and new versioning system 
match (new:Document)-[r:Link]-(old:Document) 
    where new.num =~'[A-Z].{4}-.*' and old.num =~'[A-Z].{3}-.*' and r.type in ['PORT','PORTED'] 
//find youngest one satisfying a condition, here a date 
optional match(new)<-[:Version*]-(newAncestor:ArticleCode) 
    where newAncestor.dateBegin >= '2012-01-01' 
with old, collect(new) + collect(newAncestor) as potentialNewVersions 
unwind potentialNewVersions as potentialNew 
with distinct old, potentialNew 
order by potentialNew.dateBegin , potentialNew.dateEnd 
with distinct old, collect(potentialNew)[0] as youngestNew 

//find oldest one satisfying a condition 
optional match(old) -[:Version *]->(oldChild:ArticleCode) 
    where oldChild.dateEnd <= youngestNew.dateBegin 
with old, youngestNew, collect(old) + collect(oldChild) as potentialOldVersions 
unwind potentialOldVersions as potentialOld 
with distinct old, youngestNew, potentialOld 
order by potentialOld.dateEnd desc, potentialOld.dateBegin desc 
    with distinct youngestNew, collect(potentialOld)[0] as oldestOld 

merge(youngestNew)<-[:VersionGlobal]-(oldestOld) 

第二個是要簡單得多,但選擇太多節點的「新」者爲多版本能滿足日期條件。另外,如果新路徑和新路徑之間唯一的「移植」關係在限制日期之前的某個節點上,則可能會失敗。

//this time I match all path of new versions whose first node satisfy condition 
match p=(new:Document)-[:Version*0..]->(:Document)-[r:Link]-(old:ArticleCode) 
    where new.num =~'[A-Z].{4}-.*' and old.num =~'[A-Z].{3}-.*' and r.type in ['PORT','PORTED'] and new.dateBegin >= '2012-01-01' 
//take first node of each path 
with distinct nodes(p)[0] as youngestNew, old 
//find latest old node 
optional match p=(old)-[:Version*0..]->(oldChild:ArticleCode) 
    where oldChild.dateFin <= youngestNew.dateDebut 
with distinct last(nodes(p)) as oldestOld, old 
merge(youngestNew)<-[:VersionGlobal]-(oldestOld) 

感謝

回答

0

我認爲,我們發現使用可選的比賽和案件的答案:

match (new:Document)-[:Version*0..]-(:Document)-[r:Lien]-(:Document)-[:Version*0..]-(old:Document) 
where *myConditions* 

optional match (newAncestor:Document)-[:Version]->(new) 
with distinct 
case 
    when newAncestor.dateBegin < '2012-01-01' or newAncestor is null 
    then new 
end as youngestNew, old 
where not(youngestNew is null) 

optional match (old)-[:Version]->(oldChild:Document) 
with distinct 
youngestNew, 
case 
    when oldChild.dateBegin > youngestNew.dateBegin or oldChild is null 
    then old 
end as oldestOld 
where not(oldestOld is null) 

*merge part*