redisgraph v0.1.0 RedisGraph.Graph View Source

A Graph consisting of RedisGraph.Nodes and RedisGraph.Edges.

A name is required for each graph.

Construct graphs by adding RedisGraph.Nodes followed by RedisGraph.Edges which relate existing nodes.

If a node does not have an alias, a random alias will be created for it when adding to a RedisGraph.Graph.

Edges cannot be added unless both the source node and destination node aliases already exist in the graph.

Link to this section Summary

Functions

Create a graph from a map.

Link to this section Types

Link to this type

t()

View Source
t() :: %RedisGraph.Graph{
  edges: [RedisGraph.Edge.t()],
  name: String.t(),
  nodes: %{optional(String.t()) => RedisGraph.Node.t()}
}

Link to this section Functions

Link to this function

add_edge(graph, edge)

View Source
add_edge(t(), RedisGraph.Edge.t()) :: {:ok, t()} | {:error, any()}

Add a RedisGraph.Edge to a graph.

If the source node or destination node are not part of the graph, then the edge cannot be added. Uses node aliases to check graph membership.

Link to this function

add_node(graph, node)

View Source
add_node(t(), RedisGraph.Node.t()) :: {t(), RedisGraph.Node.t()}

Add a RedisGraph.Node to a graph.

Creates a random string alias for the Node if the Node has no alias.

Create a graph from a map.

Example

alias RedisGraph.{Node, Edge, Graph, QueryResult}

# Create a graph
graph = Graph.new(%{
  name: "social"
})

# Create a node
john = Node.new(%{
  label: "person",
  properties: %{
    name: "John Doe",
    age: 33,
    gender: "male",
    status: "single"
  }
})

# Add the node to the graph
# The graph and node are returned
# The node may be modified if no alias has been set
# For this reason, nodes should always be added to the graph
# before creating edges between them.
{graph, john} = Graph.add_node(graph, john)

# Create a second node
japan = Node.new(%{
  label: "country",
  properties: %{
    name: "Japan"
  }
})

# Add the second node
{graph, japan} = Graph.add_node(graph, japan)

# Create an edge connecting the two nodes
edge = Edge.new(%{
  src_node: john,
  dest_node: japan,
  relation: "visited"
})

# Add the edge to the graph
# If the nodes are not present, an {:error, error} is returned
{:ok, graph} = Graph.add_edge(graph, edge)