Skip to content

fluree ontology from a Neo4j schema export - #2216

Merged
aaj3f merged 3 commits into
mainfrom
feat/ontology-from-neo4j
Nov 3, 2026
Merged

aaj3f merged 3 commits into
mainfrom
feat/ontology-from-neo4j

Conversation

@aaj3f

@aaj3f aaj3f commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Stacked on #2215 (base feat/ontology-from-dbt-ossie, so the diff is just the fifth commit). This adds the last converter from the original list: a Neo4j graph's schema.

fluree ontology from neo4j-schema.json reads either of the two schema exports people actually have:

  • apoc.meta.schema() (CALL apoc.meta.schema() YIELD value RETURN value, saved as JSON; the row wrapper is unwrapped): labels, their typed properties with unique/indexed, and each relationship type's direction, end labels and properties;
  • db.schema.visualization() (built in, no APOC): labels and relationship types only, with their ends given as node ids or nested nodes (both shapes read).

A property graph is already close to an ontology, so the mapping is direct and goes through the same table model and emitter as everything else:

  • a label is a class (MovieGenre → "movie genre", IRI MovieGenre);
  • a node property is a property typed from its Neo4j type (STRING, INTEGER, FLOAT, BOOLEAN, DATE, the date-time and time types); a unique id, uuid or <label>Id is a key and is left out, as keys are everywhere else;
  • an outgoing relationship type is a relation from its label to the label at its other end, labelled in words (ACTED_IN → "acted in"). Since Neo4j relationship types are verbs rather than role nouns, the description is written as the sentence the relationship reads ("A person acted in a movie.") rather than the "A contact's employer: a company." form used for foreign keys. Incoming relationships are left to the label they start from, so nothing is doubled;
  • a relationship type that reaches several labels gets no range, and its description names them ("A person attended a company or a school.");
  • relationship properties (ACTED_IN.roles, REVIEWED.rating) have no place on an RDF property, so they're left out and counted on stderr. If those turn out to matter, the obvious next step is a class per relationship type that has properties (the shape the Ossie converter uses for relationships with three or more roles), but I didn't want to guess at that without a real need.

Detection is by content: .json that isn't a Genie export or an Ossie document and has APOC's {"Label": {"type": "node" | "relationship", ...}} shape or the visualization's nodes/relationships is read as a Neo4j schema; anything else still goes to JSON-LD. --input-format neo4j forces it.

Tests: unit tests for both export shapes (keys, incoming relationships, multi-label targets, the relationship-property count) and a CLI test on a small movies-graph schema that also runs the result through fluree ontology check. Docs: a Neo4j paragraph in docs/cli/ontology.md under "From tables and semantic layers", with how to export the schema.

cargo test -p fluree-db-ontology -p fluree-db-cli and cargo clippy -p fluree-db-ontology -p fluree-db-cli --all-targets --no-deps are clean locally; full CI will run once the stack is rebased onto main.

🤖 Generated with Claude Code

https://claude.ai/code/session_016CjYmQVZNYkCgGd38GYUyX

