我目前正在使用 TinkerPop Gremlin(带有 Titan 后端)来实现“相似文档”算法。
接下来的几行在 gremlin shell 中运行得很好:
v = g.v(880068)
m=[:]
v.as('x').out('auto_tag').in('auto_tag').has('status', 1).except('x').groupCount(m).filter{false}
results=[]
m.sort{-it.value}[0..9].each(){key, value -> results.add(key.document_id) }
results
以下结果可见:
==>3188749
==>3190640
==>3191407
==>3187753
==>3186634
==>3185534
==>3189883
==>3190108
==>3187088
==>3188890
但是当我尝试将相同的代码“包装”在函数中时,它不再起作用:
v = g.v(880068)
def get_similar_documents(v) {
m=[:]
v.as('x').out('auto_tag').in('auto_tag').has('status', 1).except('x').groupCount(m).filter{false}
results=[]
m.sort{-it.value}[0..9].each(){key, value -> results.add(key.document_id) }
return results
}
get_similar_documents(v)
...没有返回任何内容
来自 Python 后端,我认为这与变量范围有关,但到目前为止我不明白如何修复它。
预先感谢您的帮助
编辑:我正在使用 Bulbs,这就是为什么我想将我的代码包装在一个函数中(我稍后可以从 Python 调用)
最佳答案
我认为您需要在 get_similar_documents
函数中迭代
您的管道。含义:
v.as('x').out('auto_tag').in('auto_tag').has('status', 1).except('x').groupCount(m).filter{false}.iterate()
请务必记住,Gremlin Shell 会自动为您迭代管道。 shell 不会在函数内迭代它,因此不会对 groupCount
中的 m
产生副作用。
您可以在那里阅读更多相关信息here 。
关于groovy - 如何在 gremlin 中定义返回与 gremlin shell 中相同结果的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20975252/