Queries submitted to a Composite database may contain several USE clauses that direct different parts of the query to different constituent graphs.

Each constituent graph is named after the alias that introduces it into the Composite database.

The examples assume the same setup as the one created in Managing Composite databases .

Query a single graph

Example 1. Reading and returning data from a single graph
USE cineasts.latest
MATCH (movie:Movie)
RETURN movie.title AS title

The USE clause at the beginning of the query selects the cineasts.latest graph for all the subsequent clauses. MATCH is performed on that graph.

Query multiple graphs

Example 2. Reading and returning data from two graphs
USE cineasts.latest
MATCH (movie:Movie)
RETURN movie.title AS title
  UNION
USE cineasts.upcoming
MATCH (movie:Movie)
RETURN movie.title AS title

Dynamic graph access

Queries can also select constituent graphs dynamically, using the form USE graph.byName(graphName) .

Example 3. Reading and returning data from dynamically selected graphs
UNWIND ['cineasts.latest', 'cineasts.upcoming'] AS graphName
CALL {
  USE graph.byName(graphName)
  MATCH (movie:Movie)
  RETURN movie
RETURN movie.title AS title

In the example above, the part of the query accessing graph data, MATCH (movie:Movie) , is wrapped in a sub-query with a dynamic USE clause. UNWIND is used to get the names of our graphs, each on one row. The CALL {} sub-query executes once per input row. In this case, once selecting cineasts.latest , and once selecting cineasts.upcoming .

Listing graphs

The built-in function graph.names() returns a list containing the names of all constituent graphs on the current Composite database.

Example 4. The graph.names() function
UNWIND graph.names() AS graphName
RETURN graphName

Correlated subqueries

This query finds all movies in cineasts.upcoming that are to be released in the same month as the longest movie in cineasts.latest .

Example 7. Correlated subquery
CALL {
  USE cineasts.latest
  MATCH (movie:Movie)
  RETURN movie.releasedMonth AS monthOfLongest
    ORDER BY movie.runningTime DESC
    LIMIT 1
CALL {
  USE cineasts.upcoming
  WITH monthOfLongest
  MATCH (movie:Movie)
  WHERE movie.releasedMonth = monthOfLongest
  RETURN movie
RETURN movie

The first part of the query finds the movie with the longest running time from cineasts.latest , and returns its release month. The second part of the query finds all movies in cineasts.upcoming that fulfill our condition and returns them. The sub-query imports the monthOfLongest variable using WITH monthOfLongest , to make it accessible.

Updates

Composite database queries can perform updates to constituent graphs.

Example 8. Constituent graph update
USE cineasts.upcoming
CREATE (:Movie {title: 'Dune: Part Two'})

Here the outer clauses, i.e. the UNWIND , the CALL itself, and the final RETURN , appear in the root scope of the query, without a specifically chosen graph. Clauses or expressions in scopes where no graph has been specified must not be graph-accessing.

The following Composite database query is invalid because [p=(movie)-→() | p] AS paths is a graph-accessing operation in the root scope of the query:

UNWIND graph.names() AS graphName
CALL {
  USE graph.byName(graphName)
  MATCH (movie:Movie)
  RETURN movie
RETURN [p=(movie)-->() | p] AS paths

See examples of graph-accessing operations:

Nested subqueries must use the same graph as their parent query.
Attempted to access graph cineasts.upcoming
"    USE cineasts.upcoming"

Sub-queries without a USE clause can be nested. They inherit the specified graph from the outer scope.

CALL {
  USE cineasts.upcoming
  CALL {
    MATCH (m:Movie)
    RETURN m
  RETURN m
RETURN m

When a query is submitted to a Composite database, different parts of the query may run using different runtimes. Clauses or expressions in scopes where no graph has been specified run using the slotted runtime. Parts of the query directed to different constituent graphs are run using the default runtime for that graph, or respect the submitted Cypher query options if specified.

Graph functions are located in the namespace graph. The following table describes these functions:

Table 1. Built-in graph functions

graph.names()

Provides a list of names of all constituent graphs on the current Composite database.

graph.byName(graphName)

Used with the USE clause to select a constituent graph by name dynamically. This function is supported only with USE clauses.

graph.propertiesByName(graphName)

Returns a map containing the properties associated with the given graph.

Join us for the biggest graph community conference dedicated to learning how to integrate graph technologies into ML and dev projects.

Save your spot

© 2023 Neo4j, Inc.
Terms | Privacy | Sitemap

Neo4j®, Neo Technology®, Cypher®, Neo4j® Bloom and Neo4j® Aura are registered trademarks of Neo4j, Inc. All other marks are owned by their respective companies.

Contact Us →

US: 1-855-636-4532
Sweden +46 171 480 113
UK: +44 20 3868 3223
France: +33 (0) 1 88 46 13 20