2016-07-08 5 views
1

私はこの質問のようにデータベースを持っている:Neo4jrb - サイファークエリ

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 
(Railsのアプリから):

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) 

Neo4j - Finding the Shortest Path between two nodes based on relationship propertyは今、私はこのようなクエリを実行しようとしています

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)モデルが必要です。あなたはまた、Neo4j::Core::QueryQueryProxyチェーンから移動することができhttps://github.com/neo4jrb/neo4j-core/wiki/Queries :この宝石は、あなたがどのようにに関する詳細な情報を持っているほとんどのNeo4jのための標準的なActiveModelの素敵なラッパー:)

です。 MATCH p =(start) - [:distance *] - https://github.com/neo4jrb/neo4j/wiki/Search-and-Match#detailed-querying

0

MATCH(開始:ポイント{タイトル: 'Some Point 1'}) >(終了)Pで は、削減| DISTのDESC

ギジェルモBY(S = 0、当該relsのR(P)S + r.value)AS distの RETURN pを、DIST順序は絶対的に正しいですが、私は思いましたあなたが反復処理または上to_aを呼び出すことができEnumerableを与える

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) 

:私はいくつかのコードで刺しを与えるだろう。それとも、直接2次元配列を返すことができます:ギレルモが言ったように

.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

ギジェルモは絶対的に正しいですが、私は、私はいくつかのコードで刺しを与えるだろうと思った:

あなたが反復処理または上 to_aを呼び出すことができ Enumerableを与える
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) 

。それとも、直接2次元配列を返すことができます:ギレルモが言ったように

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

これはActiveNodeモデルを前提としています。何かのようなもの;

class Point 
    include Neo4j::ActiveNode 
    property :title 

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

アソシエーションの名前は、あなたが望むものかもしれませんが、私はちょうど推測しました。

関連する問題