cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Construct queries with condition

madiskou
Graph Buddy

Hello,
I am using spring data neo4j and i have a repository like this :

public interface MyNeo4jRepository extends Neo4jRepository<Object, Long> {

@Query("with ['X', 'Y','Z'] as list_labels, "
            + "$appsFilter as appsList\n "
            + "MATCH (apps:) where apps.n IN appsList "
            + "MATCH (a)<-[:event]-(nodes) "
            + "WHERE any(x IN labels(nodes) WHERE x IN list_labels) "
            + "CALL apoc.path.expandConfig(nodes, { "
            + "relationshipFilter: 'R1|R2>',"
            + "labelFilter: '-l1|>l2',"
            + "maxLevel: 6,"
            + "endNodes: [apps],"
            + "uniqueness: 'NODE_PATH'}) YIELD path "
            + "unwind nodes(path) as n  "
           ...
}

what i want is to create this query using conditions like this :

@Query("with ['X', 'Y','Z'] as list_labels, "
            + "$appsFilter as appsList\n "
            + "MATCH (apps:) where apps.n IN appsList "
            + "MATCH (a)<-[:event]-(nodes) "
            + "WHERE any(x IN labels(nodes) WHERE x IN list_labels) "
           if (condition) + "WHERE  ...." else + ""
            + "CALL apoc.path.expandConfig(nodes, { "
  ...

Thnak you

2 REPLIES 2

Bennu
Graph Fellow

Hi @madiskou !

I dont think you can do something like that . You may need to create two different custom queries and handle them on your service layer. Or you can always use apoc.when and let the db itself handle the condition.

Yeah I know it's not what you expected tho

H

You have various options when it comes to expression conditions: Conditional Cypher Execution - Knowledge Base : CASE..WHEN, FOREACH trick, APOC...