Neo4jGraphStore

org.llm4s.knowledgegraph.neo4j.Neo4jGraphStore
See theNeo4jGraphStore companion object

Neo4j implementation of GraphStore.

Maps GraphStore operations to Cypher queries via the official Neo4j Java driver. All nodes carry the :LLM4S label for namespace isolation alongside any user-defined label stored as the llm4s_label property.

Properties round-trip as the in-memory store keeps them: strings, booleans and numbers are stored natively (a whole number as an integer, so a property filter on 42 matches as it does in memory), an array of one primitive type as a Neo4j list, and any other value (null, an object, a nested or mixed array) as JSON in the reserved llm4s_json property. Property keys starting with llm4s_ are reserved for the store: an upsertNode or upsertEdge that uses one is a Left (#2969).

Relationship types may be any string that is not empty or whitespace-only, and property keys any non-empty string, as in the other stores, except that a relationship type with a backslash is refused: a relationship type is written backtick-quoted, and a property key is passed as a parameter (#2970). The factories create the schema, and return a Left when they cannot (a user without schema rights needs an administrator to run these once):

CREATE CONSTRAINT llm4s_id_unique IF NOT EXISTS FOR (n:LLM4S) REQUIRE n.llm4s_id IS UNIQUE
CREATE INDEX llm4s_label_index IF NOT EXISTS FOR (n:LLM4S) ON (n.llm4s_label)

Connection management: each GraphStore call acquires a single session, executes the query, then closes the session — mirroring the PgVectorStore pattern. The driver (and its internal connection pool) is managed externally; pass ownsDriver = true only when this store should close the driver on close().

Thread-safety: the Neo4j Java driver is thread-safe; this class inherits that.

Value parameters

database

Target database name (default "neo4j")

driver

Neo4j Java driver instance

ownsDriver

Whether to close the driver on close() (default true)

Attributes

Companion
object
Source
Neo4jGraphStore.scala
Graph
Supertypes
trait GraphStore
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

override def close(): Unit

Closes the driver if this store owns it.

Closes the driver if this store owns it.

Attributes

Definition Classes
Source
Neo4jGraphStore.scala
override def deleteEdge(source: String, target: String, relationship: String): Result[Unit]

Deletes a specific edge.

Deletes a specific edge.

Value parameters

relationship

The relationship type

source

The source node ID

target

The target node ID

Attributes

Returns

Right(()) on success or if edge doesn't exist, Left(error) on failure

Definition Classes
Source
Neo4jGraphStore.scala
override def deleteNode(id: String): Result[Unit]

Deletes a node and all its connected edges.

Deletes a node and all its connected edges.

Value parameters

id

The node ID to delete

Attributes

Returns

Right(()) on success or if node doesn't exist, Left(error) on failure

Definition Classes
Source
Neo4jGraphStore.scala
def executeRead(cypher: String, params: Map[String, AnyRef]): Result[List[Map[String, Value]]]

Executes an arbitrary read-only Cypher query and returns results as a list of maps (column name → value).

Executes an arbitrary read-only Cypher query and returns results as a list of maps (column name → value).

Strings, numbers, booleans, null, lists and maps convert to the matching ujson.Value. A node is an object with its id, label and properties (for an :LLM4S node, as getNode reads them), and a relationship one with its type and properties; any other driver value is its string form.

Attributes

Source
Neo4jGraphStore.scala
def executeWrite(cypher: String, params: Map[String, AnyRef]): Result[Unit]

Executes an arbitrary write Cypher query. Useful for schema migrations and bulk operations outside the GraphStore API.

Executes an arbitrary write Cypher query. Useful for schema migrations and bulk operations outside the GraphStore API.

Attributes

Source
Neo4jGraphStore.scala
override def getNeighbors(nodeId: String, direction: Direction): Result[Seq[EdgeNodePair]]

Retrieves neighboring nodes and their connecting edges.

Retrieves neighboring nodes and their connecting edges.

Value parameters

direction

The direction of traversal (Outgoing, Incoming, Both)

nodeId

The ID of the node

Attributes

Returns

Right(Seq of (edge, neighbor node)) on success, Left(error) on failure

Definition Classes
Source
Neo4jGraphStore.scala
override def getNode(id: String): Result[Option[Node]]

Retrieves a node by ID.

Retrieves a node by ID.

Value parameters

id

The node ID

Attributes

Returns

Right(Some(node)) if found, Right(None) if not found, Left(error) on failure

Definition Classes
Source
Neo4jGraphStore.scala
override def loadAll(): Result[Graph]

Loads the entire graph.

Loads the entire graph.

For implementations backed by external databases, consider pagination or size limits to avoid memory issues with large graphs.

Attributes

Returns

Right(graph) on success, Left(error) on failure

Definition Classes
Source
Neo4jGraphStore.scala
override def query(filter: GraphFilter): Result[Graph]

Queries the graph based on filter criteria. Returns a subgraph matching the filter conditions.

Queries the graph based on filter criteria. Returns a subgraph matching the filter conditions.

A property filter needs both propertyKey and propertyValue: one without the other is a Left.

Value parameters

filter

The filter criteria

Attributes

Returns

Right(subgraph) on success, Left(error) on failure

Definition Classes
Source
Neo4jGraphStore.scala
override def schema(maxEntries: Int): Result[GraphSchema]

Bounded reads, never the whole graph (#3336): the distinct llm4s_label values (served by llm4s_label_index), one node per listed label, and, for the relationship types db.relationshipTypes() names in sorted order, one relationship of each type between two :LLM4S nodes until maxEntries + 1 are found. A type no :LLM4S pair uses (another application's, or one whose relationships were deleted) is skipped, as is one with a backslash, which this store never writes.

Bounded reads, never the whole graph (#3336): the distinct llm4s_label values (served by llm4s_label_index), one node per listed label, and, for the relationship types db.relationshipTypes() names in sorted order, one relationship of each type between two :LLM4S nodes until maxEntries + 1 are found. A type no :LLM4S pair uses (another application's, or one whose relationships were deleted) is skipped, as is one with a backslash, which this store never writes.

Attributes

Definition Classes
Source
Neo4jGraphStore.scala
override def stats(): Result[GraphStats]

Computes statistics about the graph.

Computes statistics about the graph.

Attributes

Returns

Right(stats) on success, Left(error) on failure

Definition Classes
Source
Neo4jGraphStore.scala
override def traverse(startId: String, config: TraversalConfig): Result[Seq[Node]]

Traverses the graph starting from a node using BFS. Nodes come in order of depth, and by node id within one depth, so every implementation returns the same sequence.

Traverses the graph starting from a node using BFS. Nodes come in order of depth, and by node id within one depth, so every implementation returns the same sequence.

Missing start node: should return Right(Seq.empty) not an error, since traversal of a non-existent node yields no nodes.

Value parameters

config

Traversal configuration (depth, direction, visited set). The direction parameter controls whether traversal follows outgoing, incoming, or all edges from each node.

startId

The ID of the starting node

Attributes

Returns

Right(Seq of traversed nodes) on success, Left(error) on critical failure

Definition Classes
Source
Neo4jGraphStore.scala
override def upsertEdge(edge: Edge): Result[Unit]

Inserts or updates an edge in the graph. Both source and target nodes must exist. If an edge with the same source, target, and relationship already exists, it is replaced.

Inserts or updates an edge in the graph. Both source and target nodes must exist. If an edge with the same source, target, and relationship already exists, it is replaced.

Value parameters

edge

The edge to upsert

Attributes

Returns

Right(()) on success, Left(error) if source or target nodes don't exist, or the relationship type is empty or whitespace-only

Definition Classes
Source
Neo4jGraphStore.scala
override def upsertNode(node: Node): Result[Unit]

Inserts or updates a node in the graph. If the node ID already exists, the existing node is replaced.

Inserts or updates a node in the graph. If the node ID already exists, the existing node is replaced.

Value parameters

node

The node to upsert

Attributes

Returns

Right(()) on success, Left(error) on failure

Definition Classes
Source
Neo4jGraphStore.scala

Concrete fields

Attributes

Source
Neo4jGraphStore.scala