2

I'm currently using TinkerPop Gremlin (with a Titan backend) to implement a "similar document" algorithm.

The next lines are working perfectly well in 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

following results are visible :

==>3188749
==>3190640
==>3191407
==>3187753
==>3186634
==>3185534
==>3189883
==>3190108
==>3187088
==>3188890

But when I try to "wrap" the same code in a function, it doesn't work anymore :

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)

...nothing is returned

Coming from a Python backend, I assume this is related to variable scope but so far I don't understand how to fix it.

Thanks in advance for any help

Edit : I'm using Bulbs, that's why I'd like to wrap my code in a function (That I could later call from Python)

4

1 回答 1

2

我认为你需要在函数iterate内使用你的管道。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 不会在函数中对其进行迭代,因此不会对您mgroupCount.

你可以在这里阅读更多关于那里的信息。

于 2014-01-07T19:26:22.703 回答