首页 > 解决方案 > 如何使用 gremlin 和 python 在海王星中进行组合布尔运算?

问题描述

我正在尝试做一个查询,我有多个布尔运算要完成,但不知道该怎么做。

查询类似于(A and B) or (C and D)

我第一次尝试

g.V()\
.has("attra", P.lte(20))\
.has("attrb", P.gte(10))\
.or_()\
.has("attrc", P.lte(20))\
.has("attrd", P.gte(10))

但事实证明,or_()在查询中的ored 之后的任何内容都不是我想要的。因为我还有其他复杂的布尔逻辑。

我也试过

g.V()\
.or_(
  has("attra", P.lte(20)).and().has("attrb", P.gte(10)),
  has("attrc", P.lte(20)).and().has("attrd", P.gte(10))
)

但它说has没有定义。你是这样做的吗?这甚至在哪里定义?

非常感谢您的帮助

编辑:我的文件中有以下导入

from __future__ import print_function
from gremlin_python.structure.graph import Graph
from gremlin_python.process.strategies import *
from gremlin_python.process.traversal import *
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
from gremlin_python.process.graph_traversal import __ as AnonymousTraversal

标签: pythongremlinamazon-neptune

解决方案


我找到了has定义的地方。

在这里能找到它

from gremlin_python.process.graph_traversal import __ as AnonymousTraversal

所以我们就这样做

has = AnonymousTraversal.has

并像这样查询

g.V()\
.or_(
  has("attra", P.lte(20)).and().has("attrb", P.gte(10)),
  has("attrc", P.lte(20)).and().has("attrd", P.gte(10))
)

推荐阅读