🔍 Code Extractor

function generate_cypher_examples

Maturity: 45

Generates a comprehensive Cypher query examples file for interacting with a Neo4j graph database based on the provided schema information.

File:
/tf/active/vicechatdev/neo4j_schema_report.py
Lines:
1288 - 1446
Complexity:
moderate

Purpose

This function creates a .cypher file containing example queries for exploring, querying, creating, updating, and deleting nodes and relationships in a Neo4j database. It generates queries tailored to the specific schema including node labels, relationships, and provides advanced query patterns for graph traversal, aggregation, pattern matching, and more. This is useful for documentation, learning, and quick reference when working with a Neo4j database.

Source Code

def generate_cypher_examples(schema_info, output_dir):
    """Generate Cypher query examples for working with the Neo4j database"""
    cypher_file = os.path.join(output_dir, "neo4j_cypher_examples.cypher")
    
    cypher = """// Neo4j Cypher Query Examples
// Generated from database schema

// General database exploration
// Count nodes by label
MATCH (n)
RETURN labels(n) AS Label, count(*) AS Count
ORDER BY Count DESC;

// Count relationships by type
MATCH ()-[r]->()
RETURN type(r) AS RelationshipType, count(*) AS Count
ORDER BY Count DESC;

"""
    
    # Add node label queries
    cypher += "// Node queries\n"
    for label in sorted(schema_info["node_labels"]):
        properties = schema_info["node_properties"].get(label, [])
        
        cypher += f"""
// Get all {label} nodes (limited)
MATCH (n:{label})
RETURN n
LIMIT 100;

// Get {label} by id
MATCH (n:{label} {{id: "example_id"}})
RETURN n;

// Get {label} by UID
MATCH (n:{label} {{UID: "example_uid"}})
RETURN n;

// Create a new {label} node
CREATE (n:{label} {{
    id: "new_id",
    UID: "new_uid"
    // Add other properties as needed
}})
RETURN n;

// Update a {label} node
MATCH (n:{label} {{id: "example_id"}})
SET n.property = "new value"
RETURN n;

// Delete a {label} node
MATCH (n:{label} {{id: "example_id"}})
DETACH DELETE n;

"""
    
    # Add relationship queries
    cypher += "// Relationship queries\n"
    for source, targets in schema_info["node_relationships"].items():
        for target, rels in targets.items():
            for rel in rels:
                rel_type = rel["type"]
                
                cypher += f"""
// Get all {source}-[{rel_type}]->{target} relationships
MATCH (source:{source})-[r:{rel_type}]->(target:{target})
RETURN source, r, target
LIMIT 100;

// Create a {rel_type} relationship
MATCH (source:{source} {{id: "source_id"}})
MATCH (target:{target} {{id: "target_id"}})
CREATE (source)-[r:{rel_type}]->(target)
// SET r.property = "value" // Add properties if needed
RETURN source, r, target;

"""
    
    # Add graph traversal examples
    cypher += "// Graph traversal examples\n"
    
    # Pick a path for traversal example
    if schema_info["node_relationships"]:
        source_node = list(schema_info["node_relationships"].keys())[0]
        
        cypher += f"""
// Path traversal from a {source_node}
MATCH path = (n:{source_node})-[*1..3]->()
RETURN path
LIMIT 10;

// Finding shortest paths
MATCH (start:{source_node} {{id: "start_id"}}),
      (end:SomeOtherLabel {{id: "end_id"}})
MATCH path = shortestPath((start)-[*]-(end))
RETURN path;

"""
    
    # Add more advanced queries
    cypher += """// Advanced queries

// Aggregation example
MATCH (n)-[r]->(m)
RETURN type(r) AS relationship, count(*) AS count, 
       collect(distinct labels(n)) AS source_labels,
       collect(distinct labels(m)) AS target_labels
ORDER BY count DESC;

// Pattern matching with multiple relationships
MATCH (a)-[:RELATIONSHIP_TYPE1]->(b)-[:RELATIONSHIP_TYPE2]->(c)
WHERE a.property = "value"
RETURN a, b, c
LIMIT 100;

// Using WITH for query chaining
MATCH (n:Label1)
WITH n, size((n)--()) AS connections
WHERE connections > 3
RETURN n.id, connections
ORDER BY connections DESC;

// Sub-queries with CALL
MATCH (n:Label1 {property: "value"})
CALL {
  WITH n
  MATCH (n)-[:RELATION]->(m:Label2)
  RETURN count(m) AS connected_count
}
RETURN n.id, connected_count;

// Working with lists
MATCH (n:Label1)
RETURN n.id, 
       [prop IN keys(n) WHERE prop STARTS WITH "prefix_"] AS filtered_properties,
       size([prop IN keys(n) WHERE prop STARTS WITH "prefix_"]) AS property_count;

// Date and time functions
MATCH (n:Label1)
WHERE datetime(n.created_date) > datetime("2023-01-01T00:00:00")
RETURN n
ORDER BY n.created_date DESC
LIMIT 20;

// Full-text search (if configured)
// CALL db.index.fulltext.queryNodes("nodeIndex", "search term") YIELD node
// RETURN node;

// Spatial functions (if you have point data)
// MATCH (n:Location)
// WHERE point.distance(point({longitude: n.longitude, latitude: n.latitude}), 
//                      point({longitude: 12.34, latitude: 56.78})) < 5000
// RETURN n;
"""
    
    with open(cypher_file, "w") as f:
        f.write(cypher)

