Steps H-N

Updated at:

has

  • Function: (filter) Filters and retains entities (vertices or edges) that meet specified conditions.

  • Note: Use filter() instead.

g("thinkerpop").E("1;2;3;4;5;6").hasLabel("created").has("weight", P.inside(0.0, 0.6))
==> {"label":"created","pk":"1","sk":"3","weight":0.4}
==> {"label":"created","pk":"4","sk":"3","weight":0.4}
==> {"label":"created","pk":"6","sk":"3","weight":0.2}
// Equivalent to
g("thinkerpop").E("1;2;3;4;5;6").hasLabel("created").filter("weight<=0.6 AND weight>=0.0")

hasKey

  • Function: (filter) Filters by field name, and retains the field names and values that meet the specified conditions.

  • Note: This step supports P as a filter condition.

// Get the location or name of a person
g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").properties().hasKey(P.within("location","name"))
==> {"name":"marko"}
==> {"name":"josh"}
==> {"name":"vadas"}
==> {"name":"peter"}

// Get the name of a person
g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").properties().hasKey("name")
==> {"name":"marko"}
==> {"name":"josh"}
==> {"name":"vadas"}
==> {"name":"peter"}

hasLabel

  • Function: (filter) Filters entities (vertices or edges) by label, and retains the entities that meet the specified conditions.

  • Note: When you access a graph, the first step to query an entity must use hasLabel() to specify the label. If you do not specify a label in subsequent traversal steps, all accessible labels are queried by default.

g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").union(outE("knows"),outE("created")).hasLabel("knows")
==> {"label":"knows","pk":"1","sk":"4","weight":1.0}
==> {"label":"knows","pk":"1","sk":"2","weight":0.5}

g("thinkerpop").V("1").hasLabel("person")
==> {"label":"person","age":29,"name":"marko","pk":"1"}

hasValue

  • Function: (filter) Filters by field value, and retains the field names and values that meet the specified conditions.

  • Note: This step supports P as a filter condition.

// The age of people older than 30
g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").properties("age").hasValue(P.gt(30))
==> {"age":32}
==> {"age":35}

identity

Function: (map) Returns the input object itself.

Note: A single step is a self-referential step that is common in step types such as branches.

g("thinkerpop").V("1").hasLabel("person").identity()
==> {"label":"person","age":29,"name":"marko","pk":"1"}

// Person 1 and their friends
g("thinkerpop").V("1").hasLabel("person").union(identity(),outE("knows").inV())
==> {"label":"person","age":29,"name":"marko","pk":"1"}
==> {"label":"person","age":32,"name":"josh","pk":"4"}
==> {"label":"person","age":27,"name":"vadas","pk":"2"}

indexQuery

  • Function: (modifier) Performs an inverted index query.

  • Format: For more information, see Inverted Index Query.

  • Note:

    • The parameter of V() can be empty only when it is modified by indexQuery().

    • This step is a custom extension developed for IGraph.

g("thinkerpop").V().hasLabel("software_index").indexQuery("{\"match\":{\"lang\":\"java\"}}")
==> {"label":"software_index","lang":"java","name":"ripple","pk":"5"}

inV

  • Function: Traverses from an edge to its associated incoming vertex.

  • Note:

    • Graph access method: Queries all associated vertices.

// By default, the skey of the input edge is used as the traversal field.
g("thinkerpop").E("1").hasLabel("knows").inV()
==> {"label":"person","age":32,"name":"josh","pk":"4"}
==> {"label":"person","age":27,"name":"vadas","pk":"2"}

g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").outE().inV()
==> {"label":"software","lang":"java","name":"lop","pk":"3"}
==> {"label":"software","lang":"java","name":"lop","pk":"3"}
==> {"label":"software","lang":"java","name":"lop","pk":"3"}
==> {"label":"software","lang":"java","name":"ripple","pk":"5"}
==> {"label":"person","age":32,"name":"josh","pk":"4"}
==> {"label":"person","age":27,"name":"vadas","pk":"2"}

is

  • Function: (filter) Filters numbers or strings. This step retains the numbers or strings that meet the filter condition and discards the rest.

  • Note:

    • This step supports P as a filter condition.

g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").values("name").is("marko")
==> "marko"

