Are you tired of dealing with unnecessary nodes and links in your Cypher queries? Do you want to take your graph querying skills to the next level? Look no further! In this article, we’ll dive into the powerful world of apoc.path.expand and explore the art of filtering nodes and links like a pro.
What is apoc.path.expand?
Apoc.path.expand is a powerful Cypher function provided by the APOC library, a popular graph database plugin. This function allows you to expand a pattern-based query to traverse the graph, returning a set of nodes and relationships that match the specified pattern. But, did you know that you can also use apoc.path.expand to filter out unwanted nodes and links?
Basic Syntax
apoc.path.expand(graph, startNode, 'pattern',Config)
In this syntax:
graph
is the graph database to querystartNode
is the starting node for the query'pattern'
is the pattern to match, such as ‘_Node’ or ‘_Relationship’Config
is an optional configuration object to customize the query
Filtering Nodes
One of the most common use cases for apoc.path.expand is filtering nodes based on specific properties or labels. Let’s say you want to find all nodes with a specific label ‘Person’ that are within 2 hops from a start node:
MATCH (startNode:City {name: 'Paris'})
CALL apoc.path.expand(startNode, 'Person', 'Node', {
labelFilter: 'Person',
maxLevel: 2
}) YIELD path
RETURN path
In this example, we’re using the labelFilter
configuration option to filter out nodes that don’t have the ‘Person’ label. The maxLevel
option specifies the maximum depth of the traversal.
Filtering by Property
What if you want to filter nodes based on a specific property value? Let’s say you want to find all nodes with a property ‘age’ greater than 30:
MATCH (startNode:City {name: 'Paris'})
CALL apoc.path.expand(startNode, 'Person', 'Node', {
propertyFilter: { age: 30, operator: 'gt' }
}) YIELD path
RETURN path
In this example, we’re using the propertyFilter
configuration option to filter out nodes that don’t have an ‘age’ property greater than 30.
Filtering Links
But what about filtering links? Let’s say you want to find all relationships of type ‘FRIEND’ between nodes with a specific label ‘Person’:
MATCH (startNode:City {name: 'Paris'})
CALL apoc.path.expand(startNode, 'Person', 'Relationship', {
relationshipFilter: 'FRIEND'
}) YIELD path
RETURN path
In this example, we’re using the relationshipFilter
configuration option to filter out relationships that aren’t of type ‘FRIEND’.
Filtering by Property (Again!)
What if you want to filter relationships based on a specific property value? Let’s say you want to find all relationships with a property ‘since’ greater than 2010:
MATCH (startNode:City {name: 'Paris'})
CALL apoc.path.expand(startNode, 'Person', 'Relationship', {
relationshipPropertyFilter: { since: 2010, operator: 'gt' }
}) YIELD path
RETURN path
In this example, we’re using the relationshipPropertyFilter
configuration option to filter out relationships that don’t have a ‘since’ property greater than 2010.
Combining Filters
But wait, there’s more! You can combine multiple filters to create even more complex queries. Let’s say you want to find all nodes with label ‘Person’ and property ‘age’ greater than 30, and only traverse relationships of type ‘FRIEND’ with a property ‘since’ greater than 2010:
MATCH (startNode:City {name: 'Paris'})
CALL apoc.path.expand(startNode, 'Person', 'Node', {
labelFilter: 'Person',
propertyFilter: { age: 30, operator: 'gt' }
}, {
relationshipFilter: 'FRIEND',
relationshipPropertyFilter: { since: 2010, operator: 'gt' }
}) YIELD path
RETURN path
In this example, we’re using multiple filters to create a highly customized query that returns only the desired nodes and relationships.
Performance Optimization
As with any graph query, performance is crucial when working with apoc.path.expand. Here are some tips to optimize your queries:
- Use indexes on filtered properties to speed up the query
- Limit the maximum depth of the traversal using the
maxLevel
option - Use the
bfs
(breadth-first search) option to traverse the graph more efficiently - Use the
uniqueness
option to deduplicate results and reduce the amount of data returned
Conclusion
Mastering apoc.path.expand is all about understanding the power of filtering nodes and links in your Cypher queries. By combining multiple filters and optimizing your queries, you can unlock the full potential of your graph database and take your querying skills to the next level.
Remember, the key to success lies in experimenting with different filters and configurations to find the perfect combination for your specific use case. So, go ahead, get creative, and start filtering like a pro!
Filter Type | Example | Description |
---|---|---|
Label Filter | labelFilter: 'Person' |
Filter nodes by label |
Property Filter | propertyFilter: { age: 30, operator: 'gt' } |
Filter nodes by property value |
Relationship Filter | relationshipFilter: 'FRIEND' |
Filter relationships by type |
Relationship Property Filter | relationshipPropertyFilter: { since: 2010, operator: 'gt' } |
Filter relationships by property value |
Happy querying!
Frequently Asked Questions
Get answers to the most pressing questions about apoc.path.expand filtering nodes/links.
What is the purpose of apoc.path.expand in filtering nodes/links?
Apoc.path.expand is a powerful tool in Cypher that allows you to navigate and filter nodes and relationships in a graph database. Its primary purpose is to expand a pattern by following relationships and filtering out unwanted nodes and links, making it easier to query and analyze complex graph data.
How do I specify the filtering criteria for apoc.path.expand?
You can specify filtering criteria for apoc.path.expand using various options such as node labels, relationship types, and property values. For example, `apoc.path.expand($node, ‘node:*’, ‘RELATIONSHIP_TYPE’, ‘prop_key=prop_value’)` filters nodes with the specified label and relationship type, and nodes with the specified property key-value pair.
Can I use apoc.path.expand to filter nodes based on their properties?
Yes, you can use apoc.path.expand to filter nodes based on their properties. For example, `apoc.path.expand($node, ‘node:*’, ‘*’, ‘prop_key=prop_value’)` filters nodes with the specified property key-value pair. You can also use more complex property filtering using Cypher expressions, such as `apoc.path.expand($node, ‘node:*’, ‘*’, ‘prop_key >= 10 AND prop_key <= 20')`.
How does apoc.path.expand handle duplicate nodes and relationships?
Apoc.path.expand automatically removes duplicate nodes and relationships from the result set, so you don’t need to worry about handling duplicates. This is particularly useful when working with large graphs where duplicates can occur due to redundant relationships or node occurrences.
Can I use apoc.path.expand in combination with other Cypher clauses?
Yes, you can use apoc.path.expand in combination with other Cypher clauses, such as MATCH, WHERE, and WITH, to create more complex queries. For example, `MATCH p = apoc.path.expand($node, ‘node:*’, ‘RELATIONSHIP_TYPE’) WHERE p.node.prop_key = ‘prop_value’ RETURN p` combines apoc.path.expand with a MATCH clause and a WHERE clause to filter the result set.