我已经从SQL跳到了Neo4j。我有一些复杂的关系需要在节点上设置为属性,作为构建推荐引擎的第一步。
此Cypher查询返回类别和权重列表。
MATCH (m:Movie {name: "The Matrix"})<-[:TAKEN_FROM]-(i:Image)-[r:CLASSIFIED_AS]->(c:Category) RETURN c.name, avg(r.weight)
返回
{ "fighting": 0.334, "looking moody": 0.250, "lying down": 0.237 }
如何将这些结果设置为父节点上的键值对?
期望的结果是:
(m:Movie { "name": "The Matrix", "fighting": 0.334, "looking moody": 0.250, "lying down": 0.237 })
另外,我假设我应该批量处理我的(m:Movie)节点,那么实现这个的最佳方法是什么?
答案 0 :(得分:1)
不太确定如何获得该输出,该返回不应该将它们作为键值对返回。相反,我会期望类似:{"c.name":"fighting", "avg(r.weight)":0.334}
,每对都有单独的记录。
您可能需要APOC procedures,因为您需要一种方法将属性键设置为类别名称的值。这有点棘手,但你可以通过从收集的对中创建一个地图,然后使用SET + +来更新相关属性:
MATCH (m:Movie {name: "The Matrix"})<-[:TAKEN_FROM]-(:Image)-[r:CLASSIFIED_AS]->(c:Category)
WITH m, c.name as name, avg(r.weight) as weight
WITH m, collect([name, weight]) as category
WITH m, apoc.map.fromPairs(category) as categories
SET m += categories
就批处理而言,看一下apoc.periodic.iterate()
,它将允许您迭代外部查询的流结果并对流的批次执行内部查询:
CALL apoc.periodic.iterate(
"MATCH (m:Movie)
RETURN m",
"MATCH (m)<-[:TAKEN_FROM]-(:Image)-[r:CLASSIFIED_AS]->(c:Category)
WITH m, c.name as name, avg(r.weight) as weight
WITH m, collect([name, weight]) as category
WITH m, apoc.map.fromPairs(category) as categories
SET m += categories",
{iterateList:true, parallel:false}) YIELD total, batches, errorMessages
RETURN total, batches, errorMessages