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

This query doesn’t work when I paste it in a block. Is there anything special I need to do? Thanks

  • “doesn’t work” doesn’t mean much.
    • Please describe in what way it doesn’t work, provide screenshots etc.
  • The query actually works, so ensure that the pasting process is correct.
    • Compare the text of the query here with the text of the query in the block.
    • Ensure that the pasted text is plain, i.e. not formatted.
      • e.g. it should not contain asterisks etc.
      • A couple of ways to remove formatting:
        • paste with Ctrl + Shift + V
        • paste in a plain notepad, then copy again from there

Thanks for your help. Sorry for my lack of clarity. I meant that Logseq was just treating the block like regular text. It wasn’t processing it as a query.

But now it’s working. The problem was that when I pasted into Logseq, it added an asterisk (*) at the beginning and end of the first and last line. Once I removed those, it works.