Skip to content

Imperative

await gom.indexes.ensure(vectorIndex('Proposition', 'embedding', 1536));
await gom.indexes.ensure(rangeIndex('Proposition', 'contextId'));
await gom.indexes.ensure(rangeIndex('Message', ['sessionId', 'createdAt'])); // composite
await gom.indexes.ensure(fullTextIndex('Chunk', 'text'));
await gom.indexes.ensure(fullTextIndex('Entity', ['name', 'description']));
await gom.constraints.ensure(uniquenessConstraint('ChatSession', 'sessionId'));

A uniqueness constraint can name the property its nodes are identified by, for a violation’s sample: uniquenessConstraint('ChatSession', 'sessionId', { nodeIdProperty: 'id' }). It plays no part in what is made. A catalog read from fragments sets it from the @NodeId.

Schema statements run outside any transaction. On Neo4j and FalkorDB, which build an index in the background, ensure and recreate return only once the index is built: one made, or a matching one found there, as one another process made a moment ago. On Neo4j a write of a value too large for an index still being built fails the build, or, committed just as the index comes online, stops the database; a FalkorDB index still being built answers queries with only the rows it holds so far. Waiting fixes that for the indexes Drivine’s ensure makes and finds, but an index made some other way can still give partial rows on FalkorDB until it is OPERATIONAL. The wait is at most indexBuildTimeout (60 seconds unless set; see the options below). An index whose build failed is an error with the engine’s reason (drop it, and ensure it again once the data is put right), and so is one still being built when the time is up, which is left building. Applied by the schema manager (enforce, recreateAll, ensure), it is a problem of its owner: under FAIL_FAST, the default, it fails the owner once the owner’s other items are applied, listing each such index as BUILD FAILED or STILL BUILDING, and the owner’s marker is not recorded, so the next enforce applies its items and waits again. Under WARN it is logged so, naming the index and giving the engine’s reason, and the rest goes on; the item is reported as what was done (CREATED, ALREADY_MATCHING or RECREATED), the index is left as it is, to finish building or to stay failed, and it is waited for again on the next apply. An error reading the index’s state (a connection lost) is thrown as it is. Where a constraint’s index covers the properties already, Neo4j makes no other and there is nothing to wait for. gom.indexes waits 60 seconds, as drivine4j’s index manager does, and throws either. Neo4j’s CREATE CONSTRAINT returns once its index is built, so a constraint is not waited on; FalkorDB’s is (see below), and so is the range index it needs first.

On Postgres and Oracle, ensure builds an index so that writes to the graph go on while it is built: on Postgres a range, vector or full-text index is made CREATE INDEX CONCURRENTLY, and a uniqueness constraint’s index CREATE UNIQUE INDEX CONCURRENTLY; on Oracle a range index is built ONLINE. On Oracle a uniqueness constraint’s indexes block writes while they are built, as before, and so does the index a recreate builds beside the old one on both. Where the store can’t build online (Oracle without Enterprise Edition, ORA-00439), the index is built by a statement that blocks writes, said once at WARN. A Postgres build cut short, or one the data refuses, leaves its index invalid; the next ensure drops it and builds it again (drivine-cypher-spec#381).

Ensures of one item at once on Postgres, as replicas booting together run them, take turns (drivine-cypher-spec#382). Before it builds or drops an item’s index, an ensure claims its turn, a CLAIM:<kind> row of the graph’s schema table that no listing reads, and gives it up once done, made or failed. While another session holds the turn it waits, for 60 seconds at most, then reads the store again, finding the item where the other session made it, and ensures it without its turn where the wait ran out, said at INFO. A claim older than 60 seconds, with no online build under way on the node table, is taken over at once, as its session ended without giving it up. An ensure that finds another session’s build of the index under way waits for it and reads how it ended, rather than take the index as made or drop it as invalid, so a constraint is never reported CREATED or ALREADY_MATCHING over an index that isn’t there, is still being built, or enforces nothing. Postgres can cancel one of two online builds on the same table as a deadlock; that ensure waits until no build is under way there and runs again, up to 3 times. An online build waits for each transaction older than it, so a REPEATABLE READ or SERIALIZABLE transaction left open holds it up until it ends.

Each statement an ensure runs is run once, on every store, as drivine4j runs it: Drivine doesn’t run it again where the store turns it away for another session’s (a deadlock, a lock another session holds, FalkorDB’s write in progress). What comes next is the schema planner’s, the same in every Drivine library: it takes turns with another session and waits out its build, as above, and otherwise throws the store’s error (#830).

ensure returns a result whose status is:

Status Meaning
CREATED Nothing existed, and the item was made.
ALREADY_MATCHING A matching item exists; nothing changed.
DRIFT An item exists with a different shape, such as other vector dimensions. Nothing changed: recreate(spec) replaces it.
VIOLATION Constraints only: the data breaks it. A sample of the keys held more than once is included, each its properties, the count of nodes that hold it, and nodes: as many of them as the sample size at most, each its elementId (the engine’s own id) and the constraint’s nodeIdProperty.
BLOCKED_BY_INDEX Constraints only, on Neo4j: a plain range index over the same label and properties is in the way of it, and is named in index. Nothing changed: ensure(spec, sampleSize, true) drops the index and makes the constraint, whose own index serves the same lookups. A key held more than once keeps the index, as a VIOLATION.
RECREATED From recreate(spec): the old item dropped and the new one made.

The data is read once the engine refuses the constraint: a key held more than once is a VIOLATION, as in drivine4j, and a refusal no repeated key bears out is thrown. A sample size of 0, as in ensure(spec, 0), still reads it, and gives a VIOLATION with an empty sample. FalkorDB doesn’t refuse one: it builds it in the background and marks the build failed. Drivine then drops the failed constraint, which enforces nothing, and reads the data alike. A build that fails with no key held more than once throws. Once the data is clean, ensure makes the constraint. A constraint FalkorDB already lists as failed, or still under construction (another process’s build, or an earlier run’s), is not taken as ALREADY_MATCHING: one under construction is waited on, and matches once built, and a failed one is dropped and made again. drop(spec) also removes a constraint FalkorDB failed to build.

A VIOLATION‘s sample is a breaking change from 3.2. Each entry used to be the repeated value itself: one property’s value as it is, or several properties’ values as a list. Each entry is now a ViolatingKey, and the value is in its properties.

Pinning a vector index. Only the dimensions and similarity are always sent; the rest is the engine’s choice, and engine defaults change between versions. Pin the HNSW shape portably, and anything engine-specific as an engine option:

@VectorIndex({ similarity: 'COSINE', hnswM: 32, hnswEfConstruction: 200 }) embedding?: number[];
vectorIndex('Proposition', 'embedding', 1536, 'COSINE', {
engineOptions: [neo4jVectorOptions({ quantizationEnabled: false }), falkorDbVectorOptions({ efRuntime: 20 })]
});

An unpinned parameter is never reported as drift; a pinned one the engine contradicts is.