1

I want to run directly Cypher queries in rails don't want to use ORM style because I have long queries which I made on neo4j console and when I am trying to change into orm style its not behaving as expected

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 and Neo4j._query etc

Any Help?

Edit: How to write it in ORM style may be I was doing something wrong?

Vishal G
  • 1,521
  • 11
  • 30

2 Answers2

1
Neo4j::ActiveBase.current_session.query(cypher_query_string)
Brian Underwood
  • 10,746
  • 1
  • 22
  • 34
  • can you please tell me how to write it in ORM style as well `.match().where() etc`? – Vishal G Dec 19 '17 at 05:59
  • Also in my query there is match written after `where not`, all the match statements grouped together to run at first then where, with etc? – Vishal G Dec 19 '17 at 06:03
  • 1
    You should definitely group appropriate `WHERE` clauses with the `MATCH` clauses that define the variables. One thing that I don't think is generally understood is that `WHERE` is really a modifier for `MATCH`, not it's own clause – Brian Underwood Dec 19 '17 at 14:56
  • I can make another answer with `Query` style, though your question said you wanted to just execute the Cypher string – Brian Underwood Dec 19 '17 at 14:58
  • Thanks @brian I will look into your answers and will update you – Vishal G Dec 19 '17 at 16:29
1

Here is Query style as requested in comments. With a query like this, though, you don't get much benefit from this style unless maybe you're passing partial Query objects around. You probably want to stick to a Cypher query defined in a Ruby heredoc.

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]]')

You need the break in there to keep the match clauses from grouping, though that's because of a design decision that I've wanted to reverse for a while now.

Also, I'm thinking that there should be a with_distinct method because DISTINCT (IIRC) applies to the whole set of columns.

Brian Underwood
  • 10,746
  • 1
  • 22
  • 34