简体   繁体   English

如果Cypher查询中的属性为Null,则跳过其他属性的关系创建

[英]Relationship Creation for other properties skipped if property is Null in Cypher query

I have a cypher query for creating nodes in batch. 我有一个用于批量创建节点的密码查询。

Query 询问

UNWIND {batch} as row MERGE(m:TempEncounter {encounterId: row.encounterId}) ON CREATE SET m+=row
WITH m,row MATCH (u:Users {userId:  row.creator }) MERGE (m)-[:USERS]->(u)
WITH m,row MATCH (l:Location {locationId:  row.locationId }) MERGE (m)-[:LOCATION]->(l)
WITH m,row MATCH (p:Patient {patientId:  row.patientId }) MERGE (m)-[:PATIENT]->(p)
WITH m,row MATCH (e:EncounterType {encounterTypeId:  row.encounterType }) MERGE (m)-[:ENCOUNTERTYPE]->(e)
WITH m,row MATCH (u2:Users {userId:  row.voidedBy }) MERGE (m)-[:USERS]->(u2)
return m;

The problem with above cypher query is that suppose locationId is NULL for row1 (but may not for other rows) then relationship starting from LOCATION to USERS would not be created. 上面的密码查询的问题是,假设row1的locationId为NULL(但其他行可能不这样),那么将不会创建从LOCATION到USERS的关系。 Although other properties may present like(voidedBy, enounterType) 尽管可能会出现其他属性,例如(voidedBy,enounterType)

How can I improve my cypher query so that it would skip making a particular relationship if not present in row but make others which are present in row. 如何改进我的密码查询,以便在不存在于行中的情况下跳过建立特定关系,而在行中存在其他关系。

I am using this query for increment my nodes in neo4j. 我正在使用此查询来增加我的neo4j中的节点。 Performance is required here 这里需要性能

I don't know if this query will be more performant or not, but the way to do what you want is the following : 我不知道此查询是否会更高效,但是执行所需操作的方法如下:

UNWIND {batch} as row 
  MERGE(m:TempEncounter {encounterId: row.encounterId}) 
    ON CREATE SET m+=row
  WITH m,row 
    OPTIONAL MATCH (u:Users {userId:  row.creator })
    FOREACH( node IN filter(x IN [u] WHERE x IS NOT NULL) |
      MERGE (m)-[:USERS]->(node)
    )
  WITH m, row
    OPTIONAL MATCH (l:Location {locationId:  row.locationId })
    FOREACH( node IN filter(x IN [l] WHERE x IS NOT NULL) |
      MERGE (m)-[:LOCATION]->(node)
    )
  ...

If this query is not enought performant, you should try to split this query in many parts (one for each relationship), and do the batch in my code by managing manually my transaction's commit. 如果此查询的性能不足,则应尝试将此查询分为多个部分(每个关系一个),并通过手动管理事务的提交来对代码进行批处理。

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

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