0

I am trying to do a query where I have multiple boolean operations to be done but can't figure out how to do it.

The query is something like (A and B) or (C and D)

I first tried

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

but it turns out anything after the or_() in the query is ored and that is not what I want. Because I have other complex boolean logic down the line as well.

and I also tried

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))
)

but it says that has is not defined. Is this how you do it? Where is this has even defined?

Help would be really appreciated

EDIT: I have the following imports in my file

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
Prasanna
  • 4,125
  • 18
  • 41

1 Answers1

1

I found where the has is defined.

It can be found here

from gremlin_python.process.graph_traversal import __ as AnonymousTraversal

so we just do

has = AnonymousTraversal.has

and query like this

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))
)
Prasanna
  • 4,125
  • 18
  • 41
  • 1
    Glad you got it working. There are a few ways you can handle the imports with Gremlin Python. One way is to add things to globals as statics. Another way is to just prefix any anonymous traversal with __. That way you will not have to explicitly assign each member of the AnonymousTraversal class (there are a lot of them). – Kelvin Lawrence Sep 26 '20 at 21:45
  • thanks for the advice. There actually are a lot of them :D I realized it as well after working with it for a while. And I really like the prefixing idea as well. Thank you again – Prasanna Sep 28 '20 at 21:15