`fluree ontology from` reads a Neo4j graph's schema as JSON, from
apoc.meta.schema() (labels, typed properties, relationship types with
direction and end labels) or the built-in db.schema.visualization()
(labels and relationship types). Labels are classes; node properties are
typed properties (a unique id/uuid/<label>Id is a key and left out);
outgoing relationship types are relations labelled in words (ACTED_IN ->
"acted in") and described as the sentence they read ("A person acted in a
movie."), without a range when they reach several labels. Relationship
properties have no place on an RDF property: left out and counted.
--input-format gains neo4j.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016CjYmQVZNYkCgGd38GYUyX
@aaj3f aaj3f added enhancement New feature or request area:cli fluree CLI UX, rdf toolkit, publish/export/insert flows labels Oct 30, 2026

@bplatz bplatz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. Please go through the inline comments before merging, the detection one especially: a JSON-LD file can come out as an empty ontology with exit 0.

Comment on lines +44 to +58
fn visualization(doc: &Value) -> Option<(&Vec<Value>, &Vec<Value>)> {
let doc = match doc {
Value::Array(rows) if rows.len() == 1 => &rows[0],
other => other,
};
Some((
doc.get("nodes")?.as_array()?,
doc.get("relationships")?.as_array()?,
))
}

/// Whether a JSON document is a Neo4j schema export this module reads.
pub fn is_neo4j(doc: &Value) -> bool {
apoc_map(doc).is_some() || visualization(doc).is_some()
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

visualization() only needs nodes and relationships arrays, so a JSON-LD document with those keys ({"@context": …, "nodes": […], "relationships": […]}) is read as a Neo4j schema. The Source::Neo4j arm doesn't check for an empty model, so the result is an empty ontology, "0 classes", exit 0. Ossie's datasets detection (#2215) and is_genie (#2213) have the same gap. Two changes would close it for all of them: never route a document with @context, @graph or @id to a converter, and fail on an empty model as the dbt arm already does.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed for all of them, in f4ea978 (and the PRs below it): a document with @context, @graph, @id or @type at its top is never routed to Genie, Ossie or Neo4j detection (is_jsonld), and from fails on a file that yields no classes, for table sources (since #2210) and now the RDF path too. There's a CLI test with exactly your JSON-LD-with-nodes/relationships/datasets case.

/// A relationship type as a column of its start label's table.
fn relation(start: &str, rel: &str, targets: &[String]) -> Column {
Column {
name: rel.to_string(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The relationship type goes through the emitter's foreign-key labelling (property_label(name, true)), which strips key suffixes from what is a verb. HAS_KEY is labelled "has" ("A thing has key a key."), and the same goes for _ID, _REF and _FK; _CODE and _NUMBER take the target's label instead. Relationship types should be labelled with property_label(name, false).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in f4ea978: columns can now carry a label from the source, which the emitter uses before deriving one, and the Neo4j reader sets it to the relationship type in words. So HAS_KEY is "has key" and _ID/_CODE types keep their words.

Comment thread fluree-db-ontology/src/convert/neo4j.rs Outdated
fn relation(start: &str, rel: &str, targets: &[String]) -> Column {
Column {
name: rel.to_string(),
description: Some(sentence(start, rel, targets)),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When one relationship type starts from several labels, each start label's column carries its own sentence and the emitter keeps the first. So worksAt from both Person and Robot is described as "A person works at a company." Leaving description unset here, so the emitter's multi-domain wording applies, or joining the sentences, would fix it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in f4ea978: the readers now collect every (start, type, targets) first and write each sentence with all the start labels, so WORKS_AT from Person and Robot reads "A person or a robot works at a company." Covered by relationship_types_are_verbs_described_for_every_start.

Review follow-ups for the Neo4j converter:
- A JSON-LD document (with @context, @graph, @id or @type at its top) is
  never read as a Neo4j schema, whatever other keys it has; the same guard
  covers Genie and Ossie detection. `from` fails on a file that yields no
  classes, on the RDF path too, instead of writing an empty ontology with
  exit 0.
- A relationship type is labelled with the verb it is (`HAS_KEY` "has
  key"), through a label the reader gives the table model, rather than
  through foreign-key suffix stripping ("has", or the target's label).
- A relationship type that starts at several labels is described for all
  of them ("A person or a robot works at a company."), not with the first
  label's sentence.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016CjYmQVZNYkCgGd38GYUyX
@aaj3f

aaj3f commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @bplatz! All three are fixed in f4ea978 and replied to inline. The detection one is fixed at the root for all the JSON readers: a JSON-LD document is never routed to Genie, Ossie or Neo4j detection, and from now fails on a file that yields no classes on the RDF path too, so the empty-ontology-with-exit-0 case can't happen from either side.

@aaj3f
aaj3f marked this pull request as ready for review October 30, 2026 16:41
Base automatically changed from feat/ontology-from-dbt-ossie to main November 3, 2026 14:56
@aaj3f
aaj3f merged commit 1838f4a into main Nov 3, 2026
19 checks passed
@aaj3f
aaj3f deleted the feat/ontology-from-neo4j branch November 3, 2026 17:56
bplatz added a commit that referenced this pull request Nov 4, 2026
Brings #2247, #2232, #2246, #2248, #2245, #2230, #2205, #2231, #2255,
#2219, #2209, #2207, #2190, #2216, #2186, #2215, #2213, #2184, #2210,
#2206, #2198, #2187, #2183, #2200, #2193, #2199, #2196 and #2145 into next.

Resolutions:
- Novelty: main's fact-state trim (a cutoff plus a bounded sweep) and the
  dictionary's coverage watermark sit on next's graph slots and per-graph
  OrdMaps. A commit's reification links count toward coverage. Planner
  stats from segment sums decline under annotations, whose link deltas
  only the walk counts, and otherwise carry the indexed link counts.
- Stats-view identity: the index root's id reaches the snapshot through
  next's root decoder (`decode` takes the id, as `take_snapshot_metadata`
  does). `LedgerState::load_record` has a decoding-root form beside
  `load_decoding_root`.
- Transact: #2184's parallel-relationship check runs in next's link
  cascade, counting the `rdf:reifies` links on the retracted edge that the
  transaction does not retract itself. Main's bundle `edge_reifiers` is
  dropped, and `subjects_with_prior_rows` and
  `enforce_single_target_reifiers` stay deleted. The upsert pre-check
  takes main's dictionary probe and bounded seek.
- Query: geo search resolves the graph's POST tree once at open (pooled
  graphs keep their filter), an empty manifest when there is none.
  `branch_or_none` reads the dedicated branch, and the batched joins keep
  next's pooled lanes. The stats-view key hashes the root id.
- Indexer: `apply_dict_novelty` and `compact_forward_streams` take the
  cycle's compaction budget, so the term dictionary keeps its reserved
  share. `Prefetched` holds `ContentBytes`. `relocalize_sorted_commit`
  rewrites next's 20-byte types records.
- Disk cache: main's in-flight write registration, with an empty entry
  still read as crash debris that a write replaces. The process-wide
  eviction walk stays removed.
- Graph import (#2232): fenced CAS and publishes, the seed carries the
  record's namespace and fence, 32-bit graph ids and next's loaders. An
  import into a graph of an existing ledger refuses triple terms and edge
  annotations: the splice merges no triple-term dictionary yet.
- Iceberg: `create_iceberg_graph_source` goes with main (a source needs a
  mapping); the R2RML path keeps next's `model_instance`.
- CLI: main's shared import flags without the leaflet flags; `graph
  import` routes through next's process-wide server routing, and
  `upload_source_import` serves next's `Destination`. Doc sources read a
  ledger through the command's open instance.
- Tests: sibling ledger ids become distinct names, `genesis_ledger` is
  awaited, Cypher state dumps include each relationship's `rdf:reifies`
  link, and the graph-import arena test checks the refusal instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:cli fluree CLI UX, rdf toolkit, publish/export/insert flows enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants