Skip to content

Query API

Marcus Klemm edited this page Aug 10, 2018 · 14 revisions

class graphit.EESQuery

Create complex ElasticSearch queries. Returns a graphit.EESQuery instance that can be passed to graphit.GraphitSession.query() or used as a sub–clause in another query.

Public API

__init__(*clauses, operation='AND')

Parameters:
  • *clauses: Arbitrary number of query clauses, either a str containing a valid ElasticSearch query string or another instance of EESQuery.
  • operation: The logical operation by that the clauses should be linked, one of "AND" (default), "OR" or "NOT" (in case of multiple clause, "NOT" will be translated to NOT ( clause1 OR clause2 OR ...))

append(*clauses)

Add additional clauses to an existing Query.

Parameters:
  • *clauses: Arbitrary number of query clauses, either a str containing a valid ElasticSearch query string or another instance of EESQuery.

    ⚠️ Opposed to the direct use of graphit.GraphitSession.query(), there is no need to escape characters like /, in fact that would lead to an invalid query.

clear()

Remove all clauses from an existing Query.

__str__()

Render the EESQuery object, returns the final query string as str, so it can be passed to the query method of a GraphitSession

class graphit.GremlinQuery

Create Gremlin queries. Returns a graphit.GremlinQuery instance that can be passed to graphit.GraphitSession.query().

Public API

__init__(root, query=None)

Parameters:
  • root: Starting point of the graph traversal, a str containing a valid ogit/_id.
  • query: A str containing a valid Gremlin query string.

class graphit.VerbQuery

Create a simple "verb" query that returns the nodes that are connected to a certain node by a specific verb. Returns a graphit.VerbQuery instance that can be passed to graphit.GraphitSession.query().

Public API

__init__(root, verb, ogit_types=None, direction='both')

Parameters:
  • root: Starting point of the query, a str containing a valid ogit/_id.
  • verb: A strcontaining a valid ogit/_type for verbs/edges.
  • ogit_types: A list (or other iterable) of str containing a valid ogit/_type for nodes/vertices. Limits the result set to nodes of these types.
  • direction: One of "in", "out" or "both" (default), return only nodes that are connected by incoming or outgoing edges or return all connected nodes, respectively.

class graphit.IDQuery

Create a Multi-ID query. Returns a graphit.IDQuery instance that can be passed to graphit.GraphitSession.query().

Public API

__init__(*node_ids)

Parameters:
  • *node_ids: Arbitrary number of str containing an ogit/_id.

Examples

Build and execute an ElasticSearch query

q1 = graphit.EESQuery(
  'ogit/_type:"ogit/BusinessProcess/DataObject"',
  'ogit/_owner:"arago.de"'
)
print(q1)

( ogit\/_type:"ogit\/BusinessProcess\/DataObject" AND ogit\/_owner:"arago.de" )

q1.append('ogit/_modified-on:[now-7d TO now]')
print(q1)

( ogit\/_type:"ogit\/BusinessProcess\/DataObject" AND ogit\/_owner:"arago.de" AND ogit\/_modified\-on:[now\-7d TO now] )

q2 = graphit.EESQuery('ogit/_id:"cjkmq4st02xseyd33qkutr6ut"', q1, operation='OR')
print(q2)

( ogit\/_id:"cjkmq4st02xseyd33qkutr6ut" OR ( ogit\/_type:"ogit\/BusinessProcess\/DataObject" AND ogit\/_owner:"arago.de" AND ogit\/_modified\-on:[now\-7d TO now] ) )

for item in session.query(q2, limit=1000, fields=['ogit/_id', '/MyCustomAttribute']):
  do_something(item)

Run a Gremlin query

q3 = graphit.GremlinQuery(
  root="cjkmq4st02xseyd33qkutr6ut",
  query="inE('ogit/uses').has('ogit/_out-type','ogit/BusinessProcess/DataObject').outV()"
)

for item in session.query(q3):
  do_something(item)

Run a Verb query

q4 = graphit.VerbQuery(
  root="cjkmq4st02xseyd33qkutr6ut",
  verb="ogit/uses",
  ogit_types=["ogit/Business/DataObject"],
  direction="in"
)

for item in session.query(q4):
  do_something(item)