Predicate Functions in Apache AGE
Tags: apacheage, postgres, database
Published: 2023-06-14 21:12:26

Apache AGE brings graph processing capabilities to PostgreSQL, allowing users to work with graph data efficiently. One of the key features of Apache AGE is the ability to use predicate functions within the Cypher query language. These predicate functions enable users to filter and query graph data based on specific conditions or criteria. In this blog post, we will explore some commonly used predicate functions in Apache AGE and provide examples of their usage.

### `exists()`
The exists() function is used to check if a property exists in a node or relationship. It returns true if the specified property exists and false otherwise. Here's an example that retrieves nodes where the name property exists:
```cypher
MATCH (n)
WHERE exists(n.name)
RETURN n
```

### `startsWith()`
The startsWith() function checks if the value of a property starts with a given substring. It returns true if the condition is met and false otherwise. For example, the following query retrieves nodes where the name property starts with "John":
```cypher
MATCH (n)
WHERE startsWith(n.name, 'John')
RETURN n
```

### `endsWith()`
Similar to startsWith(), the endsWith() function checks if the value of a property ends with a given substring. It returns true if the condition is satisfied and false otherwise. Here's an example that retrieves nodes where the email property ends with "@example.com":
```cypher
MATCH (n)
WHERE endsWith(n.email, '@example.com')
RETURN n
```
### `contains()`
The contains() function is used to check if a property value contains a specific substring. It returns true if the substring is found and false otherwise. Consider the following query that retrieves nodes where the text property contains the word "graph":
```cypher
MATCH (n)
WHERE contains(n.text, 'graph')
RETURN n
```

### `regex()`
The regex() function allows you to match a property value against a regular expression pattern. It returns true if the pattern is matched and false otherwise. Here's an example that retrieves nodes where the name property matches a regular expression pattern for names starting with "John" or "john":
```cypher
MATCH (n)
WHERE regex(n.name, '^(J|j)ohn.*$')
RETURN n
```

### `in()`
The in() function checks if a value is present in a specified list. It returns true if the value is found and false otherwise. For instance, consider the following query that retrieves nodes where the age property is either 20, 30, or 40:
```cypher
MATCH (n)
WHERE n.age IN [20, 30, 40]
RETURN n
```

### `isNull()`
The isNull() function checks if a property is null. It returns true if the property is null and false otherwise. Here's an example that retrieves nodes where the property property is null:
```cypher
MATCH (n)
WHERE isNull(n.property)
RETURN n
```

### Conclusion
Predicate functions in Apache AGE's Cypher query language provide powerful filtering capabilities for graph data. By utilizing these functions, users can extract the desired information from their graph databases effectively. Whether it's checking property existence, matching patterns, or performing list-based checks, predicate functions enhance the querying capabilities of Apache AGE and streamline graph data analysis.