简体   繁体   中英

Neo4j Cypher pattern comprehension

This is my Neo4j Sandbox for query tests:

Neo4j Browser: https://10-0-1-223-34371.neo4jsandbox.com/

Direct Neo4j HTTP: http://54.89.60.35:34371/browser/

Username: neo4j
Password: capacities-complement-deputies
IP Address: 54.89.60.35
HTTP Port: 34371
Bolt Port: 34370

I have a following Cypher query:

MATCH (parentD)<-[:DEFINED_BY]-(ch1:Characteristic)<-[v1:HAS_VALUE_ON]-(childD)  
WHERE NOT (ch1)<-[:DEPENDS_ON]-() AND parentD.id = 1 
RETURN v1

which correctly returns the following data(1 record):

{
  "totalHistoryValues": 0,
  "available": true,
  "description": "Integer value 1",
  "value": 10
}

but inside of the following query as pattern comprehension:

MATCH (parentD)-[:CONTAINS]->(childD:Decision) 
WHERE parentD.id = 1 
WITH * MATCH (childD)-[ru:CREATED_BY]->(u:User) 
OPTIONAL MATCH (childD)-[rup:UPDATED_BY]->(up:User)  
WITH ru, u, rup, up, childD  
SKIP 0 LIMIT 100 
RETURN ru, u, rup, up, childD AS decision, 
[ (parentD)<-[:DEFINED_BY]-(entity)<-[:COMMENTED_ON]-(comg:CommentGroup)-[:COMMENTED_FOR]->(childD)
 | {entityId: toInt(entity.id),  types: labels(entity), totalComments: toInt(comg.totalComments)} ] AS commentGroups, 
[ (parentD)<-[:DEFINED_BY]-(c1)<-[vg1:HAS_VOTE_ON]-(childD)
 | {criterionId: toInt(c1.id),  weight: vg1.avgVotesWeight, totalVotes: toInt(vg1.totalVotes)} ] AS weightedCriteria, 
[ (parentD)<-[:DEFINED_BY]-(ch1:Characteristic)<-[v1:HAS_VALUE_ON]-(childD)  WHERE  NOT ((ch1)<-[:DEPENDS_ON]-()) 
 | {characteristicId: toInt(ch1.id),  value: v1.value, available: v1.available, totalHistoryValues: v1.totalHistoryValues, description: v1.description, valueType: ch1.valueType, visualMode: ch1.visualMode} ] AS valuedCharacteristics, 
[ (childD)-[rdt:BELONGS_TO]->(t:Tag)
 | t ] AS tags

valuedCharacteristics accidentally contains 2 values:

{
  "totalHistoryValues": 0,
  "description": "Integer value 2",
  "valueType": "INTEGER",
  "characteristicId": 2,
  "available": true,
  "visualMode": "INTEGERRANGESLIDER",
  "value": 20
}

,

{
  "totalHistoryValues": 0,
  "description": "Integer value 1",
  "valueType": "INTEGER",
  "characteristicId": 1,
  "available": true,
  "visualMode": "INTEGERRANGESLIDER",
  "value": 10
}

what is wrong with this part of query:

 [ (parentD)<-[:DEFINED_BY]-(ch1:Characteristic)<-[v1:HAS_VALUE_ON]-(childD)  WHERE  NOT ((ch1)<-[:DEPENDS_ON]-()) 
     | {characteristicId: toInt(ch1.id),  value: v1.value, available: v1.available, totalHistoryValues: v1.totalHistoryValues, description: v1.description, valueType: ch1.valueType, visualMode: ch1.visualMode} ] AS valuedCharacteristics

and how to fix it in order to return a correct single record (because value#2 is set to characteristic from another parent(id=2)and should not be present here)?

You should pass parentD to the next context in the WITH . Since you're not doing this, the parentD variable used on the pattern comprehension is not binded to parentD.id = 1 . So parentD is MATCH ing all possible nodes. Change your query to:

MATCH (parentD)-[:CONTAINS]->(childD:Decision) 
WHERE parentD.id = 1 
WITH * MATCH (childD)-[ru:CREATED_BY]->(u:User) 
OPTIONAL MATCH (childD)-[rup:UPDATED_BY]->(up:User)  
WITH ru, u, rup, up, childD, parentD  
SKIP 0 LIMIT 100 
RETURN ru, u, rup, up, childD AS decision, 
[ (parentD)<-[:DEFINED_BY]-(entity)<-[:COMMENTED_ON]-(comg:CommentGroup)-[:COMMENTED_FOR]->(childD)
 | {entityId: toInt(entity.id),  types: labels(entity), totalComments: toInt(comg.totalComments)} ] AS commentGroups, 
[ (parentD)<-[:DEFINED_BY]-(c1)<-[vg1:HAS_VOTE_ON]-(childD)
 | {criterionId: toInt(c1.id),  weight: vg1.avgVotesWeight, totalVotes: toInt(vg1.totalVotes)} ] AS weightedCriteria, 
[ (parentD)<-[:DEFINED_BY]-(ch1:Characteristic)<-[v1:HAS_VALUE_ON]-(childD)  WHERE  NOT ((ch1)<-[:DEPENDS_ON]-()) 
 | {characteristicId: toInt(ch1.id),  value: v1.value, available: v1.available, totalHistoryValues: v1.totalHistoryValues, description: v1.description, valueType: ch1.valueType, visualMode: ch1.visualMode} ] AS valuedCharacteristics, 
[ (childD)-[rdt:BELONGS_TO]->(t:Tag)
 | t ] AS tags

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