Neo4jrb - 密码查询

Neo4jrb - Cypher query

我有一个类似这个问题的数据库:

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 查询?

是的,如果你愿意,你可以通过 neo4j-core gem 轻松地直接使用 Cypher(使用 neo4j gem 时无论如何你都需要它)。

例如:n = Neo4j::Session.query("MATCH (n) RETURN n").first(假设您已将应用程序配置为与 Neo4j 服务器通信)。

如果您正在使用 neo4j gem,您需要在您的应用程序中具有相应的 ActiveNode(可能 ActiveRel)模型,以便您可以执行您的查询。这个 gem 几乎是 Neo4j 标准 ActiveModel 的一个很好的包装 :)

您有更多关于如何:https://github.com/neo4jrb/neo4j-core/wiki/Queries的信息 您还可以从 QueryProxy 链移动到 Neo4j::Core::Query。参见 https://github.com/neo4jrb/neo4j/wiki/Search-and-Match#detailed-querying

匹配(开始:点{标题:'Some Point 1'}),(结束:点{标题:'Some Point 5'}) 匹配 p=(开始)-[:距离*]->(结束) WITH p,reduce(s = 0, r IN rels(p) | s + r.value) AS dist RETURN p, dist ORDER BY dist DESC

Guillermo 完全正确,但我想我会尝试一些代码:

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。或者你可以直接 return 一个二维数组:

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

正如 Guillermo 所说,这假设了一个 ActiveNode 模型。像;

class Point
  include Neo4j::ActiveNode
  property :title

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

协会的名称可能是你想要的,也可能不是,我只是猜测。

Guillermo 完全正确,但我想我会尝试一些代码:

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。或者你可以直接 return 一个二维数组:

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

正如 Guillermo 所说,这假设了一个 ActiveNode 模型。像;

class Point
  include Neo4j::ActiveNode
  property :title

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

协会的名称可能是你想要的,也可能不是,我只是猜测。