Skip to main content
Version: 0.0.7

agrag.graphdb.cypher_store.CypherGraphStore

CypherGraphStore(*, tracer:Tracer | None = None) -> None

Bases: GraphStore

A GraphStore for databases that run the shared Cypher builders.

Holds the record grouping, batching, per-record failure isolation and label bookkeeping that every Cypher backend needs. A backend supplies its connection, query execution, schema statements and error classification.

Functions:

Parameters:

  • tracer (Tracer | None) – Opens every span this store's methods produce.

blocked_candidate_keys​

blocked_candidate_keys(keys:Sequence[str]) -> set[str]

Return keys with a committed candidate that is not pending.

close​

close() -> None

Release the backend connection.

connect​

connect() -> None

Open the backend connection and verify connectivity.

ensure_fulltext_index​

ensure_fulltext_index(*, label:str, properties:Sequence[str]) -> None

Create a full-text index over node properties if it does not exist.

The index is built from the node text of the given properties. Its name is derived from the label and the properties, so a repeated call with the same arguments does nothing.

Parameters:

  • label (str) – The node label to index.
  • properties (Sequence[str]) – The property names to index. Each must be a safe identifier.

Raises:

  • ValueError – properties is empty, or a name is not a safe identifier.

ensure_unique_constraint​

ensure_unique_constraint(*, label:str, properties:Sequence[str]) -> None

Make a property combination unique across the nodes of a label.

Does nothing when the constraint already exists. A backend that builds constraints in the background returns once this one is usable.

Parameters:

  • label (str) – The node label.
  • properties (Sequence[str]) – The property names whose combination must be unique.

Raises:

  • ValueError – properties is empty, or a name is not a safe identifier.

ensure_vector_index​

ensure_vector_index(*, label:str, vector_property:str, dimensions:int, distance:Distance) -> None

Create a native vector index if it does not exist.

Parameters:

  • label (str) – The node label to index.
  • vector_property (str) – The embedding property name.
  • dimensions (int) – The embedding dimension. If the index already exists with a different dimension, this raises.
  • distance (Distance) – The distance metric.

Raises:

  • EmbeddingDimensionMismatchError – The index already exists with a different dimension than dimensions.

execute_read​

execute_read(query:str, parameters:Mapping[str, Any] | None = None, *, timeout:float | None = None) -> list[dict[str, Any]]

Run a read transaction.

Parameters:

  • query (str) – The Cypher query to run.
  • parameters (Mapping[str, Any] | None) – The query parameters.
  • timeout (float | None) – Server-side transaction timeout in seconds. The database terminates the transaction when it runs longer. None uses the server's default timeout. Backends that cannot enforce a timeout ignore it.

Returns:

  • list[dict[str, Any]] – The result rows as dicts.

execute_write​

execute_write(query:str, parameters:Mapping[str, Any] | None = None) -> list[dict[str, Any]]

Run a write transaction.

Parameters:

  • query (str) – The Cypher query to run.
  • parameters (Mapping[str, Any] | None) – The query parameters.

Returns:

  • list[dict[str, Any]] – The result rows as dicts.

fulltext_copy_set​

fulltext_copy_set(alias:str, source:str, properties:Iterable[str]) -> str

Return SET clauses a backend needs to keep text searchable.

Parameters:

  • alias (str) – The Cypher variable of the node being written.
  • source (str) – A Cypher map expression that holds the written properties.
  • properties (Iterable[str]) – The text properties that have a full-text index.

Returns:

  • str – Clauses to add to the write, or an empty string when the backend
  • str – searches the stored text directly.

fulltext_search_call​

fulltext_search_call(*, label:str, properties:Sequence[str]) -> SearchCall

Build the Cypher fragment that searches a full-text index.

Parameters:

  • label (str) – The node label the index covers.
  • properties (Sequence[str]) – The indexed property names, as passed to ensure_fulltext_index.

Returns:

  • SearchCall – A fragment that yields node and score and expects
  • SearchCall – $fulltext_terms and $k.

Raises:

  • ValueError – label or a property is not a safe identifier.

list_indexes​

list_indexes(*, timeout:float | None = None) -> list[IndexInfo]

List every index in the database with its readiness.

Parameters:

  • timeout (float | None) – Server-side timeout in seconds. None uses the server's default.

Returns:

  • list[IndexInfo] – One entry per index, including indexes that are still building.

node_write_extra_set​

node_write_extra_set() -> str

Return extra SET clauses that every node upsert must carry.

Returns:

  • str – Clauses for a node upsert query, which can read record.properties,
  • str – or an empty string when the backend stores the properties as given.

register_labels​

register_labels(labels:Sequence[str]) -> None

Add labels to this instance's known-label set.

Parameters:

  • labels (Sequence[str]) – The labels to register. Each must be a safe Cypher identifier.

Raises:

  • ValueError – Any label is not a safe identifier.