g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").where(values("name").is("marko"))
==> {"label":"person","age":29,"name":"marko","pk":"1"}

g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").where(outE("knows").count().is(2))
==> {"label":"person","age":29,"name":"marko","pk":"1"}

g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").where(values("age").is(P.inside(28,34)))
==> {"label":"person","age":29,"name":"marko","pk":"1"}
==> {"label":"person","age":32,"name":"josh","pk":"4"}

label

  • Function: (map) Returns the graph label of an entity (vertex or edge).

  • Note:

    • In graph access, this step returns the corresponding label of the entity in the graph.

g("thinkerpop").V("1").hasLabel("person").label()
==> "person"

g("thinkerpop").V("1").hasLabel("person").outE().label()
==> "created"
==> "knows"
==> "knows"

limit

  • Function: (filter) Truncates the results and retains the first NUM results.

  • Format: limit(NUM), where NUM is a uint32 integer. You must set NUM to a value less than 50,000.

  • Note

    • limit(NUM) is equivalent to range(0,NUM).

    • Use tail() to retain the last NUM results.

    • This step implicitly inserts a barrier().

g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").limit(2)
==> {"label":"person","age":29,"name":"marko","pk":"1"}
==> {"label":"person","age":32,"name":"josh","pk":"4"}

local

local

  • Function: (branch) Executes a sub-traversal for each input object individually.

  • Note:

    • This step calculates the sub-traversal for each object serially, which can result in poor performance.

    • You can use this step to ensure result diversity. However, using distinct() is recommended for this scenario.

// Without local, only 1 traversal result is retained for all objects.
g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").outE("created").limit(1)
==> {"label":"created","pk":"1","sk":"3","weight":0.4}

// With local, 1 traversal result is retained for each object to ensure result diversity.
g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").local(outE("created").limit(1))
==> {"label":"created","pk":"1","sk":"3","weight":0.4}
==> {"label":"created","pk":"4","sk":"3","weight":0.4}
==> {"label":"created","pk":"6","sk":"3","weight":0.2}
Equivalent to
g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").outE("created").distinct().by("pk")

loops

  • Function: (map) Returns the number of times the current loop has been executed.

// Execute the out() loop until "marko" is found or the loop has run twice.
g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").emit(or(__.has("name","marko"),loops().is(P.eq(2)))).repeat(__.out()).values("name")
==> "marko"
==> "lop"
==> "ripple"

