neo4j-cypher-guide
Skill548 starsChanged 11 days agoApache-2.0View on GitHub
Comprehensive guide for writing modern Neo4j Cypher queries. Essential for text2cypher MCP tools and LLMs generating Cypher queries. Covers removed/deprecated syntax, modern replacements, CALL subqueries for reads, COLLECT patterns, sorting best practices, set-based writes, Quantified Path Patterns (QPP) for efficient graph traversal, and how to choose the entry point of a query so the plan starts with an index seek instead of a label scan.
In this repository54 other files it gives its agents.
- AGENTS.mdAGENTS.md
- backend/AGENTS.mdAGENTS.md
- docs/AGENTS.mdAGENTS.md
- backend/CLAUDE.mdCLAUDE.md
- CLAUDE.mdCLAUDE.md
- docs/CLAUDE.mdCLAUDE.md
- analyzing-ci-flakinessSkill
- audit-docsSkill
- commitSkill
- creating-changelog-entriesSkill
- creating-issuesSkill
- creating-prdSkill
- grilling-ideasSkill
- harvesting-reviewSkill
- migrate-feature-pageSkill
- monitoring-pull-requestsSkill
- opsmill-dev-analyzing-bugsSkill
- opsmill-dev-fixing-bugsSkill
- opsmill-dev-test-driving-bugsSkill
- pre-ciSkill
- prSkill
- rebaseSkill
- reviewing-pull-requestsSkill
- speckit-agent-context-updateSkill
- speckit-analyzeSkill
- speckit-checklistSkill
- speckit-clarifySkill
- speckit-constitutionSkill
- speckit-convergeSkill
- speckit-critique-runSkill
- speckit-git-commitSkill
- speckit-git-featureSkill
- speckit-git-initializeSkill
- speckit-git-remoteSkill
- speckit-git-validateSkill
- speckit-implementSkill
- speckit-opsmill-autoSkill
- speckit-opsmill-extractSkill
- speckit-opsmill-implementSkill
- speckit-opsmill-prepSkill
- speckit-opsmill-retrospectSkill
- speckit-opsmill-summarySkill
- speckit-planSkill
- speckit-review-codeSkill
- speckit-review-commentsSkill
- speckit-review-errorsSkill
- speckit-review-runSkill
- speckit-review-simplifySkill
- speckit-review-testsSkill
- speckit-review-typesSkill
- speckit-specifySkill
- speckit-tasksSkill
- speckit-taskstoissuesSkill
- upgrading-infrastructure-dependenciesSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
14 rules. About 0 are common, 13 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈13
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 12
When generating Cypher queries, immediately avoid these REMOVED features:
- Distinctive: line 24
Always filter nulls when sorting: - Add IS NOT NULL checks for sorted properties
- Distinctive: line 25
Explicit is better than implicit: - Always use explicit grouping and type checking
- Distinctive: line 57
Use standard Cypher patterns with modern syntax:
- Distinctive: line 67
Consider Quantified Path Patterns (QPP) for better performance:
- Distinctive: line 79
Use COUNT{}, EXISTS{}, and COLLECT{} subqueries:
- Distinctive: line 90
Use CALL subqueries for sophisticated data retrieval:
- Distinctive: line 176
Before finalizing any generated query:
- Distinctive: line 212
Start with indexed properties: - Always anchor patterns with indexed lookups
- Distinctive: line 215
Limit expansion depth: - Use reasonable upper bounds in quantifiers
- Distinctive: line 217
Profile queries: - Use PROFILE to identify bottlenecks
- Very specific: line 229
The node uuids are known: keep the :Node label and pass one list, WHERE n.uuid IN $ids, so the plan starts with a NodeIndexSeek on …
- Distinctive: line 294
CALL{} - Execute subqueries for complex reads
- Distinctive: line 303
Always prefer modern syntax patterns for better performance and maintainability.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Neo4j Modern Cypher Query Guide
This skill helps generate Neo4j Cypher queries using modern syntax patterns and avoiding deprecated features. It focuses on efficient query patterns for graph traversal, data retrieval, and set-based writes.
Quick Compatibility Check
When generating Cypher queries, immediately avoid these REMOVED features:
- ❌
id()function → UseelementId() - ❌ Implicit grouping keys → Use explicit WITH clauses
- ❌ Pattern expressions for lists → Use pattern comprehension or COLLECT subqueries
- ❌ Repeated relationship variables → Use unique variable names
- ❌ Automatic list to boolean coercion → Use explicit checks
Core Principles for Query Generation
- Use modern syntax patterns - QPP for complex traversals, CALL subqueries for complex reads
- Optimize during traversal - Filter early within patterns, not after expansion
- Always filter nulls when sorting - Add IS NOT NULL checks for sorted properties
- Explicit is better than implicit - Always use explicit grouping and type checking
Write set-based, not per-node - One query with a
WHERE/property condition beats fetching nodes and mutating them one by one (an N+1). E.g. delete all rows for one owner in one shot:MATCH (n:Preference { owner_id: $owner_id }) DETACH DELETE nnot "fetch matching nodes, then run one DELETE per node".
Critical Sorting Rule
ALWAYS filter NULL values when sorting:
// WRONG - May include null values
MATCH (n:Node)
RETURN n.name, n.value
ORDER BY n.value
// CORRECT - Filter nulls before sorting
MATCH (n:Node)
WHERE n.value IS NOT NULL
RETURN n.name, n.value
ORDER BY n.valueQuery Pattern Selection Guide
For Simple Queries
Use standard Cypher patterns with modern syntax:
MATCH (n:Label {property: value})
WHERE n.otherProperty IS :: STRING
RETURN nFor Variable-Length Paths
Consider Quantified Path Patterns (QPP) for better performance:
// Instead of: MATCH (a)-[*1..5]->(b)
// Use: MATCH (a)-[]-{1,5}(b)
// With filtering:
MATCH (a)((n WHERE n.active)-[]->(m)){1,5}(b)For Aggregations
Use COUNT{}, EXISTS{}, and COLLECT{} subqueries:
MATCH (p:Person)
WHERE count{(p)-[:KNOWS]->()} > 5
RETURN p.name,
exists{(p)-[:MANAGES]->()} AS isManagerFor Complex Read Operations
Use CALL subqueries for sophisticated data retrieval:
MATCH (d:Department)
CALL (d) {
MATCH (d)<-[:WORKS_IN]-(p:Person)
WHERE p.salary IS NOT NULL // Filter nulls
WITH p ORDER BY p.salary DESC
LIMIT 3
RETURN collect(p.name) AS topEarners
}
RETURN d.name, topEarnersCommon Query Transformations
Counting Patterns
// Old: RETURN size((n)-[]->())
// Modern: RETURN count{(n)-[]->()}Checking Existence
// Old: WHERE exists((n)-[:REL]->())
// Modern: WHERE EXISTS {MATCH (n)-[:REL]->()}
// Also valid: WHERE exists{(n)-[:REL]->()}Element IDs
// Old: WHERE id(n) = 123
// Modern: WHERE elementId(n) = "4:abc123:456"
// Note: elementId returns a string, not integerSorting with Null Handling
// Always add null check
MATCH (n:Node)
WHERE n.sortProperty IS NOT NULL
RETURN n
ORDER BY n.sortProperty
// Or use NULLS LAST
MATCH (n:Node)
RETURN n
ORDER BY n.sortProperty NULLS LASTWhen to Load Reference Documentation
Load the appropriate reference file when:
references/deprecated-syntax.md
- Migrating queries from older Neo4j versions
- Encountering syntax errors with legacy queries
- Need complete list of removed/deprecated features
references/subqueries.md
- Working with CALL subqueries for reads
- Using COLLECT or COUNT subqueries
- Handling complex aggregations
- Implementing sorting with null filtering
references/qpp.md
- Optimizing variable-length path queries
- Need early filtering during traversal
- Working with paths longer than 3-4 hops
- Complex pattern matching requirements
references/entry-points.md
- A plan starts with
NodeByLabelScanorUnionNodeByLabelsScanwhere you expected an index seek - Writing a query that starts from a relationship's
branchindex - The indexed edge is not the first hop of the pattern
Query Generation Checklist
Before finalizing any generated query:
- ✅ No deprecated functions (id, btree indexes, etc.)
- ✅ Explicit grouping for aggregations
- ✅ NULL filters for all sorted properties
- ✅ Appropriate subquery patterns for reads
- ✅ Consider QPP for paths with filtering needs
- ✅ Use COUNT{} instead of size() for pattern counting
- ✅ Variable scope clauses in CALL subqueries
- ✅ Unique variable names for relationships
Error Resolution Patterns
"Implicit grouping key" errors
// Problem: RETURN n.prop, count(*) + n.other
// Solution: WITH n.prop AS prop, n.other AS other, count(*) AS cnt
// RETURN prop, cnt + other"id() function not found"
// Use elementId() but note it returns a string, not integer"Repeated variable" errors
// Problem: MATCH (a)-[r*]->(), (b)-[r*]->()
// Solution: MATCH (a)-[r1*]->(), (b)-[r2*]->()Performance Tips
- Start with indexed properties - Always anchor patterns with indexed lookups
- Filter early in QPP - Apply WHERE clauses within the pattern
- Filter nulls before sorting - Prevent unexpected results and improve performance
- Limit expansion depth - Use reasonable upper bounds in quantifiers
- Use EXISTS for existence checks - More efficient than counting
- Profile queries - Use PROFILE to identify bottlenecks
- Leave indexed properties unwrapped -
toString(n.uuid) IN $idscannot use theuuidindex
Entry Point of a Branch-Scoped Query
Build one query text per entry point, and check the first operator of each plan with EXPLAIN.
Neo4j plans a query text once per parameter type, such as null or a list, and per length range of
a list or string, then reuses the plan for every value of that type and range. A
$ids IS NULL OR n.uuid IN $ids check plans a NodeByLabelScan instead of the Node(uuid) seek
whether $ids is null or a list, and a {branch: $branch_name} seek gets the same plan on a
small branch and on the default branch.
- The node uuids are known: keep the
:Nodelabel and pass one list,WHERE n.uuid IN $ids, so the plan starts with aNodeIndexSeekonNode(uuid). Use this entry whenever the query can run on the default branch, where abranchindex seek reads nearly every edge of the type. - Only the branch is known: match the indexed edge without node labels. A small label, such as
a kind label, or
:Rootor:Nodeon an instance with few branches, can make the plan start with aNodeByLabelScan. Check the labels after aCALL () { ... }subquery, or afterWITH DISTINCTon the start node when a secondMATCHexpands from it. A label check in the sameMATCH, or after a plainWITH, brings the scan back. - The
branchindex exists only for the edge types listed inbackend/infrahub/core/graph/index.py. A type union such asHAS_ATTRIBUTE|IS_RELATED {branch: $branch_name}plans one seek per type.
// ❌ A kind label with few vertices makes the plan start with NodeByLabelScan
MATCH (n:InfraDevice)-[r:HAS_ATTRIBUTE {branch: $branch_name}]->(a:Attribute)
// ✅ The seek runs first, and the labels filter its rows
CALL () {
MATCH (n)-[r:HAS_ATTRIBUTE {branch: $branch_name}]->(a)
RETURN n, r, a
}
WITH n, r, a
WHERE n:InfraDevice AND a:AttributeLoad references/entry-points.md for why the planner prefers the label scan, which plan operators
to look for, and patterns where the indexed edge is not the first hop.
Modern Cypher Features
Label Expressions
WHERE n:Label1|Label2 // OR
WHERE n:Label1&Label2 // AND
WHERE n:!Archived // NOTDynamic Labels and Types
Labels and relationship types accept parameters (Neo4j 5.26+):
MATCH (n:$($label)) // dynamic node label
MATCH (a)-[r:$($rel_type)]->(b) // dynamic relationship typeChoosing between a dynamic label and interpolating the label into the query text is a performance
tradeoff, not a style rule: each interpolated variant is planned and cached separately, but the
planner generally does better with a static label than a dynamic one. If the label is the primary
filter of the MATCH, prefer a static/interpolated label; when other indexed predicates carry the
filtering, a dynamic label is more likely to be acceptable. When in doubt, PROFILE both.
Type Predicates
WHERE n.prop IS :: STRING
WHERE n.value IS :: INTEGER NOT NULL
WHERE n.data IS :: LIST<STRING>Subquery Patterns for Reads
- COUNT{} - Count patterns efficiently
- EXISTS{} - Check pattern existence
- COLLECT{} - Collect complex results
- CALL{} - Execute subqueries for complex reads
Quantified Path Patterns
- Inline filtering during traversal
- Access to nodes and relationships in patterns
- Significant performance improvements (up to 1000x)
- Support for complex, multi-hop patterns
Always prefer modern syntax patterns for better performance and maintainability.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

