Query for recently accessed/modified/created pages, limiting the query to top-N

Newbie here. I am trying to come up with a query to put on my right-side bar that list the last N (e.g. 10 or 20) recently accessed pages, ideally ordered by recency (mst recent first) and paginated (showing 5 at a time).

(The Recent Pages on the Left menu does not help in my use case, I need this on the right-side bar)

My 3-day logseq experience and null datalog experience is not helping me on this.

I found this: List of recently created/modified pages (timestamp issues)

but that approach is based on limiting the timeframe (e.g. pages created in the last X days) which is not what I need. I need the last N pages opened regardless of their access timestamp.

I couldn’t even extract the top-N results from the timestamp-based example above, the below doesn’t work

#+BEGIN_QUERY
{:title [:h3 "Recent Pages"]
:query [:find (pull ?b [* {:block/_parent ...}])
:in $ ?end
:where
[?b :block/updated-at ?v]
[(- ?end 86400000 ) ?period]
[(>= ?v ?period)]
[(< ?v ?end)]
]
:inputs [:end-of-today-ms]
:result-transform (fn [result] ( ->> result  (take(10))))
}
#+END_QUERY

To limit the results to N, can use this:

:result-transform (fn [result] (take N result) )

To sort the results, can replace result with a sort-by expression like this:

:result-transform (fn [result] (take N
  (sort-by ...)
) )
1 Like

Thanks! the following worked (with reverse sorting and top-N, recent pages in the last 30 days)

#+BEGIN_QUERY
{:title [:h3 "Recent Pages"]
:query [:find (pull ?b [* {:block/_parent ...}])
:in $ ?end
:where
[?b :block/updated-at ?v]
[(- ?end 30 * 24 * 60 * 60 * 1000 ) ?period]
[(>= ?v ?period)]
[(< ?v ?end)]
]
:inputs [:end-of-today-ms]
:result-transform (fn [result] (take 15
  (sort-by (comp - (fn [h]
    (get h :block/updated-at))) result)
) )
}
#+END_QUERY