Group links/tags in org-roam

Total newbie to org-roam here. Is there a way to group nodes in org-roam in the style of group tags in org-mode but across the whole database. I want to incorporate polyhierarchy in my Zettelkasten in order to summon information on the spot by filtering my database to a handful of nodes (top-down approach as opposed only having the option to navigate laterally from link to link to get to the information I want). Example: #animal (group tag for #cat #dog, etc.) AND #color (group tag for #red #black, etc.) would return nodes about colored animals like “Black cat” or “Purple dogs” without ever explicitly using the word “animal” or “color”, hence the taxonomies in the background.

P.S. I’ve used tags to illustrate my use case but I would want the taxonomies to apply to links as wells, whenever I’m refering to a concept really. I’m assuming tags and links are interchangeable.

So, I understand your use case here but the way you are thinking about it doesn’t really follow the zettelkasten philosophy on which org roam is based. The idea is you don’t need hierarchy because everything is so well linked with one another.

However, the use case you are describing is something that needs a solution and I have an idea for you. To my understanding, you essentially want to group nodes with these tags which are much wider in scope than the tags you already use.

The zettelkasten-y way of doing this is instead of using tags or something like that, create a node in your system with the name “Animals” and one with the name “Color” and then link every node related to those to these. These will be your “index” nodes, also known as entry points or Maps of Contents (MOCs) by some people. Then, you can look at their back links with the org-roam buffer, or use a backlinks searcher sort of thing.

Due to the flexibility of org roam filter functions, you can then create custom query functions with these. I have on my config for example a small section about a recursive backlinks searcher where you get prompted for a node and see its backlinks recursively. Building on that, you can even make a function that accepts n nodes and then runs org-roam-find on all nodes that are backlinked to every selected node. Possibilities there are basically endless. I can even help you do something like that if you want.

Why Org vs Org-roam? If you have confirmed that Org-mode does what you like, I would think you could use it for your Org-roam notes so you keep both.

You are right actually. I can still do org-mode tagging in org-roam! I’m just dissapointed I can’t do the same nesting/grouping with links or can I?

Is this below what you’re going for? I believe this gives you “taxonomies in the background”.

I don’t know if this data structure “does” anything special for you in the current Org-roam implementation. If I remember correctly, Org-roam-ui understands the hierarchical structure within one file like you see in Animal and Color org files and render them accordingly. Something you might like to see for yourself.

Index file for Animal
:ID: A
#+title: Animal
#+filetags: :index:

* Cat
:ID: A.1

* Dog
:ID: A.2
Index file for Color
:ID: C
#+title: Color
#+filetags: :index:

* Red
:ID: C.1

* Black
:ID: C.2
Note File
:ID: N
#+title: Note on a black dog

Today I saw a [[id:A.2][dog]]. It was [[id:C.2][black]]. 

Thank you for that. I perfectly understand the philosophy behind Zettelkasten but I find it to be limiting. I see tags/links as a level of abstracted concept (not as a mere reference to something). I need to be able to use broader tags/links which encompass narrower ones as another layer of abstraction and keep moving up the tree of knowledge.

The solution you proposed might just work using custom query functions. Excuse my using layman terms since I’m not a programmer: is there a way, using #animal, to return all n nodes linked to it or any subnode I’ve specified, not knowing in advanced how many levels deep I’ve gone? e.g. animal/mammal/cow It would have to go down the tree until it touches all terminal nodes.

The next step is for me to look for the intersection between taxonomies: What nodes do I have that relate to #mammal AND #philosophy (or a specific subset of philosophy). I want to be in complete control of the level of abstraction for each component. If my main brain can do it, I want my second brain to be able to.

Interesting! I like this idea of giving each subset an ID. Would I then be able to query Animal AND Color then return “Today I saw a dog. It was black”? but not, say, “Today I saw a dog.” (no reference to color)?

Yes, you should be able to, but you’d need write your own query. There should be many different approaches. One way I could think of is to use org-roam-db-query and write your own SQL query for the database. You can inspect the DB schemata by looking at variable org-roam-db--table-schemata. I think you would only need to use tables nodes and links.

If you always query from the top-level category (“Animal” and “Color” are level 0 = top-level categories), then the logic would get easier (starting from a sub-level would need to only look at that particular subtree – the database does not have this tree information, only the file does). The logic should be something like this below. I am not proficient in SQL and I do not have good sample data, so I won’t be able to spend time to have a prototype.

  1. For given two (or n) top-categories file-node IDs, find their respective file from nodes table

  2. For all the files found in step 1, find all the IDs contained in them from nodes table

  3. For all the IDs found in step 2, find from links table all sources that have at least one ID from each and every category as dest
    (A or A.1 or A.2) and (C or C.1 or C.2) and (N.n) and ...

This should give you all the IDs of the nodes (sources are IDs) that have reference to both an Animal (cat or dog, or animal) and a Color (black or red, or color).

If you’re still up for it, I would love your help on this. Pretty sure it’s exactly what I’m looking to do! Thank you for offering.

Ok, so I played around a bit with this concept and I hacked this little thing. Took me like 3 hours so its definitely not polished or tested amply, but it appears to work fine. The first section is more or less the backbone of the process, which I recommend you take a look at so you get more acquainted with hacking on org-roam (if you know elisp that is, which you definitely should if you want to play around with your config), while the third section are the end-user functions which you will need to run for what you asked.

Note that there are no dependencies besides org-roam for all but the last function. The last function, as mentioned in its docstring is an extension to my aforementioned recursive backlink searcher (org-roam-backlinks-search), which you can also try. The code for it is just above this part.

If you have any issues, feel free to ask, happy to help!

1 Like