DEV Community

WALEED SHAHID
WALEED SHAHID

Posted on

Predicate Functions

In the world of graph databases, predicates are essential for filtering and refining query results. Predicates are boolean functions that evaluate input data and return true or false, depending on whether the input meets certain criteria. In this blog post, we'll take a closer look at two types of predicates - exists() with a property argument and exists() with a path argument.

exists() with a property argument:

The exists() function with a property argument returns true if the specified property exists in the node, relationship, or map. It is different from the EXISTS clause, which is used to check the existence of a subquery result. The exists() function is typically used to filter out subgraphs in the WHERE part of a query.

The syntax for using exists() with a property argument is as follows:

exists(property)
Enter fullscreen mode Exit fullscreen mode

Here's an example of how to use exists() with a property argument:

SELECT *
FROM cypher('graph_name', $$
     MATCH (n)
     WHERE exists(n.surname)
     RETURN n.first_name, n.last_name
$$) as (first_name agtype, last_name agtype);
Enter fullscreen mode Exit fullscreen mode

This query returns all nodes that have a surname property, along with their first_name and last_name values.

exists() with a path argument:

The exists() function with a path argument returns true if there already exists the given path in the graph. This predicate is useful for filtering nodes that are connected to a specific node through a given path.

The syntax for using exists() with a path argument is as follows:

exists(path)
Enter fullscreen mode Exit fullscreen mode

Here's an example of how to use exists() with a path argument:

SELECT *
FROM cypher('graph_name', $$
     MATCH (n)
     WHERE exists(n)-[]-(name: 'Willem Defoe')
     RETURN n.full_name
$$) as (full_name agtype);
Enter fullscreen mode Exit fullscreen mode

This query returns all nodes that have a path to a node with the name property equal to 'Willem Defoe', along with their full_name values.

Conclusion:

In conclusion, predicates are a powerful tool for filtering and refining query results in graph databases. Whether you're using exists() with a property argument or exists() with a path argument, understanding how these predicates work can help you write more efficient and effective queries.

Top comments (0)