Advanced Query Collection

Logseq Advanced Query supports the creation of functions to re-use in queries. These functions are are stored in the config.edn.

Query Transform collection

Define in config.edn, under :query/result-transforms your collection of the query functions.

Step 1

Add the following query transforms in the config.edn

;; Pre-defined :result-transform function to use in Query
 :query/result-transforms
 {:sort-by-priority (fn [result] (sort-by (fn [h] (get h :block/priority "Z")) result))
  :sort-by-scheduled (fn [result] (sort-by (fn [d] (get d :block/scheduled)) result))
  :sort-by-deadline (fn [result] (sort-by (fn [d] (get d :block/deadline)) result))
  :sort-by-scheduled-reverse (fn [result] (sort-by (comp - (fn [d] (get d :block/scheduled))) result))
  :sort-by-deadline-reverse (fn [result] (sort-by (comp - (fn [h] (get h :block/deadline))) result))
  :sort-by-priority-and-deadline
  (fn [result] (sort-by (juxt (fn [h] (get h :block/priority)) (fn [i] (get i :block/deadline)))))
  :sort-by-scheduled-and-deadline
  (fn [result] (sort-by (juxt (fn [r] (get-in r [:block/scheduled])) (fn [r] (get-in r [:block/deadline]))) result))

  :sort-by-deadline-and-scheduled
  (fn [result] (sort-by (juxt (fn [d] (get d :block/deadline)) (fn [d] (get d :block/scheduled))) result))

  :sort-by-deadline-or-scheduled
  (fn [result]
    (sort-by (fn [b]
               (or (get b :block/scheduled) (get b :block/deadline))) result))
  :sort-by-updated-at
  (fn [result] (sort-by (fn [h] (get h :block/updated-at))))

  :sort-by-created-at
  (fn [result] (sort-by (fn [h] (get h :block/created-at))))
  :sort-by-time-tracking
  (fn [r]
    (sort-by
     (fn [h]
       (clojure.string/join (or
                             (not-empty
                              (rest
                               (re-find
                                (re-pattern "\\-\\-\\[(\\d+)\\-(\\d+)\\-(\\d+)\\s.*\\s(\\d+):(\\d+):(\\d+)\\]") (get h :block/content)))) "99999999999999"))) r))
  :sort-by-journal-day
  (fn [result] (sort-by (fn [s] (get s :block/journal-day)) (fn [a b] (compare b a)) result))

  :sort-by-property
  (fn [result]
    (sort-by (fn [s]
               (get s :block/journal-day)) (fn [a b] (compare b a)) result))}

Step 2

In your advance query, add your query function for a simple a re-usable

Example of the query using the query/transforms functions

:sort-by-time-tracking

;; In an advanced query
{ :title "Sort By Time Tracked"
  :query [:find (pull ?b [*])
      :in $ ?current
      :where
          [?p :block/name ?current]
          [?b :block/page ?p]
          [?b :block/marker "TODO"]
  ]
  :inputs [:query-page]
  :result-transform :sort-by-time-tracking
}

Query View collection

Coming Soon!!!

Resources

Documentation

What is next?

  • :loudspeaker: Share your query/transforms and query/views in the comment. They will be added to the collection for the community.

Thank you to community members for sharing queries where most of these functions were pulled from, in Discord or the forum.

Thanks for this post, which I found very helpful.

I notice that the two documentation links are to pages that no longer exist. Any chance you can update them?

I think it’s the two blocks on this page:
https://docs.logseq.com/#/page/config.edn/block/specific%20config%20options%3A

Thanks for that. That documentation is helpful for people like you who are trying to use config.edn. But for someone like me trying to figure out how the :result-transform clause actually works, just another example. It would be so helpful to have a clear specification of how the clause works and what kinds of functions it will accept (I presume more than just sort-by).

It accepts clojure functions.
https://clojuredocs.org/clojure.core
Should be all in clojure core.
And there are some extras you can find here.

Under def query-fns
Learning about clojure functions is the best way to write more complex result-transform clauses. Though the where clauses are pretty powerful in and of itself.

1 Like

Is this feature at all possible to use with query :rules too?

For example, I often have useful rules to reuse to check whether one page is in the namespace of another, or whether one block is the ancestor of another.

It would be great if this could help DRY out this code, but as far as I can tell, this may not fit the bill.

That would be a good idea for a feature request!
Would indeed help with making (parts of) queries more easily reusable.

1 Like