Advanced query for a tagged random block?

Currently I use the following query to show me a random block that contains either [[tag1]] or [[tag2]]:

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

The problem is, it fetches the children of such tagged blocks into the pool which are all useless to me. If I only want the blocks with the tag but not their children, how should I write the query?

{: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

Thank you so much! I ended up with that query by sticking two other queries together without understanding what they do :sweat_smile: and I didn’t know schema.cljs has simple instructions inside. It’s a great resource!

Your query is perfect. I do want to see the children. I just don’t want the children & grandchildren to be in the pool and skew the probabilities^^

1 Like

Wait, I noticed a problem. This query, when expanded, keeps executing itself as long as my cursor hovers over the query title, so it keeps blinking and giving me new results :open_mouth:

Do you see the same thing?

Yeah that’s new in 0.9.1… it’s annoying tbh lol.

1 Like

Hi I just noticed the same issue…do we know how to solve it?
I also noticed that I have 3 different quotes in my mac, iphone and ipad…!!!

I don’t :no_mouth: I didn’t post on GitHub either. got lazy and just made a mental note to keep my cursor out of the title line :laughing:

what’s with the quotes? a sync bug?

In discord they replied to me that’s normal to have different quotes in different devices. But it’s not normal that the quote is changing continuously so probably a bug if the latest version

ohh I see what’s you’re asking. yes it’s normal. because this is a kind of “random number generator”, which when executed will give you a different result every time. When you open logseq on a different device, the query executes itself.

the result changing continuously because the query keeps executing itself when the cursor hovers over the “query title” line – I agree this is a bug.