View Source RedisGraph (ex_redisgraph v0.1.0)

Query builder library that provides functions to construct Cypher queries to communicate with RedisGraph database and interact with the entities through defined structures.

The library is developed on top of an existing library, written by Christopher Flynn, and provides additional functionality with refactored codebase to support RedisGraph result set.

To run RedisGraph locally with Docker, use

docker run -p 6379:6379 -it --rm redis/redis-stack-server

Here is a simple example of how to use the library:

alias RedisGraph.{Query, Graph, QueryResult}

# Create a connection using Redix
{:ok, conn} = Redix.start_link("redis://localhost:6379")

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

{:ok, query} =
      Query.new()
      |> Query.create()
      |> Query.node(:n, ["Person"], %{age: 30, name: "John Doe", works: true})
      |> Query.relationship_from_to(:r, "TRAVELS_TO", %{purpose: "pleasure"})
      |> Query.node(:m, ["Place"], %{name: "Japan"})
      |> Query.return(:n)
      |> Query.return_property(:n, "age", :Age)
      |> Query.return(:m)
      |> Query.build_query()

# query will hold
# "CREATE (n:Person {age: 30, name: 'John Doe', works: true})-[r:TRAVELS_TO {purpose: 'pleasure'}]->(m:Place {name: 'Japan'}) RETURN n, n.age AS Age, m

# Execute the query
{:ok, query_result} = RedisGraph.query(conn, graph.name, query)

# Get result set
result_set = Map.get(query_result, :result_set)
# result_set will hold
# [
#   [
#     %RedisGraph.Node{
#       id: 2,
#       alias: :n,
#       labels: ["Person"],
#       properties: %{age: 30, name: "John Doe", works: true}
#     },
#     30,
#     %RedisGraph.Node{
#       id: 3,
#       alias: :m,
#       labels: ["Place"],
#       properties: %{name: "Japan"}
#     }
#   ]
# ]

Link to this section Summary

Functions

Execute a procedure call against the graph specified.

Execute a procedure call against the graph specified and receive the raw result of the procedure call.

Execute arbitrary command against the database.

Delete a graph from the database.

Fetch the execution plan for a query on a graph.

Fetch response of the db.labels() procedure call against the specified graph. It returns a RedisGraph.QueryResult containing the result set and metadata from the call. ## Example

Fetch response of the db.propertyKeys() procedure call against the specified graph. It returns a RedisGraph.QueryResult containing the result set and metadata from the call. ## Example

Run a query on a graph in the database.

Fetch response of the db.relationshipTypes() procedure call against the specified graph. It returns a RedisGraph.QueryResult containing the result set and metadata from the call. ## Example

Link to this section Types

@type connection() :: GenServer.server()

Link to this section Functions

Link to this function

call_procedure(conn, graph_name, procedure, args \\ [], kwargs \\ %{})

View Source
@spec call_procedure(connection(), String.t(), String.t(), list(), map()) ::
  {:ok, RedisGraph.QueryResult.t()} | {:error, any()}

Execute a procedure call against the graph specified.

Returns a RedisGraph.QueryResult containing the result set and metadata associated with the query or error message.

https://redis.io/docs/stack/graph/design/client_spec/#procedure-calls

example

Example:

alias RedisGraph.Graph

# Create a connection using Redix
{:ok, conn} = Redix.start_link("redis://localhost:6379")

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

# Call the query
{:ok, query_result} = RedisGraph.call_procedure(conn, graph.name, "db.labels")
Link to this function

call_procedure_raw(conn, graph_name, procedure, args \\ [], kwargs \\ %{})

View Source
@spec call_procedure_raw(connection(), String.t(), String.t(), list(), map()) ::
  {:ok, list()} | {:error, any()}

Execute a procedure call against the graph specified and receive the raw result of the procedure call.

https://redis.io/docs/stack/graph/design/client_spec/#procedure-calls

example

Example:

alias RedisGraph.Graph

# Create a connection using Redix
{:ok, conn} = Redix.start_link("redis://localhost:6379")

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

# Call the query
{:ok, query_result} = RedisGraph.call_procedure_raw(conn, graph.name, "db.labels")
@spec command(connection(), [String.t()]) ::
  {:ok, RedisGraph.QueryResult.t()} | {:error, any()}

Execute arbitrary command against the database.

https://redis.io/docs/stack/graph/commands/