register_relation_types​

register_relation_types(types:Sequence[str]) -> None

Add types to this instance's known-relation-type set.

Parameters:

  • types (Sequence[str]) – The relationship types to register. Each must be a safe Cypher identifier.

Raises:

  • ValueError – Any type is not a safe identifier.

run_in_transaction​

run_in_transaction(work:Callable[[GraphStoreTransaction], Awaitable[_T]]) -> _T

Run work with a transaction handle and return its result.

The default implementation passes this store to work and provides no atomicity across calls. Backends may provide a real transaction. A transactional backend commits when work returns and rolls back when it raises. A backend may retry transient failures, so work must return results rather than change state outside the callback.

Parameters:

  • work (Callable[[GraphStoreTransaction], Awaitable[_T]]) – The reads and writes to run together. Must be safe to retry.

Returns:

  • _T – Whatever work returns.

session​

session() -> AbstractAsyncContextManager[Any]

Open a session as an async context manager.

Returns:

  • AbstractAsyncContextManager[Any] – A context manager yielding a backend session.

setup_constraints​

setup_constraints() -> None

Create per-label and per-relation-type uniqueness constraints.

Constraints cover every node label and relationship type written through this instance or already present in the database, so a fresh instance can set up an existing database without first rewriting every record.

setup_indexes​

setup_indexes() -> None

Create per-label property indexes.

Covers every node label written through this instance or already present in the database, so a fresh instance can set up an existing database without first rewriting every record.

shortest_path_distance_query​

shortest_path_distance_query() -> str

Build the Cypher that measures the shortest committed path.

Returns:

  • str – A parameterized query expecting $seed_ids and $target_ids
  • str – that returns a dist for each pair joined by a path.

upsert_candidates​

upsert_candidates(candidates:Sequence[CandidateRecord], *, batch_size:int = 256, pending_job_id:UUID | None = None) -> UpsertResult

Write candidates without changing nodes that already exist.

upsert_nodes​

upsert_nodes(label:str, nodes:Sequence[NodeRecord], *, batch_size:int = 256, pending_job_id:UUID | None = None, deferred:DeferredWrites | None = None) -> UpsertResult

Write or merge nodes, honoring each record's full label set.

label names the batch for constraint/index bookkeeping, matching every other tracked label; the labels actually written to a node come from NodeRecord.labels, which may name more than one label (for example a node that is both Chunk and Entity). Records with different label sets are grouped and written with separate MERGE queries, since Cypher requires labels to be literal in the query text rather than a runtime parameter, so batch_size chunks apply within each group rather than across the whole call.

pending_job_id tags only the nodes this call creates. A write that reaches a node the job did not create is not applied and is added to deferred, so a job's rollback cannot change data committed earlier.

Returns:

Raises:

  • ValueError – batch_size is not positive, or deferred is missing while pending_job_id is set.

upsert_relations​

upsert_relations(relations:Sequence[RelationRecord], *, batch_size:int = 256, pending_job_id:UUID | None = None, deferred:DeferredWrites | None = None) -> UpsertResult

Write or merge relationships between existing nodes.

Relationship identity is each record's id, not its endpoints: see upsert_relation_query for how endpoint changes and same-id parallel relationships are handled. pending_job_id tags only the relationships this call creates. A write that reaches a relationship the job did not create is not applied and is added to deferred.

Returns:

Raises:

  • ValueError – batch_size is not positive, or deferred is missing while pending_job_id is set.
vector_search(*, label:str, vector_property:str, query_vector:Sequence[float], limit:int = 10, filters:dict[str, Any] | None = None) -> list[VectorHit]

Search nodes by dense vector using the native vector index.

A label with no provisioned vector index has nothing to search, so an absent index returns an empty result list rather than a driver error.

The native index selects candidates before pending and caller filters apply, so a plain k=limit call can return fewer matches than actually exist. This escalates k and retries until limit visible hits come back or the escalation reaches _VECTOR_SEARCH_MAX_K.

Raises:

  • ValueError – limit is not positive. Validation runs before the backend query.

vector_search_call​

vector_search_call(*, label:str, vector_property:str) -> SearchCall

Build the Cypher fragment that searches a vector index.

Parameters:

  • label (str) – The node label the index covers.
  • vector_property (str) – The embedding property name.

Returns:

  • SearchCall – A fragment that yields node and score and expects
  • SearchCall – $vector and $k. score uses the same scale on every
  • backend (SearchCall) – higher is closer.

Raises:

  • ValueError – label or vector_property is not a safe identifier.

vector_value​

vector_value(expression:str) -> str

Wrap a Cypher expression that holds an embedding for a property write.

Parameters:

  • expression (str) – A Cypher expression that evaluates to a list of floats.

Returns:

  • str – The expression to assign to a vector property. Backends that store
  • str – a plain float list return expression unchanged.