Limit search query results

I would like to limit the amount of returned query results by either being able to set a limit in the query settings (e.g. display only last 7 results by creation date) or by making the date filter (between start end) work for pages.

Use case: I use page properties on my daily pages to track habits (e.g.: yoga:: x if I completed yoga that day). On my monthly page I have a query that lists all pages that have the property „yoga“. This way I get a nice table where the page name equals the date an there’s either an X or blank space for each habit.

My problem is: when I use (between -30d today) in my query to limit results, the table looks weird because „between“ only works on blocks. So what do I do for pages? I’d like to limit the number of results because the table might get very long otherwise and performance will probably suffer. I’d also like to have monthly and weekly pages that only display result for that month/week.

I’m also interested in limiting # of query results, in my case because rendering a lot (~150) of blocks takes a very long time (even though the query itself runs instantly). Pagination would solve this even better.

Without this feature (or some speedup in rendering) Logseq is sadly kind of unusable for me, since my workflow relies on queries that, if not limited/paginated, return too many results to render in a timely speed. I open my To Do dashboard and have to wait 10 seconds to see anything!

1 Like

Hello, @tynka and @idiosyncretic. Were you able to find a solution to this friction point?

Unfortunately no, though I have a hard time imaging that it’s not possible! In the worst case I bet there’s some custom Logseq query attribute that could be created that would pass a limit to the underlying Datomic query. I’m guessing this would be a very easy update for someone already familiar with Datomic and the Logseq codebase.

1 Like

You can try advanced query in that case.

{:title "Yoga Tracker"
 :query [:find (pull ?p [*])
         :in $ ?start ?end
         [?p :block/journal? true]
         [?p :block/journal-day ?d]
         [(>= ?d ?start)]
         [(<= ?d ?end)]
         (has-page-property ?p :yoga)]
 :inputs [:30d-before :today]

For limiting results, you can use take in advanced query. For example, this is a sample query for displaying TODOs sorted by priority and limited to 5 results.

{:title "Top 5 High Priority Tasks"
 :query [:find (pull ?b [*])
         [?b :block/marker "TODO"]]
 :result-transform (fn [result]
                     (->> result
                       (sort-by (fn [h]
                                  (get h :block/priority "Z")))
                       (take 5)))
1 Like

Thank you, @starryveshch—using take worked.

do you have a buy me coffee link?

1 Like

No, but thank you for asking.