简体   繁体   中英

Neo4j Cypher pattern comprehension and conditions

In my Cypher query I have a following pattern comprehension:

[ (parentD)<-[:DEFINED_BY]-(ch1:Characteristic)<-[:SET_ON]-(v1:Value)-[:SET_FOR]->(childD) | 
  {characteristicId: id(ch1),  value: v1.value, valueType: ch1.valueType, visualMode: ch1.visualMode} ] AS valuedCharacteristics

I have added parentCharacteristic to my SDN 4 Characteristic entity:

@NodeEntity
public class Characteristic extends Votable {

    private final static String DEPENDS_ON = "DEPENDS_ON";

    @Relationship(type = DEPENDS_ON, direction = Relationship.OUTGOING)
    private Characteristic parentCharacteristic;

...

}

Right now I need to extend my pattern comprehension and add a conditions in order to return the same Characteristic set as previously except those who have parentCharacteristic != NULL and pattern comprehension should also return Characteristic that have ID in the {includeCharacteristicIds} collection that I'll provide to this query as parameter.

In order to avoid all Characteristic without child Characteristic I have added following condition:

WHERE NOT ((ch1)<-[:DEPENDS_ON]-())

So the full pattern comprehension now looks like:

[ (parentD)<-[:DEFINED_BY]-(ch1:Characteristic)<-[:SET_ON]-(v1:Value)-[:SET_FOR]->(childD) WHERE NOT ((ch1)<-[:DEPENDS_ON]-()) | 
  {characteristicId: id(ch1),  value: v1.value, valueType: ch1.valueType, visualMode: ch1.visualMode} ] AS valuedCharacteristics

but how also in additional to this Characteristic list return Characteristic that have ID in the {includeCharacteristicIds} collection ?

Please help to extend this query.

You can just combine the two conditions with an OR statement like this...

WHERE NOT ((ch1)<-[:DEPENDS_ON]-()) OR id(ch1) IN myIDs

The WHERE in a pattern comprehension works just like a WHERE for a MATCH condition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM