【问题标题】:How to define function in gremlin that returns the same result as in gremlin shell?如何在 gremlin 中定义返回与 gremlin shell 中相同结果的函数?
【发布时间】:2014-01-25 08:23:13
【问题描述】:

我目前正在使用 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 后端,我认为这与变量范围有关,但到目前为止我不明白如何修复它。

提前感谢您的帮助

编辑:我正在使用灯泡,这就是为什么我想将我的代码包装在一个函数中(我以后可以从 Python 调用)

【问题讨论】:

    标签: groovy gremlin titan bulbs


    【解决方案1】:

    我认为您需要在 get_similar_documents 函数中 iterate 您的管道。含义:

    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 阅读更多信息。

    【讨论】:

    • 非常感谢,斯蒂芬,它有效 :) 我已经看到了这个“技巧”,但我仍然缺少练习,所以忘记了 ;(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 2023-01-15
    相关资源
    最近更新 更多