neo4jcypher-dsl

Neo4j CypherDSL pattern comprehension with Java


I have the query below. I want to know how to generate same query with cypher-dsl.

MATCH (m:Movie) WHERE m.name = "star"
RETURN DISTINCT m,
  [(m)<-[:HAS_RELATION]-(n:AnotherNode) | n] as othersnodes

I could not find a Java API for this.


Solution

  • A little bit verbose, but I think this way it is better understandable.

    var movieNode = Cypher.node("Movie").named("m");
    var anotherNode = Cypher.node("AnotherNode").named("n");
    var condition = movieNode.property("name").eq(Cypher.literalOf("star"));
    var patternComprehension = Cypher.listBasedOn(movieNode.relationshipFrom(anotherNode, "HAS_RELATION")).returning(anotherNode);
    var statement = Cypher.match(movieNode)
        .where(condition)
        .returningDistinct(movieNode.getRequiredSymbolicName(), patternComprehension.as("othersnodes"));