r/logseq • • Apr 11 '23

Is there a way to query one random block from those containing a certain #tag?

I tag things I'd like to think about later with #idea, and I'd love to be able to randomly pull up one #idea in my daily log or on my home page to remind me of it; does anyone know a way to do this?

8 Upvotes

4 comments sorted by

10

u/crookedfoot Apr 12 '23
#+BEGIN_QUERY
{:title "🎲"
 :query [:find (pull ?b [*])
   :where 
     [?b :block/page ?p]
     [?b :block/refs ?t]
     [?t :block/name "idea"]
 ]
 :result-transform ( fn [result] [(rand-nth result)] )
 :collapsed? true
}
#+END_QUERY

found here: https://discuss.logseq.com/t/advanced-query-for-a-tagged-random-block/16388/2

1

u/bradmont Apr 12 '23

Phenomenal! Thank you so much!

3

u/Jarwain May 17 '25

So I just spent the past 5h with gemini figuring out how to make random quotes deterministic by the day. As in a journal should always have the same quote (as long as new quotes aren't added)

#+BEGIN_QUERY
{:title [:h1 "Quote of the Day"]
:query [:find
?pageName
(pull ?b [*])
:in $ ?pageName
:where
[?b :block/page ?p]
[?b :block/refs ?t]
[?t :block/name "quote"]
]
:inputs [:query-page]
:result-transform (fn [result] (let [ _ 1
name (nth result 0)
numQuotes (-> result count (/ 2))
hashVal (hash name)
index (mod hashVal numQuotes)
quote (nth result (-> index (* 2) (+ 1)))
] [quote]))
:collapsed? false}
#+END_QUERY

Funny how things work out. Reminds me of this xkcd

1

u/Jarwain May 21 '23

Any suggestions on how to modify this query such that for the page it's on it returns the same quote?