Inferensys

Glossary

Cypher

Cypher is a declarative, pattern-matching query language originally developed by Neo4j for efficiently querying and modifying property graph databases.
Knowledge engineer constructing knowledge base on laptop, document hierarchy visible, casual office setup.
DECLARATIVE GRAPH QUERYING

What is Cypher?

Cypher is a declarative, pattern-matching query language originally developed by Neo4j for efficiently querying and modifying property graph databases.

Cypher is a declarative query language designed specifically for the property graph model, enabling users to express graph traversal patterns using ASCII-art syntax. Rather than specifying how to retrieve data, users declare what patterns to match, allowing the query engine to optimize execution. Its syntax represents nodes with parentheses () and directed relationships with arrows -->, making complex multi-hop queries visually intuitive.

Originally developed by Neo4j, Cypher has evolved into an open standard through the openCypher project, with adoption across multiple graph database vendors. It supports graph projection, aggregation, and subgraph extraction, making it essential for constructing and querying legal knowledge graphs where entities like statutes, citations, and parties must be traversed with high precision.

PATTERN-MATCHING QUERY LANGUAGE

Key Features of Cypher

Cypher is a declarative, pattern-matching query language originally developed by Neo4j for efficiently querying and modifying property graph databases. Its ASCII-art syntax makes graph traversal intuitive and expressive.

01

Declarative Pattern Matching

Cypher uses an ASCII-art syntax to express graph patterns visually. Instead of writing procedural traversal logic, you describe the structure you want to find, and the query engine optimizes the retrieval.

  • Nodes are represented with parentheses: (p:Person)
  • Relationships are drawn with arrows: -[r:KNOWS]->
  • A complete pattern looks like: (a:Person)-[:KNOWS]->(b:Person)-[:LIVES_IN]->(c:City)

This declarative approach separates what to retrieve from how to retrieve it, allowing the Cypher planner to use cost-based optimization for efficient execution.

02

Property Graph Native Operations

Cypher is purpose-built for the Property Graph Model, where both nodes and relationships can hold arbitrary key-value properties. This enables rich, expressive queries that filter on metadata directly within traversal patterns.

  • Node properties: MATCH (c:Case {jurisdiction: 'Federal'})
  • Relationship properties: MATCH (a)-[r:CITES {weight: 'majority'}]->(b)
  • Variable-length paths: MATCH (a)-[:APPEALS_TO*2..5]->(b) finds chains of 2 to 5 appeals

For legal knowledge graphs, this allows modeling citation networks with metadata like court level, date, and treatment status directly on edges.

03

CRUD and Graph Mutation

Unlike read-only query languages, Cypher supports full Create, Read, Update, Delete (CRUD) operations on graph structures. This makes it suitable for both analytical queries and transactional graph construction.

  • CREATE: CREATE (s:Statute {name: 'CFAA', section: '1030'})
  • MERGE: Ensures a pattern exists without duplication—critical for idempotent knowledge graph construction
  • SET/REMOVE: Modify properties on existing nodes and relationships
  • DETACH DELETE: Remove nodes along with all their incident relationships

This mutability is essential for building and maintaining evolving legal knowledge graphs that must incorporate new case law and statutory amendments.

04

Graph Projections and Aggregation

Cypher supports aggregation functions and list comprehensions that enable complex analytical queries over graph structures without requiring external processing.

  • Aggregation: count(), collect(), avg(), min(), max() operate over matched subgraphs
  • List comprehension: [x IN nodes(path) WHERE x:Precedent | x.name] filters and transforms inline
  • Path functions: shortestPath(), allShortestPaths() find optimal routes through legal citation networks
  • WITH clause: Enables pipeline-style query composition, passing intermediate results between query segments

For legal reasoning, this enables queries like "find the most-cited precedent within a 3-hop radius of this statute" in a single expression.

05

Schema Constraints and Indexing

Cypher includes DDL-style commands for enforcing data integrity and optimizing query performance through explicit indexing and constraint creation.

  • Uniqueness constraints: CREATE CONSTRAINT FOR (c:Case) REQUIRE c.citation IS UNIQUE
  • Node key constraints: Ensure a combination of properties uniquely identifies a node
  • Existence constraints: Require specific properties on nodes or relationships
  • Index types: B-tree, full-text, and vector indexes for hybrid semantic + structural search

These features are critical for legal knowledge graphs where citation integrity and entity disambiguation are non-negotiable requirements.

06

Subqueries and Query Composition

Modern Cypher (openCypher 9+) supports subqueries through CALL blocks, enabling modular query design and complex multi-stage graph analytics within a single statement.

  • CALL subqueries: Execute a nested query and union results with the outer scope
  • Post-union processing: Apply additional filters and aggregations after combining subquery results
  • Existential subqueries: Test for pattern existence without returning data: EXISTS { MATCH (a)-[:OVERRULES]->(b) }

This composability allows legal engineers to build modular reasoning pipelines—for example, first identifying relevant precedents, then filtering by jurisdictional authority, then ranking by citation frequency.

QUERY LANGUAGE COMPARISON

Cypher vs. SPARQL vs. SQL for Graph Traversal

Comparative analysis of declarative query languages for traversing graph-structured data, focusing on variable-length path expressions and recursive join capabilities essential for legal knowledge graph construction.

FeatureCypherSPARQLSQL (Recursive CTE)

Primary Data Model

Property Graph (labeled nodes, typed relationships, key-value properties)

RDF Triples (subject-predicate-object, URI-based identifiers)

Relational Tables (rows, columns, foreign key constraints)

Variable-Length Path Traversal

Native Path-Finding Syntax

MATCH (a)-[*1..5]->(b)

a :prop/:prop+ b (property paths)

WITH RECURSIVE ... UNION ALL (manual recursion)

Shortest Path Built-in

Relationship Directionality

Explicit (->, <-, --)

Implicit (property path direction)

Not applicable (join logic)

Named Relationship Types

Inference/Reasoning Support

W3C Standardized

Typical Traversal Depth Performance

O(1) per hop (index-free adjacency)

O(log n) per hop (indexed joins)

O(n) per recursion level (iterative joins)

Schema Flexibility

Optional schema (labels and property types)

Schema-optional (OWL constraints if desired)

Strict schema required (DDL)

CYPHER QUERY LANGUAGE

Frequently Asked Questions

Clear, technical answers to common questions about Cypher, the declarative graph query language for property graph databases.

Cypher is a declarative, pattern-matching query language originally developed by Neo4j for efficiently querying and modifying property graph databases. Unlike imperative languages that specify how to retrieve data, Cypher allows users to describe what data they want using ASCII-art-like graph patterns. The language works by matching these visual patterns against the stored graph structure. A query engine's cost-based optimizer then determines the most efficient traversal strategy, selecting appropriate indexes and join algorithms. Cypher's syntax is designed to be intuitive: nodes are represented by parentheses (), relationships by square brackets [] with directional arrows -->, and properties by curly braces {}. For example, (a:Person)-[:KNOWS]->(b:Person) matches all pairs of people connected by a KNOWS relationship. This pattern-matching paradigm makes complex graph traversals—like finding the shortest path or identifying subgraph isomorphisms—expressible in concise, readable queries.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.