继续以下问题Neo4j Cypher manual relationship index, APOC trigger and data duplication我创建了重现问题的场景:
modifyIORef'
现在,让我们尝试选择CALL apoc.trigger.add('TEST_TRIGGER', "UNWIND keys({assignedRelationshipProperties}) AS key
UNWIND {assignedRelationshipProperties}[key] AS map
WITH map
WHERE type(map.relationship) = 'LIVES_IN'
CALL apoc.index.addRelationship(map.relationship, keys(map.relationship))
RETURN count(*)", {phase:'before'})
CREATE (p:Person) SET p.id = 1 return p
CREATE (p:Person) SET p.id = 2 return p
CREATE (c:City) return c
MATCH (p:Person), (c:City) WHERE p.id = 1 CREATE (p)-[r:LIVES_IN]->(c) SET r.time = 10 RETURN type(r)
MATCH (p:Person), (c:City) WHERE p.id = 2 CREATE (p)-[r:LIVES_IN]->(c) SET r.time = 20 RETURN type(r)
的人:
r.time = 10
上面的查询只能正确返回一个节点。
现在,让我们做同样的事情,但返回MATCH (p:Person)-[r:LIVES_IN]->(c:City)
CALL apoc.index.in(c, 'LIVES_IN', 'time:10') YIELD node AS person
RETURN person
计数:
person
上面的查询返回MATCH (p:Person)-[r:LIVES_IN]->(c:City)
CALL apoc.index.in(c, 'LIVES_IN', 'time:10') YIELD node AS person
RETURN count(person)
。
为什么此查询返回count = 2
而不是单个节点?
此外,还有以下查询:
count = 2
返回2个关系:
MATCH (p:Person)-[r:LIVES_IN]->(c:City)
CALL apoc.index.relationships('LIVES_IN', 'time:10') YIELD rel
RETURN rel
但我希望手动索引中只有一个{
"time": 10
}
{
"time": 10
}
。
我做错了什么?
答案 0 :(得分:1)
示例中的第一个查询也返回两行。显然你以图表的形式看结果。尝试或切换到表格模式,或更改此查询:
MATCH (p:Person)-[r:LIVES_IN]->(c:City)
CALL apoc.index.in(c, 'LIVES_IN', 'time:10') YIELD node AS person
RETURN ID(person)
获得了两行,因为您有两个人居住在同一个城市,并且您在索引上搜索每个关系。试试这个:
MATCH (p:Person)-[r:LIVES_IN]->(c:City)
WITH DISTINCT c
CALL apoc.index.in(c, 'LIVES_IN', 'time:10') YIELD node AS person
RETURN COUNT(DISTINCT person)
答案 1 :(得分:1)
只是你的查询示例有误,请使用:
CALL apoc.index.relationships('LIVES_IN', 'time:10') YIELD rel
RETURN rel
或者
MATCH (c:City)
CALL apoc.index.in(c, 'LIVES_IN', 'time:10') YIELD node AS person
RETURN count(person)