Query commands will be a list of strings. They will begin with either GRAPH.QUERY, GRAPH.EXPLAIN, GRAPH.DELETE etc.

The next element will be the name of the graph.

The third element will be the query command.

Optionally pass the last element --compact for compact results.

Returns a RedisGraph.QueryResult containing the result set and metadata associated with the query or error message.

example

Example:

alias RedisGraph.Graph

# Create a connection using Redix
{:ok, conn} = Redix.start_link("redis://localhost:6379")

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

# Create the query
query = [
    "GRAPH.QUERY",
    graph.name,
    "MATCH (a:actor)-[:act]->(m:movie {title:'straight outta compton'}) RETURN a",
    "--compact"
  ]

# Call the command
{:ok, query_result} = RedisGraph.command(conn, query)
Link to this function

delete(conn, graph_name)

View Source
@spec delete(connection(), String.t()) ::
  {:ok, RedisGraph.QueryResult.t()} | {:error, any()}

Delete a graph from the database.

Returns a RedisGraph.QueryResult containing the result set and metadata associated with the query or error message.

https://redis.io/commands/graph.delete/

example

Example:

alias RedisGraph.Graph

# Create a connection using Redix
{:ok, conn} = Redix.start_link("redis://localhost:6379")

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

# Call the query
{:ok, query_result} = RedisGraph.delete(conn, graph.name)
Link to this function

execution_plan(conn, graph_name, q)

View Source
@spec execution_plan(connection(), String.t(), String.t()) ::
  {:ok, list()} | {:error, any()}

Fetch the execution plan for a query on a graph.

Returns a raw result containing the query plan.

https://redis.io/commands/graph.explain/

example

Example:

alias RedisGraph.Graph

# Create a connection using Redix
{:ok, conn} = Redix.start_link("redis://localhost:6379")

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

# Create the query
query = "MATCH (a:actor)-[:act]->(m:movie {title:'straight outta compton'}) RETURN a"

# Call the query
{:ok, query_result} = RedisGraph.execution_plan(conn, graph.name, query)
Link to this function

labels(conn, graph_name)

View Source
@spec labels(connection(), String.t()) :: {:ok, list()} | {:error, any()}

Fetch response of the db.labels() procedure call against the specified graph. It returns a RedisGraph.QueryResult containing the result set and metadata from the call. ## Example:

alias RedisGraph.Graph

# Create a connection using Redix
{:ok, conn} = Redix.start_link("redis://localhost:6379")

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

# Call the query
{:ok, query_result} = RedisGraph.labels(conn, graph.name)
Link to this function

property_keys(conn, graph_name)

View Source
@spec property_keys(connection(), String.t()) :: {:ok, list()} | {:error, any()}

Fetch response of the db.propertyKeys() procedure call against the specified graph. It returns a RedisGraph.QueryResult containing the result set and metadata from the call. ## Example:

alias RedisGraph.Graph

# Create a connection using Redix
{:ok, conn} = Redix.start_link("redis://localhost:6379")

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

# Call the query
{:ok, query_result} = RedisGraph.property_keys(conn, graph.name)
Link to this function

query(conn, graph_name, q)

View Source
@spec query(connection(), String.t(), String.t()) ::
  {:ok, RedisGraph.QueryResult.t()} | {:error, any()}

Run a query on a graph in the database.

Returns a RedisGraph.QueryResult containing the result set and metadata associated with the query or error message.

https://redis.io/commands/graph.query/

example

Example:

alias RedisGraph.Graph

# Create a connection using Redix
{:ok, conn} = Redix.start_link("redis://localhost:6379")

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

# Create the query
query = "MATCH (a:actor)-[:act]->(m:movie {title:'straight outta compton'}) RETURN a"

# Call the query
{:ok, query_result} = RedisGraph.query(conn, graph.name, query)
Link to this function

relationship_types(conn, graph_name)

View Source
@spec relationship_types(connection(), String.t()) :: {:ok, list()} | {:error, any()}

Fetch response of the db.relationshipTypes() procedure call against the specified graph. It returns a RedisGraph.QueryResult containing the result set and metadata from the call. ## Example:

alias RedisGraph.Graph

# Create a connection using Redix
{:ok, conn} = Redix.start_link("redis://localhost:6379")

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

# Call the query
{:ok, query_result} = RedisGraph.relationship_types(conn, graph.name)