2016-07-08 32 views
1

我有一個數據庫,像這樣的問題:Neo4jrb - 暗號查詢

Neo4j - Finding the Shortest Path between two nodes based on relationship property

CREATE (some_point_1:Point {title:'Some Point 1'}) 
CREATE (some_point_2:Point {title:'Some Point 2'}) 
CREATE (some_point_3:Point {title:'Some Point 3'}) 
CREATE (some_point_4:Point {title:'Some Point 4'}) 
CREATE (some_point_5:Point {title:'Some Point 5'}) 
CREATE (some_point_6:Point {title:'Some Point 6'}) 

CREATE (some_point_1)-[:distance {value:100}]->(some_point_2) 
CREATE (some_point_2)-[:distance {value:150}]->(some_point_4) 
CREATE (some_point_1)-[:distance {value:200}]->(some_point_3) 
CREATE (some_point_3)-[:distance {value:300}]->(some_point_4) 
CREATE (some_point_2)-[:distance {value:500}]->(some_point_5) 
CREATE (some_point_4)-[:distance {value:300}]->(some_point_5) 
CREATE (some_point_5)-[:distance {value:300}]->(some_point_6) 
CREATE (some_point_6)-[:distance {value:300}]->(some_point_1) 

現在我試圖執行這樣的查詢:(從Rails應用程序)

MATCH (start:Point {title: 'Some Point 1'}), (end:Point {title: 'Some Point 5'}) 
MATCH p=(start)-[:distance*]->(end) 
WITH p,reduce(s = 0, r IN rels(p) | s + r.value) AS dist 
RETURN p, dist ORDER BY dist DESC 

我試圖做一個類似的查詢與active model wrapper,但它不工作:(

有沒有辦法從neo4jrb或neo4j-core執行純Cypher查詢?

回答

1

是的,如果您想通過neo4j-core寶石(您在使用neo4j寶石時無論如何需要),可以直接輕鬆地完成Cypher。

例如:n = Neo4j::Session.query("MATCH (n) RETURN n").first(假設已配置你的應用程序與所述Neo4j的服務器進行通信)。

如果您使用的是neo4j寶石,您需要在應用程序中有相應的ActiveNode(可能是ActiveRel)模型,以便您可以進行查詢。這顆寶石是非常標準ActiveModel對Neo4j的一個很好的包裝:)

您有關於如何的更多信息:https://github.com/neo4jrb/neo4j-core/wiki/Queries 您也可以從QueryProxy鏈轉移到Neo4j::Core::Query。見https://github.com/neo4jrb/neo4j/wiki/Search-and-Match#detailed-querying

0

MATCH(開始:點{標題: '某些點1'}),(端:點{標題: '某些點5'}) MATCH P =(開始) - [:距離*] - >(結束) 具p,降低性(s = 0,R IN RELS(p)| S + r.value)AS DIST RETURN p,DIST ORDER BY DIST DESC

吉列爾莫是絕對正確的,但我想我將一些代碼給刺:

Point.where(title: 'Some Point 1').connected(:end, :rels, rel_length: :any).where(title: 'Some Point 5') 
    .query.with(:p, dist: 'reduce(s = 0, r IN :rels | s + r.value)') 
    .return(:p, :dist) 
    .order(disc: :desc) 

這會給你一個Enumerable您可以遍歷或致電to_a。或者你可以直接返回一個二維數組:

.order(disc: :desc) 
    .pluck(:p, :dist) 

這是假設的ActiveNode模型吉列爾莫說。就像是;

class Point 
    include Neo4j::ActiveNode 
    property :title 

    has_many :out, :connected, type: :distance, model_class: :Point 
end 

該協會的名稱可能是也可能不是你想要的,我只是猜到了。

0

吉列爾莫是絕對正確的,但我想我會在一些代碼給刺:

Point.where(title: 'Some Point 1').connected(:end, :rels, rel_length: :any).where(title: 'Some Point 5') 
    .query.with(:p, dist: 'reduce(s = 0, r IN :rels | s + r.value)') 
    .return(:p, :dist) 
    .order(disc: :desc) 

這會給你一個Enumerable您可以遍歷或致電to_a。或者你可以直接返回一個二維數組:

.order(disc: :desc) 
    .pluck(:p, :dist) 

這是假設的ActiveNode模型吉列爾莫說。就像是;

class Point 
    include Neo4j::ActiveNode 
    property :title 

    has_many :out, :connected, type: :distance, model_class: :Point 
end 

該協會的名稱可能是也可能不是你想要的,我只是猜到了。