All Collections
Data Analysis
Gremlin training
Gremlin Training 2: Understanding Graph Queries
Gremlin Training 2: Understanding Graph Queries

Learn about filters and traversal steps, the two types of graph query steps in Ardoq which can extract any knowledge. Written by Kristine Marhilevica
Updated over a week ago

Now that we have established a basic understanding of what a graph is, and how to start a graph query, we will explain how we can build upon this to create queries which can extract any knowledge we might be looking for.

We have already established that we start queries by selecting a graph to work with (the only graphs of relevance in Ardoq are g and gxp), and whether we want to start with a selection of vertices or a selection of edges. A graph query consists of a chain of steps which modifies the initial selection of vertices or edges. The steps in the query do not modify the actual vertices or edges, but rather which vertices or edges are selected. Furthermore, the query can modify the way these vertices or edges are presented.

Filters

Filter steps are steps which removes elements from the selection which do not fulfill some criteria. Even if an element is filtered away from the current selection, it is still available in the graph, so a later step in the query will still be able to access elements which are filtered away.

A typical filter step is the hasLabel()-step which is used to filter the current selection of components and references based on their type. The following query finds all vertices and then filters away all whose type is not "Coffee"

`g.V().hasLabel('Coffee')`

Another way to modify the current selection is through traversal steps, i.e. changing the current selection of elements to another selection of elements, by following (traversing) edges. Note that when doing a traversal step, we don't just add more elements to our current selection of elements. Instead, we replace the current selection of elements with a new selection of elements. In technical terms, a step like this is called a flat map step.

Traversal steps

Traversal steps are steps which change the current selection of elements to another selection of elements by following (traversing) edges. When doing a traversal step, we don't just add more elements to our current selection of elements. Instead, we replace the current selection of elements with a new selection of elements. In technical terms, a step like this is called a flat map step.

Using a traversal step, we can for instance change a selection of vertices to all vertices which are reachable through outgoing edges from the current selection of vertices. Then vertices which have no outgoing edges will map to nothing, and vertices which have multiple outgoing edges will map to multiple vertices. After a traversal step, the number of elements in the selection may be different from the number of elements before the traversal step.

An example of a traversal step is the out()-step, which changes a selection of vertices to all the vertices reached by following outgoing edges. The following query returns all components which are connected by outgoing references from components of type "Person".

`g.V().hasLabel('Person').out()`

Map steps

Map steps are steps which change how the elements in our current selection are represented by mapping each element in the current selection to exactly one value. After a map step, the number of elements in the selection is equal to the number of elements before the map step.

A typical map step is the label()-step which maps each vertex or edge to a string denoting its type. The following query finds all the component types in the organization.

`g.V().label()`

The number of results is the same after running the label()-step as before running the label()-step, so if multiple components are of the same type, there will be multiple equal results.

Aggregation steps

Aggregation steps are steps which aggregate a selection of multiple elements or multiple values to a single value.

A typical example of an aggregation step is the count()-step. The following query counts the number of Person components in the organization.

`g.V().hasLabel('Person').count()`