math

  • Function: (map) Performs scientific computing.

  • Operators:

    • Functions marked with an asterisk (*) are IGraph extensions.

      Name

      Description

      Example

      +, -, *, /, %, ^

      Addition, subtraction, multiplication, division, modulo, power

      math("_+_")/math("_^2")

      abs

      Calculates the absolute value

      math("abs(_)")

      cos, sin, tan, (*)cot, (*)sec, (*)csc, acos, asin, atan

      Trigonometric functions

      math("cos(_)")

      (*)atan2

      Returns the arctangent of y/x in radians

      math("atan2(x,y)")

      consh, sinh, tanh,

      (*)acosh, (*)asinh, (*)atanh

      Hyperbolic functions

      math("cosh(_)")

      (*)sinc

      Sinc/sampling function (sinx/x)

      math("sinc(_)")

      (*)deg2grad, (*)grad2deg, (*)rad2deg, (*)deg2rad

      Conversions between radians (rad), degrees (deg), and gradians (grad)

      math("cos(deg2rad(_))")

      log, log10, log2, (*)log1p,

      Logarithmic operations

      math("log2(_)")

      (*)logn

      Logarithmic operations

      math("logn(_,2)")

      exp, (*)expm1

      Exponential operations

      math("exp(_)")

      (*)pow

      Exponential operations

      math("pow(_,2)")

      ceil

      Rounds up to the nearest integer

      math("ceil(_)")

      floor

      Rounds down to the nearest integer

      math("floor(_)")

      (*)round

      Rounds to the nearest integer

      math("round(_)")

      (*)roundn

      Rounds to n decimal places

      math("roundn(_,2)")

      sqrt

      Calculates the square root

      math("sqrt(_)")

      cbrt

      Calculates the cube root

      math("cbrt(_)")

      (*)root

      Calculates the nth root (where n is a positive integer)

      math("root(x,5)")

      signum, (*)sgn

      Sign function

      math("signum(_)")

      (*)avg, (*)max, (*)min,

      Calculates the average, maximum, or minimum value

      math("a,b,c,d")

      (*)sum, (*)mul

      Sum and Product

      math("mul(a,b,c)")

      (*)clamp, (*)iclamp

      Clamps a value to a specified range

      math("clamp(-1,a,1)")

      (*)inrange

      Returns whether a value is within a specified range

      math("inrange(-1,_,1)")

      (*)<, (*)<=, (*)=, (*)==, (*)!=, (*)<>, (*)>, (*)>=, (*)equal, (*)not_equal

      Logical operators

      math("equal(_,2)") /

      math("_==2")

      (*)erf, (*)erfc

      Error function

      math("erf(_)")

      (*)frac

      Returns the fractional part of a real number

      math("frac(_)")

      (*)trunc

      Returns the integer part of a real number

      math("trunc(_)")

      (*)hypot

      Returns the hypotenuse of a right-angled triangle

      math("hypot(a,b)")

      (*)ncdf

      Normal cumulative distribution function

      math("ncdf(_)")

      (*)and, (*)nand, (*)or, (*)nor, (*)not, (*)xor, (*)xnor, (*)&,

      (*)|

      Logical operators

      math("a and b or c") /

      math("a & b | c")

      (*)mand, (*)mor

      Batch AND/OR

      math("mand("a>1,b<2,c=3")")

      (*)true, (*)false

      Returns 0 or 1

      math("true & _=2")

  • Note:

    • The "_" character represents the input object itself.

    • Operands: _ and other sideEffect keys. The _ character refers to the input object itself.

    • If an element in the calculation is a multi-valued type, the math operator processes it as a vector. The following functions and operators do not support multi-valued types: mand, mor, &, |, atan2, logn, clamp, iclamp, root, hypot, inrange, or roundn.

    • The following functions perform aggregate operations when they process vectors: avg, min, max, sum, and mul.

// The square of person 1's age
g("thinkerpop").V("1").hasLabel("person").values("age").math("_ * _")
==> 841

// The sum of the age of person 1 and the age of each of their friends
g("thinkerpop").V("1").hasLabel("person").values("age").aggregate("x").cap("x").E("1").hasLabel("knows").inV().values("age").as("y").local(select("x").unfold().math("_ + y"))
==> 61
==> 56

max

  • Function: (map) Calculates the maximum value of the input objects.

  • Note:

    • This step supports only numeric (non-bool) inputs.

    • max(Scope.local) supports iterator inputs. The elements in the container must be numeric (non-bool).

    • This step implicitly inserts a barrier().

g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").values("age").max()
==> 35

g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").values("age").fold().max(Scope.local)
==> 35

g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").aggregate("x").by("age").cap("x").max(Scope.local)
==> 35

mean

  • Function: (map) Calculates the average value of the input objects.

  • Note:

    • This step supports only numeric (non-bool) inputs.

    • This step implicitly inserts a barrier().

g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").values("age").mean()
==> 30.75

min

  • Function: (map) Calculates the minimum value of the input objects.

  • Note:

    • This step supports only numeric (non-bool) inputs.

    • min(Scope.local) supports iterator inputs. The elements in the container must be numeric (non-bool).

    • This step implicitly inserts a barrier().

g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").values("age").min()
==> 27

g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").values("age").fold().min(Scope.local)
==> 27

g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").aggregate("x").by("age").cap("x").min(Scope.local)
==> 27

not

  • Function: (filter) Returns inputs for which the sub-traversal produces no results.

// People who have no friends
g("thinkerpop").V("1;2;4;6").hasLabel("person").not(outE("knows"))
==> {"label":"person","age":32,"name":"josh","pk":"4"}
==> {"label":"person","age":27,"name":"vadas","pk":"2"}
==> {"label":"person","age":35,"name":"peter","pk":"6"}

// People who are not 27 years old
g("thinkerpop").V("1;2;4;6").hasLabel("person").not(values("age").is(27))
==> {"label":"person","age":29,"name":"marko","pk":"1"}
==> {"label":"person","age":32,"name":"josh","pk":"4"}
==> {"label":"person","age":35,"name":"peter","pk":"6"}