Sorting org-roam files by frequency

I use ivy as my org-roam-completion system. When I enter the command org-roam-find-file I get a popup window which allows me to enter the name of a file. I also have ivy-prescient mode installed. I would like to have the most recently opened files gravitate to the top of the list, rather than the files most recently created. How can I achieve this?

@revrari
A thought occurred to me: is this possible? Apologies if I am missing something.

Most recently created file is the one you have just opened; that is, most recently created is the most recently opened, isn’t it?

The files I create are not necessarily the same as the files I am frequently accessing. I may create file-a file-b and file-c. However in the same week I may access the file test-a more often. Still, the files are showing listed as:
file-a
file-b
file-c

Yes, that makes sense, and consistent with the title. I think I was thrown off by the body of text asking for the most recently opened files (“recently” vs “frequently”).

I have moved on from Ivy and Ivy-prescient, but Prescient describes its algorithm, which keeps track of access frequency, It seems to specifically proritise recency (“The last few candidates you selected are displayed first”). Perhaps you could influence this by the customizing mentioned here, or explore this avenue to change it with help of a Prescient community?

The last few candidates you selected are displayed first, followed by the most frequently selected ones, and then the remaining candidates are sorted by length. If you don’t like the algorithm used for filtering, you can choose a different one by customizing prescient-filter-method .

Just as an idea:

If you are OK with Prescient completely not caring about recency globally (not just Org-roam), you might be able to force it to prioritise frequency by setting prescient-history-length to zero (default seems 100).

I only had a quick look at precient code; it appears to me that using recency (history) is baked in the core of its logic. I could be wrong (I do not fully understand what this sorting logic really does).

If my understanding is correct (enough), then it would be a challenge to customise this behaviour.

;; Macro: prescient--sort-compare
  `(progn
     (let* ((p1 (gethash c1 hist len)) ; <<< Here recency first
            (p2 (gethash c2 hist len)))
       (or (< p1 p2)
           (and (eq p1 p2)
                (let* ((f1 (gethash c1 freq 0)) ;<<< Then frequency 
                       (f2 (gethash c2 freq 0)))
                  (or (> f1 f2)
                      (and (eq f1 f2)
                           len-enable
                           (< (length c1)
                              (length c2))))))))))

Your message has made me think for the 1st time about the distinction between recency and frequency. I had conflated them! :face_with_hand_over_mouth: This distinction is something I am now trying to understand. I have tinkered with a few things, but I am going to need some time to test things out. I will revert back once I have a clearer path. Many thanks.

1 Like

Okay. I have managed to get ivy to sort things properly. It remembers my choices within a session. However, when the session ends, it defaults to the old list.

1 Like

@nobiot, to make a slight tangent… why did you move on from ivy and what did you move to? Is there something more appropriate for org-roam?

I am not committed to using ivy and prescient. Before that I used ido as a completion framework, but did not get different results. I could not order the files by frequency of use. I would be happy to use anything else that you suggest.

Ivy was good, and didn’t have any issue. It’s just my quirky preference. I generally want to try Emacs built-in as much as I can make it work, and to only add focused packages that do one thing well. Ivy felt (though working perfectly, and totally my personal judgement) like a big package that tries to many things, overriding the Emacs built-in too much.

Long story short, I first moved to selectrum + consult + embark + marginalia. And then I removed selectrum, and instead added icomplete-vertical + orderless. I like the combo so far.

This video from Prot is something I am trying to emulate, but I am not there yet.

This does not sort the candidates by frequency so not good for what @revrari has achieved with Ivy.

1 Like

Depending on how you made it work, you might like to see if you can persist elements that you use for your sorting mechanism in a specific file like prescient-persist-mode does – I presume you use it already, and it does not sufficiently cover your needs for frequency. Or even the top-most candidates.

You are absolutely correct. I tried prescient-persist-mode and it seemed to make no difference at all.