reddit / emacs

How to get a list of top referenced nodes in Org roam?

underconstructions · 2022-10-11 00:58
title
chrisdbarrett · 2022-10-11 04:08
org-roam doesn't expose a way to get this that I can see without querying its DB directly. So, you can get this info by querying the `links` table in the DB. The target of a link is its `dest` attribute. Try this out and see if it looks right to you: ```elisp (seq-map (pcase-lambda (`(,id ,count)) (let ((node (org-roam-node-from-id id))) (list :id id :title (org-roam-node-title node) :count count))) (org-roam-db-query [:select [dest (funcall count dest)] :from links :group-by dest :order-by (funcall count dest) :desc :limit 5])) ``` That query should return the 5 nodes with the most backlinks. I don't think org-roam ships with a nice way to display arbitrary lists of nodes out-of-the-box. I rolled something myself, which is in my [nursery](https://github.com/chrisbarrett/nursery). If you want to use that, you could do something like: ```elisp (defvar org-roam-list-most-linked-count 5) (defun org-roam-list-most-linked () "List nodes with the highest number of links." (interactive) (org-roam-review-display-buffer-and-select (org-roam-review-create-buffer :title "Most linked" :nodes (lambda () (seq-map (-compose #'org-roam-node-from-id #'car) (org-roam-db-query [:select [dest] :from links :group-by dest :order-by (funcall count dest) :desc :limit $s1] org-roam-list-most-linked-count)))))) ```
AuroraDraco · 2022-10-12 19:55
The other commentors approach is a nice one but I have another one you might find useful which I use myself. Basically, I have a function that runs \`org-roam-node-find\` sorted by the number of backlinks, so the first entries there are obviously the top referenced notes. For this, I have defined a method that takes a node and returns the number of backlinks it has, then a comparison function that takes 2 nodes and finds which one has the most backlinks, which function I pass to \`org-roam-node-find\` as its sorting function. Here is the code ``` (cl-defmethod org-roam-node-backlinkscount-number ((node org-roam-node)) "Access slot \"backlinks\" of org-roam-node struct CL-X. This is identical to `org-roam-node-backlinkscount' with the difference that it returns a number instead of a fromatted string. This is to be used in `org-roam-node-sort-by-backlinks'" (let* ((count (caar (org-roam-db-query [:select (funcall count source) :from links :where (= dest $s1) :and (= type "id")] (org-roam-node-id node))))) count)) (defun org-roam-node-sort-by-backlinks (completion-a completion-b) "Sorting function for org-roam that sorts the list of nodes by the number of backlinks. This is the sorting function in `org-roam-node-find-by-backlinks'" (let ((node-a (cdr completion-a)) (node-b (cdr completion-b))) (>= (org-roam-node-backlinkscount-number node-a) (org-roam-node-backlinkscount-number node-b)))) (defun org-roam-node-find-by-backlinks () "Essentially works like `org-roam-node-find' (although it uses a combination of `find-file' and `org-roam-node-read' to accomplish that and not `org-roam-node-find' as only `org-roam-node-read' can take a sorting function as an argument) but the list of nodes is sorted by the number of backlinks instead of most recent nodes. Sorting is done with `org-roam-node-sort-by-backlinks'" (interactive) (find-file (org-roam-node-file (org-roam-node-read nil nil #'org-roam-node-sort-by-backlinks)))) ```
chrisdbarrett · 2022-10-12 20:37
That’s a cool idea!
underconstructions · 2022-10-25 07:24
Thank you