DotNet4Neo4j / Neo4jClient

.NET client binding for Neo4j
https://www.nuget.org/packages/Neo4jClient
Microsoft Public License
431 stars 146 forks source link

Multiple Limit/order by in Cypher #447

Closed flieks closed 1 year ago

flieks commented 2 years ago

What i want to achieve is this RAW cypher with Neo4jClient:

MATCH (artist:Artist)<-[similarRelation:SIMILAR]-(artist2:Artist)<-[:ON]-(music:Music)
WITH music, similarRelation LIMIT 1
WITH music, COUNT(similarRelation) as countRelation ORDER BY countRelation DESC
WITH music, countRelation
......
LIMIT 10

But adding this in a with does not work .With("music, similarRelation LIMIT 1") or .With("music, similarRelation LIMIT 1 as similarRelationWithLimit")

i see someone also posted this on stackoverflow a long time ago.

cskardon commented 2 years ago

I don't know what the problem is, the code you've tried generates what you're after.

var q = new CypherFluentQuery(client)
    .Match("(artist:Artist)<-[similarRelation:SIMILAR]-(artist2:Artist)<-[:ON]-(music:Music)")
    .With("music, similarRelation LIMIT 1")
    .With ("music, COUNT(similarRelation) as countRelation ORDER BY countRelation DESC")
    .With ("music, countRelation");

q.Query.DebugQueryText.Dump();

Generates:

MATCH (artist:Artist)<-[similarRelation:SIMILAR]-(artist2:Artist)<-[:ON]-(music:Music)
WITH music, similarRelation LIMIT 1
WITH music, COUNT(similarRelation) as countRelation ORDER BY countRelation DESC
WITH music, countRelation

Or to match what the StackOverflow post was talking about, you could do this:

var q = new CypherFluentQuery(client)
    .Match("(artist:Artist)<-[similarRelation:SIMILAR]-(artist2:Artist)<-[:ON]-(music:Music)")
    .With("music, similarRelation")
    .Limit(1)
    .With ("music, COUNT(similarRelation) as countRelation ORDER BY countRelation DESC")
    .With ("music, countRelation");

q.Query.DebugQueryText.Dump();

and you get:

MATCH (artist:Artist)<-[similarRelation:SIMILAR]-(artist2:Artist)<-[:ON]-(music:Music)
WITH music, similarRelation
LIMIT 1
WITH music, COUNT(similarRelation) as countRelation ORDER BY countRelation DESC
WITH music, countRelation

Which is also what you wanted (albeit with an extra CR/LF there)

What problem are you actually getting?

cskardon commented 1 year ago

No response. Closing.