Parameters

Name Type Default Kind
schema_info - - positional_or_keyword
output_dir - - positional_or_keyword

Parameter Details

schema_info: A dictionary containing Neo4j database schema information. Expected keys: 'node_labels' (list of node label strings), 'node_properties' (dict mapping label names to lists of property names), and 'node_relationships' (nested dict structure mapping source labels to target labels to relationship details with 'type' key). This structure describes the graph database schema.

output_dir: String path to the directory where the generated 'neo4j_cypher_examples.cypher' file will be written. The directory must exist or be creatable by the process.

Return Value

This function returns None (implicitly). Its primary effect is the side effect of writing a .cypher file to disk containing generated Cypher query examples.

Dependencies

  • os

Required Imports

import os

Usage Example

import os

# Define schema information from your Neo4j database
schema_info = {
    "node_labels": ["Person", "Company", "Product"],
    "node_properties": {
        "Person": ["id", "UID", "name", "age"],
        "Company": ["id", "UID", "name", "industry"],
        "Product": ["id", "UID", "name", "price"]
    },
    "node_relationships": {
        "Person": {
            "Company": [{"type": "WORKS_AT"}],
            "Product": [{"type": "PURCHASED"}]
        },
        "Company": {
            "Product": [{"type": "MANUFACTURES"}]
        }
    }
}

# Specify output directory
output_dir = "./neo4j_docs"
os.makedirs(output_dir, exist_ok=True)

# Generate Cypher examples
generate_cypher_examples(schema_info, output_dir)

# Result: Creates file at ./neo4j_docs/neo4j_cypher_examples.cypher

Best Practices

  • Ensure the schema_info dictionary is properly structured with all required keys before calling this function
  • Create the output directory before calling this function or ensure the process has permissions to create it
  • The generated queries use placeholder values like 'example_id' and 'example_uid' that should be replaced with actual values when using the queries
  • Review and customize the generated queries for your specific use case, especially the LIMIT clauses and WHERE conditions
  • The function assumes nodes have 'id' and 'UID' properties; adjust generated queries if your schema differs
  • Use the generated file as a starting point and reference guide, not as production-ready queries
  • Consider version controlling the generated .cypher file for documentation purposes

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function generate_python_snippets 70.8% similar

    Generates a Python file containing code snippets and helper functions for interacting with a Neo4j graph database based on the provided schema information.

    From: /tf/active/vicechatdev/neo4j_schema_report.py
  • function generate_neo4j_schema_report 65.7% similar

    Generates a comprehensive schema report of a Neo4j graph database, including node labels, relationships, properties, constraints, indexes, and sample data, outputting multiple report formats (JSON, HTML, Python snippets, Cypher examples).

    From: /tf/active/vicechatdev/neo4j_schema_report.py
  • function generate_diagram_data 60.3% similar

    Transforms Neo4j schema information into a structured format suitable for graph visualization, creating separate node and edge data structures.

    From: /tf/active/vicechatdev/neo4j_schema_report.py
  • function create_folder_hierarchy_v2 48.6% similar

    Creates a hierarchical structure of Subfolder nodes in a Neo4j graph database based on a file path, connecting each folder level with PATH relationships.

    From: /tf/active/vicechatdev/offline_docstore_multi.py
  • function create_folder_hierarchy 48.2% similar

    Creates a hierarchical structure of Subfolder nodes in a Neo4j graph database based on a file system path, connecting each folder level with PATH relationships.

    From: /tf/active/vicechatdev/offline_docstore_multi_vice.py
← Back to Browse