简体   繁体   English

Neo4j Cypher模式理解

[英]Neo4j Cypher pattern comprehension

This is my Neo4j Sandbox for query tests: 这是我的Neo4j Sandbox用于查询测试:

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: 我有以下Cypher查询:

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): 正确返回以下数据(1条记录):

{
  "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: valuedCharacteristics偶然包含2个值:

{
  "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)? 以及如何解决该问题以便返回正确的单个记录(因为将值#2设置为另一个父代(id = 2)的特征,因此此处不应该显示该值)?

You should pass parentD to the next context in the WITH . 您应该将parentD传递给WITH的下一个上下文。 Since you're not doing this, the parentD variable used on the pattern comprehension is not binded to parentD.id = 1 . 由于您未执行此操作,因此模式理解中使用的parentD变量未绑定到parentD.id = 1 So parentD is MATCH ing all possible nodes. 因此parentD正在MATCH所有可能的节点。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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