Advanced query for a tagged random block?

{:title "🎲"
 :query [:find (pull ?b [*])
   :where 
     [?b :block/page ?p]
     [?b :block/refs ?t]
     (or
       [?t :block/name "tag1"]
       [?t :block/name "tag2"]
     )
 ]
 :result-transform ( fn [result] [(rand-nth result)] )
 :collapsed? true
}

I rewrote it like so, because

  1. that’s more readable
  2. if you didn’t make the tag yet it doesn’t throw an error
  3. we want the or for the tag name and not whether there’s a reference.
    Most importantly though we use refs instead of path-refs.

From: logseq/schema.cljs at master · logseq/logseq · GitHub

;; reference blocks
:block/refs {:db/valueType :db.type/ref
             :db/cardinality :db.cardinality/many}
;; referenced pages inherited from the parents
:block/path-refs {:db/valueType   :db.type/ref
                  :db/cardinality :db.cardinality/many}

That said, this still shows the child blocks, but as part of the block result you want.
image

1 Like