Advanced query for multiple links/tags

I have two types of structure for my gratitude journals:

  • [[journal]]
    • [[gratitude]]
      • For the wind in my hair…


for a page tagged as journal:

  • tags:: journal
  • [[gratitude]]
    • For the smell of coffee in the morning…

If I use {{query (and [[gratitude]] [[journal]])}} this only returns the first scenario. The page tag method does not work.

My attempt at an advanced query looked like this:

{:title "Find: journal2 and gratitude2"
:query [:find (pull ?b [*])
[?b :block/ref-pages ?p]
[?p :block/name ?tag]
[(contains? #{"journal" "gratitude"} ?tag)]]

But the logic works as an OR statement - and so I get results for journal, or gratitude, which I don’t want, as well as the journal AND gratitude results that I do want…

Any ideas? Thanks.

Welcome Nick!

{:title "Find: journal2 and gratitude2"
 :query [:find (pull ?b [*])
   [?b :block/path-refs [:block/name "journal"]]
   [?b :block/path-refs [:block/name "gratitude"]]

Thanks! This matches the first type of my gratitude journal structure, but not the second - i.e. it doesn’t match where I have a page tag. Maybe I’m mistaken with how page tags work…

Could you maybe post something you would like to be matched?

Thanks for your help so far. My test page looks like this:

tags:: [[journal]]

- [[gratitude]] for...
	- dhjlfa
	- fjal
	- fjhaiskohjofasd
	- fashj
	- fsahj

I want to search for [[gratitude]] AND [[journal]], but am struggling to return this when the ‘journal’ part is a page-tag.

Hello Nick.

Not sure if you’ve figured out an answer (please update if so!), but I think I can explain a bit as to why you’re having trouble matching both.

You’re starting with a block search in your query, then looking for blocks that are tagged both journal AND gratitude. The hierarchy inherits tags, so your first case is returned.

The problem with the second is that the block you’re targeting, beneath gratitude, is NOT tagged with journal. That tag only applies to the page block (basically the title).

So you would either need a query that returns “the blocks in journal pages that are tagged gratitude”, or restructure those pages so the blocks you want have both tags.

I could be wrong, but that’s what I see.