neo4jrb / neo4j-will_paginate_redux

Integrate neo4j and will_paginate
6 stars 4 forks source link

How to implement return distinct (from neo4jrb/neo4j #776) #4

Open pdipietro opened 9 years ago

pdipietro commented 9 years ago

I tried a lot of variations with with distinct but with no result. Here is the code and the result, and I saw 2 starnge things: 1) there is no distinct clause in the generated query 2) there are 2 queries .... in the wrong order

puts "Query: #{query_string}"
@users = Neo4j::Session.query.match(query_string).proxy_as(User, :user).paginate(:page => first_page, :per_page => secondary_items_per_page, return: "user with distinct",order: "user.last_name asc, user.first_name  asc")
puts "Count: #{@users.count}"

and this is the console output

Query: (user:User)-[p:participates|owns|admins]->(g:Group { uuid : 'b68fbf69-aabb-4755-890d-ba97ee6c3e02' }) 
 CYPHER 5ms MATCH (user:User)-[p:participates|owns|admins]->(g:Group { uuid : 'b68fbf69-aabb-4755-890d-ba97ee6c3e02' }) , (user:`User`) WITH user as user RETURN COUNT(user)
 CYPHER 7ms MATCH (user:User)-[p:participates|owns|admins]->(g:Group { uuid : 'b68fbf69-aabb-4755-890d-ba97ee6c3e02' }) , (user:`User`) RETURN user ORDER BY user.last_name asc, user.first_name  asc SKIP {skip_0} LIMIT {limit_25} | {"skip_0"=>0, "limit_25"=>25}
Count: 2

Below, you can find the query run in the web browser. As you can see, if I MANUALLY add the distinct clause there is only 1 item, as it should be.

So, maybe there is a BUG or aproblem on how to use correctly the DISTINCT CLAUSE in neo4j.rb

pdipietro commented 9 years ago

image

subvertallchris commented 9 years ago

Looks like a goofy feature of this gem. You can see it clearly at https://github.com/neo4jrb/neo4j-will_paginate_redux/blob/master/lib/neo4j-will_paginate_redux.rb#L48.

It doesn't respect String return values. If you do it as a symbol, it does work, though.

@users = Neo4j::Session.query.match(query_string).proxy_as(User, :user).paginate(:page => first_page, :per_page => secondary_items_per_page, return: :"DISTINCT user",order: "user.last_name asc, user.first_name  asc")

I expect that to work. In my test app with three Students connected to the same Lesson, this works:

Student.all.lessons(:l).paginate(page: 1, per_page: 2, return: :'distinct l')

We should add a case to accept a String, I don't know know why it wasn't there in the first place.