使用Neo4j 2.X和Cypher,我想查詢我直接或通過朋友知道的所有Users
。Cypher /效率關係基數
我希望是這樣的:
MATCH (me:User("123"))-[:KNOWS*1..2]-(friend) //does not work of course
我想想shortestPath
功能,但不會是太貴了?
而且,如果我有這個疑問:
MATCH (a)-[:SOME_REL]->(b)<-[:OWNS_BY]-(me:User("123")) // would load the whole in memory before filtering by knowledge !
WITH shortestPath((me)-[:KNOWS*..2]-(friend)) as path
WHERE path.length <= 2
OR
MATCH (a)-[:SOME_REL]->(b)<-[:OWNS_BY]-(me:User("123")) // would load the whole in memory before filtering by knowledge !
MATCH path = shortestPath((me)-[:KNOWS*..2]-(friend))
WHERE path.length <= 2
那豈不是更多(也許太龐大圖的情況下?)貴嗎?
事實上,這將是更好的,如果它的工作:
MATCH (a)-[:SOME_REL]->(b)<-[:OWNS_BY]-(me:User("123"))-[:KNOWS*1..2]-(friend)
加載在內存中只有合適的路徑。
我也可以用這樣的一種替代方案:
OPTIONAL MATCH (a)-[:SOME_REL]->(b)<-[:OWNS_BY]-(me:User("123"))-[:KNOWS]-(friend)
OPTIONAL MATCH (a)-[:SOME_REL]->(b)<-[:OWNS_BY]-(me:User("123"))-[:KNOWS]-()-[:KNOWS]-(friend)
但想象一下,如果我想三度分離(知識)的...查詢將是非常多餘。
是否有一個很好的語法會導致一個非常有效的查詢?
我應該使用什麼?
爲什麼第一個查詢不起作用? – JohnMark13 2014-10-19 08:05:18
我剛剛閱讀文檔,並沒有發現這種模式,除了'shortestPath' ..我認爲這個模式是專門用於'shortestPath'的。 – Mik378 2014-10-19 11:14:56