2012-10-23 82 views
2

我有一個像這樣的密碼查詢。如何使用帶空列表的頭函數獲取空值

START dep=node:cities(city_code = "JGS"), 
arr=node:cities(city_code = "XMN") 

MATCH dep-[way:BRANCH2BRANCH_AIRWAY*0..1]->()-->arr 

RETURN length(way), transfer.city_code, 
extract(w in way: w.min_consume_time) AS consumeTime 

命名爲「道」的關係是一個可選的人,所以命名爲「consumeTime」屬性會時的關係「的方式」不exsit一個空列表。

查詢結果是:

| 0 | 「JGS」| [] |
| 1 | 「SZX」| [3600] |

當我想使用具有屬性「consumeTime」的頭函數時,它返回一個錯誤「無效查詢:空列表頭」。

我怎樣才能得到這樣的結果?

| 0 | 「JGS」| null |
| 1 | 「SZX」| 3600 |

回答

1

這將是微不足道的與條件表達式,這是一件好事,我認爲重要的是要增加暗號:https://github.com/neo4j/community/issues/899

下面是使用reduce你有點哈克查詢需要1.9快照:

START dep=node:cities(city_code = "JGS"), 
     arr=node:cities(city_code = "XMN") 
MATCH dep-[way:BRANCH2BRANCH_AIRWAY*0..1]->()-->arr 
WITH length(way) as wayLength, 
     transfer.city_code as transferCityCode, 
     extract(w in way: w.min_consume_time) AS consumeTime 
WITH wayLength, 
     transferCityCode, 
     consumeTime, 
     // reverse the consumeTime list, so that we can get the head from the end 
     reduce(acc=[], x in consumeTime: x + acc) reverseConsumeTime 
RETURN wayLength, 
     transferCityCode, 
     consumeTime, 
     // return null if empty, else the end of the list 
     reduce(acc=null, x in reverseConsumeTime: x) as headOrNull; 

使用以下語法可以改進此查詢以反轉集合: reverse(coll)或條件表達式來檢查空列表。

+0

更新:在這裏發佈了一個功能請求(對於您的特定情況,這會比條件表達式更好):https://github.com/neo4j/community/issues/936 –

+0

非常感謝您的回答和功能請求。 – PeaceMaker

+0

我也提交了拉請求:https://github.com/neo4j/community/pull/938 –