1)獲取的國家的陣列與可能重複的路徑:REDUCE
2)刪除重複與比較陣列的尺寸:UNWIND
+ COLLECT(DISTINCT...)
MATCH path = (start:Person)-[:FRIENDSHIP*1..20]->(person:Person)
WHERE start.id = 128 AND start.country <> "Uganda"
WITH path,
REDUCE(acc=[], n IN NODES(path) | acc + n.country) AS countries
UNWIND countries AS country
WITH path,
countries, COLLECT(DISTINCT country) AS distinctCountries
WHERE SIZE(countries) = SIZE(distinctCountries)
RETURN path
P.S.
MATCH path = (start:Person)-[:FRIENDSHIP*1..20]->(person:Person)
WHERE start.id = 128 AND start.country <> "Uganda"
WITH path,
EXTRACT(n IN NODES(path) | n.country) AS countries
UNWIND countries AS country
WITH path,
countries, COLLECT(DISTINCT country) AS distinctCountries
WHERE SIZE(countries) = SIZE(distinctCountries)
RETURN path
P.P.S.:REDUCE
可以通過EXTRACT
(感謝的Gabor Szarnyas)來代替再次感謝Gabor Szarnyas爲簡化查詢的另一個想法:
MATCH path = (start:Person)-[:FRIENDSHIP*1..20]->(person:Person)
WHERE start.id = 128 AND start.country <> "Uganda"
WITH path
UNWIND NODES(path) AS person
WITH path,
COLLECT(DISTINCT person.country) as distinctCountries
WHERE LENGTH(path) + 1 = SIZE(distinctCountries)
RETURN path
謝謝你的回答!從性能的角度來看它會是最好的選擇嗎?我將有一個非常大的數據集,有多達數百萬個節點。有什麼方法可以使查詢更快嗎? –
@VolodymyrBakhmatiuk查詢中最重的部分是第一個匹配:MATCH path =(start:Person) - [:FRIENDSHIP * 1..20] - >(person:Person)WHERE start.id = 128 AND start .country <>「烏干達」'。目前還不完全明白如何改進它... –
@VolodymyrBakhmatiuk測試路徑最簡單的部分... –