Neo4j.rb如何直接运行Cypher查询?

问题描述 投票:1回答:2

我想在rails中直接运行Cypher查询不想使用ORM样式,因为我在neo4j控制台上进行了很长时间的查询,当我尝试更改为orm样式时,它的行为不符合预期

MATCH (n {name: 'MU1'})-[:connected_to*1..2 {status: 1}]->(sp:User),
      (sp)<-[:owner_of|house_mate]-(place:Place)-[:owner_of|house_mate]->(c:User)
WHERE NOT (n)-[:house_mate]-(place)
MATCH
  (place)-[tenant:owner_of|house_mate]->(u:User)
WITH DISTINCT place, type(tenant) AS type, u
WITH place, collect({type: type, u: u}) AS tenants
RETURN
   place,
   [tenant IN tenants WHERE tenant.type = 'owner_of'   | [tenant.u]][0] AS owner,
   [tenant IN tenants WHERE tenant.type = 'house_mate' | [tenant.u]] AS houseMatesArray

Neo4j.query和Neo4j._query等

任何帮助?

编辑:如何用ORM风格编写它可能是我做错了什么?

ruby neo4j neo4j.rb
2个回答
1
投票

这是评论中要求的Query风格。但是,通过这样的查询,除非你传递部分Query对象,否则你不会从这种风格中获得太多好处。您可能希望坚持使用Ruby heredoc中定义的Cypher查询。

Neo4j::ActiveBase.new_query
  .match(n: {name: 'MU1'})
  .match('(n)-[:connected_to*1..2 {status: ?}]->(sp:User)', 1)
  .match('(sp)<-[:owner_of|house_mate]-(place:Place)-[:owner_of|house_mate]->(c:User)')
  .where_not('(n)-[:house_mate]-(place)')
  .break
  .match('(place)-[tenant:owner_of|house_mate]->(u:User)')
  .with('DISTINCT place, type(tenant) AS type, u')
  .with(:place, tenants: 'collect({type: type, u: u})')
  .pluck(:place,
          owner: '[tenant IN tenants WHERE tenant.type = 'owner_of'   | [tenant.u]][0]',
          houseMatesArray: '[tenant IN tenants WHERE tenant.type = 'house_mate' | [tenant.u]]')

你需要在那里使用break来保持match条款不被分组,尽管那是因为我已经想要扭转一段时间的设计决定了。

此外,我认为应该有一个with_distinct方法因为DISTINCT(IIRC)适用于整个列集。


1
投票
Neo4j::ActiveBase.current_session.query(cypher_query_string)
© www.soinside.com 2019 - 2024. All rights reserved.