【发布时间】:2022-01-17 20:45:15
【问题描述】:
在 AWS Neptune 中,我试图通过计算余弦相似度来创建用户顶点之间的相似边,如 Daniel Kuppitz 此处所述——https://gist.github.com/dkuppitz/79e0b009f0c9ae87db5a#file-cosim-groovy-L368——gremlin 的 sideEffect 提供了一个有用的闭包,它允许进行一些数学运算获取相似度分数并将此值写入每个“相似度”边缘。唉,海王星不支持副作用。我正在寻找一种在不使用 sideEffect 的情况下在单个 gremlin 查询中运行以下示例中的注释部分的方法。感谢您的帮助!
g.V().match(
__.as("u1").outE("rated").as("r1"),
__.as("r1").inV().as("m"),
__.as("m").inE("rated").as("r2"),
__.as("r2").outV().as("u2")
).where("u1", neq("u2")).
group().by(select("u1","u2")).
by(select("r1","r2").by("rating")).
unfold().
as("kv").
select(keys).
addE("similarity").from("u1").to("u2").as("e").
// sideEffect {
// def r = it.get("kv").getValue()
// def xyDotProduct = r.collect {it.r1*it.r2}.sum()
// def xLength = Math.sqrt(r.collect {it.r1*it.r1}.sum())
// def yLength = Math.sqrt(r.collect {it.r2*it.r2}.sum())
// def similarity = xyDotProduct / (xLength * yLength)
// it.get().property("similarity", similarity)
// }.iterate()
【问题讨论】:
标签: graph gremlin amazon-neptune