Skip to main content

Graph Database Data Modeling - ORDERED Sequences

Using graph databases, you may have a need for ORDERED SEQUENCES of data records...

For example:
  • ordered sequences of adverse events in clinical trials
  • ordered sequences of text messages
  • ordered sequences of chemical production steps
  • ordered sequences of "content items" for a content management system
 
Good news : an open-source library exists for conveniently managing this data model in a Neo4j graph database (and in principle could be ported to other graph databases.)
 
This concept of ordered sequences of data nodes:
 
1) is hugely general : I gave them the name "(Ordered) Collections"

2) can be tricky to implement fully (for example, to deal with multiple insertions in the middle)

3) fortunately, all is handled by this open-source library! 😁
 
=> Here's the documentation

=> Here's the source code


This article is part of a growing, ongoing series on Graph Databases and Neo4j

A Simple Example

The attached image is a simple example of a "Collection" (ordered sequence) representing a photo album of a trip to Greece, with data nodes that represent individual photos (desired to be in a particular order).



In this implementation, the position of the individual items in the sequence is stored as an integer value of the link property "pos", on the "in_category" relationships.

You may ignore the green and orange nodes on the left hand side: those are part of the Schema Layer that we opt to use for data integrity and other reasons - but not relevant to this discussion of Collections (ordered sequences.)

Collections vs. "Recordsets"

"Collections", as I use the term, are relatively permanent, with only sporadic "tweaks" of relative positions. All the use cases given in this article are generally in this category.

For different use cases, there's a separate data structure slated to be added to the BrainAnnex open-source project, tentatively called "Recordset" - meant for a group of records that you want to show together at a particular time (for example tabular view of data), in response to the user request for a particular sorting.

The envisioned design for the "Recordset" structure is that no sequential positional information is to be actually stored in the database; rather, it is the responsibility of a front-end module to request the desired records to show, and to pass in the request a criterion for sorting the records : ordering is done on the fly by the backend, in servicing the data request.

The front-end module may also offer a way to select a record (node) and optionally follow links out of it.  I.e. a lot more responsibility on the front end!  This is currently experimental, and slated to be rolled out in future releases...



Technical note on design considerations:

When I first announced this python library on LinkedIn and elsewhere, I got a ton of reactions: clearly, there's a broad need, and widespread interest in the matter!  Several people mentioned an alternate design, which I'll discuss below.

On multiple occasions, starting in a parent project around 2015, I considered using a "linked list" kind of data structure, with "next" relationships among the child nodes. 

But I disliked several aspects when I first considered it - and have continued to feel the same way throughout the years.

What I dislike about the "linked list" design includes:
  • In many use cases, the more "natural" ("primary") relationship is the child node belonging to the parent (the Collection) at a particular position (e.g. the "step" in a sequence or relative position on a page, etc); being "next" to another child seems more like a consequence.
  • All the traversals of the "linked list" of nodes feels burdensome
