Neo4j Cypher使用APOC更改过滤操作apoc.index.relationships

时间:2018-05-07 08:01:07

标签: neo4j cypher neo4j-apoc

我有以下查询:

MATCH (dg:DecisionGroup)-[:CONTAINS]->(childD:Decision) 
WHERE dg.id = {decisionGroupId} 
MATCH (childD)-[relationshipValueRel1:HAS_VALUE_ON]-(filterCharacteristic1:Characteristic) 
MATCH (dg)<-[:DEFINED_BY]-(:CharacteristicGroup)-[characteristicRel1:CONTAINS]->(filterCharacteristic1) 
WHERE characteristicRel1.hidden <> true AND filterCharacteristic1.id = 1 
WITH relationshipValueRel1, childD, dg 
WHERE   
    ({relationshipValueRel11}[0] <= relationshipValueRel1.value <=  {relationshipValueRel11}[1] )  
WITH childD, dg  
MATCH (childD)-[relationshipValueRel2:HAS_VALUE_ON]-(filterCharacteristic2:Characteristic) 
MATCH (dg)<-[:DEFINED_BY]-(:CharacteristicGroup)-[characteristicRel2:CONTAINS]->(filterCharacteristic2) 
WHERE characteristicRel2.hidden <> true AND filterCharacteristic2.id = 2
WITH relationshipValueRel2, childD, dg 
WHERE   
    (ANY (id IN {relationshipValueRel22} WHERE id IN relationshipValueRel2.value ))  
        AND  (ANY (id IN [1] WHERE id IN relationshipValueRel2.optionIds ))  
            AND  ( relationshipValueRel2.`property.1.4` = {property4} AND relationshipValueRel2.`property.1.4` = {property5}  )  
WITH childD , dg  
ORDER BY childD.createDate ASC 
SKIP 0 LIMIT 100 
WITH * MATCH (childD)-[ru:CREATED_BY]->(u:User) 
OPTIONAL MATCH (childD)-[rup:UPDATED_BY]->(up:User)  
RETURN ru, u, rup, up, childD AS decision, 
[ (dg)<-[:DEFINED_BY]-(entityGroup)-[:CONTAINS]->(entity)<-[:COMMENTED_ON]-(comg:CommentGroup)-[:COMMENTED_FOR]->(childD)
 | {entityId: toInt(entity.id),  types: labels(entity), totalComments: toInt(comg.totalComments)} ] AS commentGroups, 
[ (c1)<-[vg1:HAS_VOTE_ON]-(childD)
 | {criterionId: toInt(c1.id),  weight: vg1.avgVotesWeight, totalVotes: toInt(vg1.totalVotes)} ] AS weightedCriteria, 
[ (dg)<-[:DEFINED_BY]-(chg:CharacteristicGroup)-[rchgch1:CONTAINS]->(ch1:Characteristic)<-[v1:HAS_VALUE_ON]-(childD)  WHERE  rchgch1.hidden <> true 
 | {characteristicId: toInt(ch1.id),  v1:v1, optionIds: v1.optionIds, valueIds: v1.valueIds, value: v1.value, available: v1.available, totalHistoryValues: v1.totalHistoryValues, totalFlags: v1.totalFlags, description: v1.description, valueType: ch1.valueType, visualMode: ch1.visualMode} ] AS valuedCharacteristics

我想提高以下过滤操作的效果:

({relationshipValueRel11}[0] <= relationshipValueRel1.value <=  {relationshipValueRel11}[1] )

(ANY (id IN {relationshipValueRel22} WHERE id IN relationshipValueRel2.value ))  
    AND  (ANY (id IN [1] WHERE id IN relationshipValueRel2.optionIds ))  
        AND  ( relationshipValueRel2.`property.1.4` = {property4} AND relationshipValueRel2.`property.1.4` = {property5}  )

为了做到这一点 - 我创建了以下APOC触发器,以便将所有HAS_VALUE_ON关系属性移动到索引中:

CALL apoc.trigger.add('RELATIONSHIP_INDEX_ADD_HAS_VALUE_ON', 'UNWIND {createdRelationships} AS r MATCH (d:Decision)-[r:HAS_VALUE_ON]->(ch:Characteristic) CALL apoc.index.addRelationship(r, keys(r)) RETURN count(*)', {phase:'after'});

现在我想重写原始查询,以便用

替换过滤操作
CALL apoc.index.relationships('HAS_VALUE_ON', ...

另外,我不明白如何在此处添加以下条件filterCharacteristic1.id = 1filterCharacteristic1.id = 2。我为此尝试了以下查询:

CALL apoc.index.relationships('HAS_VALUE_ON', 'property.1.4:"1"') YIELD start as childD, end as ch MATCH (childD)-[:HAS_VALUE_ON]-(ch) WHERE ch.id IN [1,2]

但它失败并出现以下异常:

Neo.ClientError.Statement.SyntaxError: Query cannot conclude with MATCH (must be RETURN or an update clause) (line 1, column 100 (offset: 99))
"CALL apoc.index.relationships('HAS_VALUE_ON', 'property.1.4:"1"') YIELD start as childD, end as ch MATCH (childD)-[:HAS_VALUE_ON]-(ch) WHERE ch.id IN [1,2]"

你能说明一下如何做到吗?

0 个答案:

没有答案