How to find all tasks under blocks tagged with project namespaces?

Perfect! It works!
Now I really can draw the Power of Logseq for my purposes.
Thanks, mighty wizard.
I do not understand the magic yet, but can use it and am eager to learn more.

1 Like

When i put ā€œprojectā€ (or the final target string) in lowercase letters, it works as intentioned.
Next iā€™ll try to filter on tasks with priority ā€œAā€.
Maybe i can do it without help from a magician.

Dear Siferiax, I do not want to be greedy but could you extend your helpful Query so that it Filters Namespace + Tasks + Priority ā€œAā€
As a beginner i still struggle a little with learning datalog.

Yes. :block/name is always lowercase. I forgot to mention, I generally add that for clarity!
Pages have 2 names, one is lowercase and stored in the attribute :block/name and the other is their original name case stored in :block/original-name.
I generally use :block/name so we donā€™t have to think how exactly we wrote the name of a page :slight_smile:

For filtering priority we have the attribute :block/priority
So we will need to add the line: [?b :block/priority "A"] to the query.
Alternatively we can do this the same way as we did for the task itself.

[?b :block/priority ?prio]
[(contains? #{"A" "B"} ?prio)]

In this case finding both A and B priority tasks. Just to illustrate how you can use multiple values for the contains? function.

Complete query for clarity:

#+BEGIN_QUERY
{:title "Find Sub-Tasks"
 :query [:find (pull ?b [*])
   :in $ %
   :where
     [?ns :block/name "project"]
     (check-ns ?ns ?p)
     [?b :block/page ?p]
     [?b :block/marker ?marker]
     [(contains? #{"LATER"} ?marker)]
     [?b :block/priority ?prio]
     [(contains? #{"A"} ?prio)]
 ]
 :rules [
   [(check-ns ?ns ?page)
     [?page :block/namespace ?ns]
   ]
   [(check-ns ?ns ?page)
     [?page :block/namespace ?t]
     (check-ns ?ns ?t)
   ]
 ]
}
#+END_QUERY
1 Like

Hi there @Siferiax , many thanks for the solutionā€¦ its nearly perfect.
If you allow one last question:
Sometimes i would also note some tasks in the root layer of a namespace (ā€œproject/ā€¦ā€).
Is it possible to include these tasks with priority into the query results?

Best regards, already a fanā€¦ :slight_smile:

Someone should develop a GPT based AI that can transform natural language queries into advanced logseq queries! I wonder if anyone is thinking about this?

1 Like

Hi there,
i already found the great Query-Builder-Tool from adxsoft

but couldnā€™t figure out how to build a recursive query for the namespaces like the one @Siferiax provided. Maybe its just a matter of time learning the nuances of datalog.
For now the hints from Siferiax provides a very powerful solution for project management in Logseq.

There seems to be another question/solution that tackles the problem of queries in hierarchies.

Yes, definitely!
Change [?b :block/page ?p] to:

(or
  [?b :block/page ?ns]
  [?b :block/page ?p]
)
1 Like

Yeah that solution does basically the same thing as my solution :wink:

Thanks mighty Query-Wizard! :smiley:

So this query you suggested works

#+BEGIN_QUERY
{:title "šŸ”Ø All projects"
  :query [:find (pull ?b [*])
          :where
          [?p :block/name "projects"]
          [?par :block/refs ?p]
          (or [?b :block/parent ?par]
     [?b :block/refs ?p])
          [?b :block/marker ?marker]
          [(contains? #{"TODO"} ?marker)]
  ]
:collapsed? true
}
#+END_QUERY

But now Iā€™m trying the following as the inverse - i.e. find all todos that donā€™t have any project tags, but itā€™s finding no results (despite the fact that Iā€™ve created some test todos without project tags). Any idea how I can fix this? Thanks very much in advance! :slight_smile:

#+BEGIN_QUERY
{:title "šŸ”Ø Inbox"
  :query [:find (pull ?b [*])
          :where
          [?p :block/name "projects"]
          [?par :block/refs ?p]
          (not (or [?b :block/parent ?par]
     [?b :block/refs ?p]))
          [?b :block/marker ?marker]
          [(contains? #{"TODO"} ?marker)]
  ]
:collapsed? false
}
#+END_QUERY

Check out this post:

(Sorry doing this from a hospital bed on my phone :rofl: but thought I could at least point you in the right direction)

Omg, youā€™re my savior - this works perfectly. For whatever reason youā€™re in the hospital - I hope you get well soon! :blush::blush:

1 Like

Thanks. Had emergency surgery, but Iā€™m back home and recovering just fine.

This thread is so helpful! I would like to sort the output by project - right now it is sorted by date of journal entry.

I tried adding:

:result-transform (fn [result]
                                 (sort-by (fn [h]
                                                  (get h :block/name)) result))

With no change

It outputs blocks, not pages. So we need to first get the page.
(get h :block/name)
Needs to be (get-in h [:block/page :block/name])

1 Like

@Siferiax can you please add that sort criteria into the original query and republish? something im not understanding - thank you!

Which query do you mean exactly? There is a bunch of queries posted in this thread and Iā€™m not sure which you refer to.