But, most importantly, I think that the following consideration is the last nail on the coffin for the "linked list" design:
  • Data nodes ought to be allowed to belong to MULTIPLE ordered collections (in which case there's no such thing as a single "next" relationship)
Example - The use case of Content Management Systems :
  1. the data nodes might be "content items", such as images or text notes or documents

  2. the ordered collections might be "categories" (topics)

  3. the sequential order might be the position of the content items on the page, akin to the relative order of paragraphs, figures, etc, in a chapter in a book.
Well, in situations like these, where the data nodes belong to MULTIPLE ordered collections, then we have a problem with an implementation that uses "next" relationships between the data nodes.   The "next" neighbor will be different depending on which collection we are traversing!

Would anyone want to juggle multiple "next" relationships - one for each collection?  I think that would be messy and confusing!  Hence I stand by my original design : the positional information is most appropriately described by an integer property value on the "belongs to collection" relationships from the data nodes.


This article is part of a growing, ongoing series on Graph Databases and Neo4j


Comments

Popular posts from this blog

Discussing Neuroscience with ChatGPT

UPDATED Apr. 2023 - I'm excited by ChatGPT 's possibilities in terms of facilitating advanced learning .  For example, I got enlightening answers to questions that I had confronted when I first studied neuroscience.  The examples below are taken from a very recent session I had with ChatGPT (mid Jan. 2023.) Source: https://neurosciencestuff.tumblr.com In case you're not familiar with ChatGPT, it's a very sophisticated "chatbot" - though, if you call it that way, it'll correct you!  'I am not a "chatbot", I am a language model, a sophisticated type of AI algorithm trained on vast amounts of text data to generate human-like text'. For a high-level explanation of how ChatGPT actually works - which also gives immense insight into its weaknesses, there's an excellent late Jan. 2023 talk by Stephen Wolfram, the brilliant author of the Mathematica software and of Wolfram Alpha , a product that could be combined with ChatGPT to imp...

Using Schema in Graph Databases such as Neo4j

UPDATED Feb. 2024 - Graph databases have an easygoing laissez-faire attitude: "express yourself (almost) however you want"... By contrast, relational databases come across with an attitude like a micro-manager:  "my way or the highway"... Is there a way to take the best of both worlds and distance oneself from their respective excesses, as best suited for one's needs?  A way to marry the flexibility of Graph Databases and the discipline of Relational Databases? This article is part 5 of a growing,  ongoing  series  on Graph Databases and Neo4j Let's Get Concrete Consider a simple scenario with scientific data such as the Sample, Experiment, Study, Run Result , where Samples are used in Experiments, and where Experiments are part of Studies and produce Run Results.  That’s all very easy and intuitive to represent and store in a Labeled Graph Database such as Neo4j .   For example, a rough draft might go like this:   The “labels” (b...

Graph Databases (Neo4j) - a revolution in modeling the real world!

UPDATED Oct. 2023 - I was "married" to Relational Databases for many years... and it was a good "relationship" full of love and productivity - but SOMETHING WAS MISSING! Let me backtrack.   In college, I got a hint of the "pre-relational database" days...  Mercifully, that was largely before my time, but  - primarily through a class - I got a taste of what the world was like before relational databases.  It's an understatement to say: YUCK! Gratitude for the power and convenience of Relational Databases and SQL - and relief at having narrowly averted life before it! - made me an instant mega-fan of that technology.  And for many years I held various jobs that, directly or indirectly, made use of MySQL and other relational databases - whether as a Database Administrator, Full-Stack Developer, Data Scientist, CTO or various other roles. UPDATE: This article is now part 1 of a growing, ongoing series on Graph Databases and Neo4j But ther...

Anti-Aging Research: Science, not Hype

Last updated May 2023 Q: "How is aging a disease?" A: It's a dynamic system that veers away from its homeostasis (normal equilibrium point): hence a form of slow-progressing illness. Labeling it as 'natural' is a surrender to our traditional state of ignorance and powerlessness, which fortunately is beginning to be changed! Aging is "normal" only from the point of view of the "selfish gene", for whom the body is a disposable carrier. Individuals organisms - for whom self-preservation has a different meaning than for genes - have received scant help from evolution... with rare exceptions such as the T. dohrnii jellyfish (which I discuss here )... but now the time has finally arrived for our rational design to remedy some of the cellular flaws that evolution never bothered to correct!   The above is my standard answer to an oft-asked question. The science of aging is by all evidence very misunderstood by the general public.  Hype,...

What are Graph Databases - and Why Should I Care?? : "Graph Databases for Poets"

  This is a very gentle introduction to the subject.  The subtitle is inspired by university courses such as "Physics for Poets"!  (if you're technically inclined, there's an alternate article for you.) It has been said that "The language of physics (or of God) is math".  On a similar note, it could be said that: The language of the biological world - or of any subject or endeavor involving complexity - is networks ('meshes') What is a network?  Think of  it as the familiar 'friends of friends' diagram from social media. Everywhere one turns in biology, there's a network – at the cellular level, tissue level, organ level, ecosystem level.  The weather and other earth systems are networks.  Human societal organization is a network.  Electrical circuits, the Internet, our own brains...  Networks are everywhere! What can we do with networks, to better understand the world around us, or to create something that we need? Broadly ...

Using Neo4j with Python : the Open-Source Library "NeoAccess"

So, you want to build a python app or Jupyter notebook to utilize Neo4j, but aren't too keen on coding a lot of string manipulation to programmatic create ad-hoc Cypher queries?   You're in the right place: the NeoAccess library can do take care of all that, sparing you from lengthy, error-prone development that requires substantial graph-database and software-development expertise! This article is part 4 of a growing,  ongoing  series  on Graph Databases and Neo4j   "NeoAccess" is the bottom layer of the technology stack provided by the BrainAnnex open-source project .  All layers are very modular, and the NeoAccess library may also be used by itself , entirely separately from the rest of the technology stack.  (A diagram of the full stack is shown later in this article.) NeoAccess interacts with the Neo4j Python driver , which is provided by the Neo4j company, to access the database from Python; the API to access that driver is very p...

Visualization of Graph Databases Using Cytoscape.js

(UPDATED APR. 2024)   I have ample evidence from multiple sources that there are strong unmet needs in the area of visualization of graph databases. And whenever there's a vacuum, vendors circle like vultures - with incomplete, non-customizable, and at times ridiculously expensive, closed-box proprietary solutions.   Fortunately, coming to the rescue is the awesome open-source cytoscape.js library ,  an offshoot of the "Cytoscape" project of the  Institute for Systems Biology , a project with a long history that goes back to 2002. One can do amazing custom solutions, relatively easily, when one combines this Cytoscape library with:   1) a front-end framework such as Vue.js   2) backend libraries (for example in python) to prepare and serve the data   For example, a while back I created a visualizer for networks of chemical reactions, for another open-source project I lead ( life123.science )   This visualizer will look and feel generally ...

Neo4j Sandbox Tutorial : try Neo4j and learn Cypher - free and easy!

So, you have an itch to test-drive Neo4j and its Cypher query language.  Maybe you want to learn it, or evaluate it, or introduce colleagues/clients to it.  And you wish for: fast, simple and free! Well, good news: the Neo4j company kindly provides a free, short-term hosted solution called "the Neo4j sandbox" .  Extremely easy to set up and use! This article is part 2 of a growing, ongoing series on Graph Databases and Neo4j Register (free) for the Neo4j "Sandbox" Go to sandbox.neo4j.com , and register with a working email and a password.  That's it! Note that this same email/password will also let you into the Neo4j Community Forums and Support ; the same login for all: very convenient! Launch your instance - blank or pre-populated After registering, go to  sandbox.neo4j.com  , and follow the steps in the diagram below (the choices might differ, but the "Blank Sandbox" should always be there): Too good to be true?  Is there...

Neo4j & Cypher Tutorial : Getting Started with a Graph Database and its Query Language

You have a general idea of what Graph Databases - and Neo4j in particular - are...  But how to get started?  Read on! This article is part 3 of a growing,  ongoing  series  on Graph Databases and Neo4j   If you're new to graph databases, please check out part 1 for an intro and motivation about them.  There, we discussed an example about an extremely simple database involving actors, movies and directors...  and saw how easy the Cypher query language makes it to answer questions such as "which directors have worked with Tom Hanks in 2016" - questions that, when done with relational databases and SQL, turn into a monster of a query and an overly-complicated data model involving a whopping 5 tables! In this tutorial, we will actually carry out that query - and get acquainted with Cypher and the Neo4j browser interface in the process.  This is the dataset we'll be constructing: Get the database in place If you don't already have a datab...

Full-Text Search with the Neo4j Graph Database

(UPDATED May 2024)   Now that we have discussed a full technology stack based on Neo4j (or other graph databases), and that we a design and implementation available from the open-source project BrainAnnex.org  , what next?  What shall we build on top? Well, how about  Full-Text Search ?  This article is part of a growing, ongoing series on Graph Databases and Neo4j Full-Text Searching/Indexing The Brain Annex open-source project includes an implementation of a design that uses the convenient services of its Schema Layer , to provide indexing of word-based documents using Neo4j. The python class FullTextIndexing ( source code ) provides the necessary methods, and it can parse both plain-text and HTML documents (for example, used in "formatted notes"); parsing of PDF files and other formats will be added at a later date. No grammatical analysis ( stemming or lemmatizing ) is done on the text.  However, a long list of common word ("stop ...