温馨提示:本文翻译自stackoverflow.com,查看原文请点击:tinkerpop - Gremlin identifying subsets of populations based on vertices that only have in or out edges
amazon-neptune gremlin gremlinpython tinkerpop

tinkerpop - Gremlin根据仅具有边或边的顶点来识别总体子集

发布于 2020-04-29 09:11:54

我有一个具有功能的用户的gremlin图。图形的边缘从用户那里出来并输入要素。用户没有传入的边缘,要素也没有传出的边缘。每个用户顶点都有许多打出的边进入要素顶点。

I want to find the subset of female users who are connected to feature_a and feature_b vertices. I am using gremlinpython and I know I can do some kind of set intersection in python with the code below. Is there a way in gremlin to achieve this?

q = '''g.V('feature_a').
        inv().has('gender','Female')'''

res1 = client.submit(q).all().result()

q2 = '''g.V('feature_b').
        inv().has('gender','Female')'''

res2 = client.submit(q2).all().result()


vs_in_common = set(res2).intersection(set(res1)))

查看更多

提问者
Justin Gerard
被浏览
22
Daniel Kuppitz 2020-02-11 22:28

What Michael posted works, but it's a full scan across all users in the graph (unless gender is indexed, but this would cause other issues, sooner or later). You should rather do something like the following:

g.V().hasId('feature_a').
  in().has('gender', 'Female').
  filter(out().hasId('feature_b'))

Also, if possible, provide an edge label in the in and out steps.